mirror of
https://github.com/danog/Valinor.git
synced 2024-11-30 04:39:05 +01:00
69ad3f4777
The cache implementation that was previously injected in the mapper builder must now be manually injected. This gives better control on when the cache should be enabled, especially depending on which environment the application is running. The library provides a cache implementation out of the box, which saves cache entries into the file system. It is also possible to use any PSR-16 compliant implementation, as long as it is capable of caching the entries handled by the library. ```php $cache = new \CuyZ\Valinor\Cache\FileSystemCache('path/to/cache-dir'); (new \CuyZ\Valinor\MapperBuilder()) ->withCache($cache) ->mapper() ->map(SomeClass::class, [/* … */]); ```
70 lines
1.8 KiB
PHP
70 lines
1.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace CuyZ\Valinor\Tests\Integration\Mapping\Attribute;
|
|
|
|
use Attribute;
|
|
use CuyZ\Valinor\Mapper\MappingError;
|
|
use CuyZ\Valinor\Mapper\Tree\Shell;
|
|
use CuyZ\Valinor\Mapper\Tree\Visitor\ShellVisitor;
|
|
use CuyZ\Valinor\MapperBuilder;
|
|
use CuyZ\Valinor\Tests\Integration\IntegrationTest;
|
|
use Doctrine\Common\Annotations\Annotation\NamedArgumentConstructor;
|
|
|
|
final class ShellVisitorMappingTest extends IntegrationTest
|
|
{
|
|
public function test_shell_visitor_attributes_are_called_during_mapping(): void
|
|
{
|
|
try {
|
|
$result = (new MapperBuilder())->enableLegacyDoctrineAnnotations()->mapper()->map(
|
|
ObjectWithShellVisitorAttributes::class,
|
|
[
|
|
'valueA' => 'foo',
|
|
'valueB' => 'foo',
|
|
]
|
|
);
|
|
} catch (MappingError $error) {
|
|
$this->mappingFail($error);
|
|
}
|
|
|
|
self::assertSame('bar', $result->valueA);
|
|
self::assertSame('baz', $result->valueB);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* @Annotation
|
|
* @NamedArgumentConstructor
|
|
*/
|
|
#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER | Attribute::IS_REPEATABLE)]
|
|
final class ValueModifierAttribute implements ShellVisitor
|
|
{
|
|
private string $value;
|
|
|
|
public function __construct(string $value)
|
|
{
|
|
$this->value = $value;
|
|
}
|
|
|
|
public function visit(Shell $shell): Shell
|
|
{
|
|
return $shell->withValue($this->value);
|
|
}
|
|
}
|
|
|
|
final class ObjectWithShellVisitorAttributes
|
|
{
|
|
/** @ValueModifierAttribute(value="bar") */
|
|
#[ValueModifierAttribute('bar')]
|
|
public string $valueA = 'Schwifty!';
|
|
|
|
/**
|
|
* @ValueModifierAttribute(value="bar")
|
|
* @ValueModifierAttribute(value="baz")
|
|
*/
|
|
#[ValueModifierAttribute('bar')]
|
|
#[ValueModifierAttribute('baz')]
|
|
public string $valueB = 'Schwifty!';
|
|
}
|