2021-11-28 17:43:02 +01:00
|
|
|
<?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;
|
2022-05-22 20:43:01 +02:00
|
|
|
use CuyZ\Valinor\MapperBuilder;
|
2021-11-28 17:43:02 +01:00
|
|
|
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 {
|
2022-05-22 20:43:01 +02:00
|
|
|
$result = (new MapperBuilder())->enableLegacyDoctrineAnnotations()->mapper()->map(
|
2021-11-28 17:43:02 +01:00
|
|
|
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!';
|
|
|
|
}
|