forked from CuyZ/Valinor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: introduce
AsTransformer
attribute
This attribute can be used to automatically register a transformer attribute. ```php namespace My\App; #[\CuyZ\Valinor\Normalizer\AsTransformer] #[\Attribute(\Attribute::TARGET_PROPERTY)] final class DateTimeFormat { public function __construct(private string $format) {} public function normalize(\DateTimeInterface $date): string { return $date->format($this->format); } } final readonly class Event { public function __construct( public string $eventName, #[\My\App\DateTimeFormat('Y/m/d')] public \DateTimeInterface $date, ) {} } (new \CuyZ\Valinor\MapperBuilder()) ->normalizer(\CuyZ\Valinor\Normalizer\Format::array()) ->normalize(new \My\App\Event( eventName: 'Release of legendary album', date: new \DateTimeImmutable('1971-11-08'), )); // [ // 'eventName' => 'Release of legendary album', // 'date' => '1971/11/08', // ]
- Loading branch information
Showing
6 changed files
with
207 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Normalizer; | ||
|
||
use Attribute; | ||
|
||
/** | ||
* This attribute can be used to automatically register a transformer attribute. | ||
* | ||
* When there is no control over the transformer attribute class, the following | ||
* method can be used: @see \CuyZ\Valinor\MapperBuilder::registerTransformer | ||
* | ||
* ```php | ||
* namespace My\App; | ||
* | ||
* #[\CuyZ\Valinor\Normalizer\AsTransformer] | ||
* #[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
* final class DateTimeFormat | ||
* { | ||
* public function __construct(private string $format) {} | ||
* | ||
* public function normalize(\DateTimeInterface $date): string | ||
* { | ||
* return $date->format($this->format); | ||
* } | ||
* } | ||
* | ||
* final readonly class Event | ||
* { | ||
* public function __construct( | ||
* public string $eventName, | ||
* #[\My\App\DateTimeFormat('Y/m/d')] | ||
* public \DateTimeInterface $date, | ||
* ) {} | ||
* } | ||
* | ||
* (new \CuyZ\Valinor\MapperBuilder()) | ||
* ->normalizer(\CuyZ\Valinor\Normalizer\Format::array()) | ||
* ->normalize(new \My\App\Event( | ||
* eventName: 'Release of legendary album', | ||
* date: new \DateTimeImmutable('1971-11-08'), | ||
* )); | ||
* | ||
* // [ | ||
* // 'eventName' => 'Release of legendary album', | ||
* // 'date' => '1971/11/08', | ||
* // ] | ||
* ``` | ||
* | ||
* @api | ||
*/ | ||
#[Attribute(Attribute::TARGET_CLASS)] | ||
final class AsTransformer {} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
tests/Integration/Normalizer/AsTransformerAttributeTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace CuyZ\Valinor\Tests\Integration\Normalizer; | ||
|
||
use Attribute; | ||
use CuyZ\Valinor\Normalizer\AsTransformer; | ||
use CuyZ\Valinor\Normalizer\Format; | ||
use CuyZ\Valinor\Tests\Integration\IntegrationTestCase; | ||
|
||
use function strtoupper; | ||
|
||
final class AsTransformerAttributeTest extends IntegrationTestCase | ||
{ | ||
public function test_property_transformer_attribute_registered_with_attribute_is_used(): void | ||
{ | ||
$class = new class ('foo') { | ||
public function __construct( | ||
#[TransformerAttributeForString] | ||
public string $value, | ||
) {} | ||
}; | ||
|
||
$result = $this->mapperBuilder() | ||
->normalizer(Format::array()) | ||
->normalize($class); | ||
|
||
self::assertSame(['value' => 'FOO'], $result); | ||
} | ||
|
||
public function test_class_transformer_attribute_registered_with_attribute_is_used(): void | ||
{ | ||
$class = new #[TransformerAttributeForObject] class ('foo') { | ||
public function __construct( | ||
public string $value, | ||
) {} | ||
}; | ||
|
||
$result = $this->mapperBuilder() | ||
->normalizer(Format::array()) | ||
->normalize($class); | ||
|
||
self::assertSame(['value' => 'foo', 'added' => 'foo'], $result); | ||
} | ||
|
||
public function test_property_key_transformer_attribute_registered_with_attribute_is_used(): void | ||
{ | ||
$class = new class ('foo') { | ||
public function __construct( | ||
#[TransformerAttributeForStringKey] | ||
public string $value, | ||
) {} | ||
}; | ||
|
||
$result = $this->mapperBuilder() | ||
->normalizer(Format::array()) | ||
->normalize($class); | ||
|
||
self::assertSame(['VALUE' => 'foo'], $result); | ||
} | ||
} | ||
|
||
#[AsTransformer, Attribute(Attribute::TARGET_PROPERTY)] | ||
final class TransformerAttributeForString | ||
{ | ||
public function normalize(string $value): string | ||
{ | ||
return strtoupper($value); | ||
} | ||
} | ||
|
||
#[AsTransformer, Attribute(Attribute::TARGET_CLASS)] | ||
final class TransformerAttributeForObject | ||
{ | ||
/** | ||
* @return array<mixed> | ||
*/ | ||
public function normalize(object $object, callable $next): array | ||
{ | ||
return $next() + ['added' => 'foo']; | ||
} | ||
} | ||
|
||
#[AsTransformer, Attribute(Attribute::TARGET_PROPERTY)] | ||
final class TransformerAttributeForStringKey | ||
{ | ||
public function normalizeKey(string $value): string | ||
{ | ||
return strtoupper($value); | ||
} | ||
} |