|
| 1 | +Data Refiners |
| 2 | += |
| 3 | + |
| 4 | +In the deserialization process, sometimes we may need to refine the data before it is passed to the deserializer. |
| 5 | +This is where Data Refiners come in. |
| 6 | + |
| 7 | +A pretty good example would be DateTimes. When attempting to create an instanceof DateTime, we may need to |
| 8 | +be aware of specific formats that the DateTime can be created from. |
| 9 | + |
| 10 | +By default, these are the DataRefiners that are available in the library: |
| 11 | +- [DateTimeRefiner](#DateTimeRefiner) - Refines the data to a DateTimeImmutable instance depending on the format provided. |
| 12 | + |
| 13 | +DateTimeRefiner |
| 14 | +- |
| 15 | + |
| 16 | +```php |
| 17 | +use Nuxtifyts\PhpDto\Data; |
| 18 | +use DateTimeImmutable; |
| 19 | + |
| 20 | +final readonly class DateRangeData extends Data |
| 21 | +{ |
| 22 | + public function __construct( |
| 23 | + public ?DateTimeImmutable $start, |
| 24 | + public ?DateTimeImmutable $end |
| 25 | + ) {} |
| 26 | +} |
| 27 | +``` |
| 28 | + |
| 29 | +With this DTO, if we try to hydrate it with a custom format `'Y/m-d'`, it will fail. |
| 30 | + |
| 31 | +```php |
| 32 | +DateRangeData::from([ |
| 33 | + 'start' => '2023/01-12', |
| 34 | + 'end' => '2023/01-14' |
| 35 | +]); |
| 36 | +``` |
| 37 | + |
| 38 | +To resolve this, we may need to specify a Data Refiner that will help deserialize the data. |
| 39 | + |
| 40 | +```php |
| 41 | +use Nuxtifyts\PhpDto\Data; |
| 42 | +use DateTimeImmutable; |
| 43 | +use Nuxtifyts\PhpDto\Attributes\Property\WithRefiner; |
| 44 | +use Nuxtifyts\PhpDto\DataRefiners\DateTimeRefiner; |
| 45 | + |
| 46 | +final readonly class DateRangeData extends Data |
| 47 | +{ |
| 48 | + public function __construct( |
| 49 | + #[WithRefiner(DateTimeRefiner::class, formats: 'Y/m-d')] |
| 50 | + public ?DateTimeImmutable $start, |
| 51 | + #[WithRefiner(DateTimeRefiner::class, formats: 'Y/m-d')] |
| 52 | + public ?DateTimeImmutable $end |
| 53 | + ) {} |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +With this, hydrating the DTO will be possible. |
| 58 | + |
| 59 | +Creating a Custom Data Refiner |
| 60 | += |
| 61 | + |
| 62 | +To create a custom Data Refiner, you need to implement the `DataRefiner` interface. for example suppose we |
| 63 | +want to create a Data Refiner that will refine an object of class `CustomDate`: |
| 64 | + |
| 65 | +```php |
| 66 | +class CustomDate { |
| 67 | + public function __construct( |
| 68 | + private(set) int $year, |
| 69 | + private(set) int $month, |
| 70 | + private(set) int $day |
| 71 | + ) {} |
| 72 | + |
| 73 | + // ... |
| 74 | +} |
| 75 | +``` |
| 76 | + |
| 77 | +We can add the ability to hydrate a `DateTime` property from this class using a custom refiner like so: |
| 78 | + |
| 79 | +```php |
| 80 | +use Nuxtifyts\PhpDto\Contexts\PropertyContext; |
| 81 | +use Nuxtifyts\PhpDto\DataRefiners\DataRefiner; |
| 82 | + |
| 83 | + |
| 84 | +class CustomDateRefiner implements DataRefiner |
| 85 | +{ |
| 86 | + public function refine(mixed $value, PropertyContext $property) : mixed |
| 87 | + { |
| 88 | + if ($value instanceof CustomDate) { |
| 89 | + return DateTimeImmutable::createFromFormat( |
| 90 | + format: 'Y-m-d', |
| 91 | + datetime: sprintf('%d-%d-%d', $value->year, $value->month, $value->day) |
| 92 | + ); |
| 93 | + } |
| 94 | + |
| 95 | + return $value; |
| 96 | + } |
| 97 | +} |
| 98 | +``` |
| 99 | + |
| 100 | +Now we can use this refiner in our previous DTO: |
| 101 | + |
| 102 | +```php |
| 103 | +use Nuxtifyts\PhpDto\Data; |
| 104 | +use DateTimeImmutable; |
| 105 | +use Nuxtifyts\PhpDto\Attributes\Property\WithRefiner; |
| 106 | +use Nuxtifyts\PhpDto\DataRefiners\DateTimeRefiner; |
| 107 | + |
| 108 | +final readonly class DateRangeData extends Data |
| 109 | +{ |
| 110 | + public function __construct( |
| 111 | + #[WithRefiner(DateTimeRefiner::class, formats: 'Y/m-d')] |
| 112 | + #[WithRefiner(CustomDateRefiner::class)] |
| 113 | + public ?DateTimeImmutable $start, |
| 114 | + #[WithRefiner(CustomDateRefiner::class)] |
| 115 | + #[WithRefiner(DateTimeRefiner::class, formats: 'Y/m-d')] |
| 116 | + public ?DateTimeImmutable $end |
| 117 | + ) {} |
| 118 | +} |
| 119 | +``` |
0 commit comments