|
| 1 | +Normalizers |
| 2 | += |
| 3 | + |
| 4 | +When trying to hydrate a data object using the `from` function, the package will use normalizers to convert the passed |
| 5 | +data into (preferable) an `array`/`ArrayAccess`. |
| 6 | + |
| 7 | +By default, there are 4 normalizers: |
| 8 | + |
| 9 | +- **JsonStringNormalizer** will cast json string. |
| 10 | +- **StrClassNormalizer** will cast stdObject. |
| 11 | +- **ArrayAccessNormalizer** will cast ArrayAccess. |
| 12 | +- **ArrayNormalizer** will cast array. |
| 13 | + |
| 14 | +Custom normalizers: |
| 15 | += |
| 16 | + |
| 17 | +Custom normalizers can be added if needed: To do this, you will need to extend the `Normalizer` class |
| 18 | +and implement the `normalize` method. |
| 19 | + |
| 20 | +For example: we have a custom class called: `Goal`: |
| 21 | + |
| 22 | +```php |
| 23 | +class Goal |
| 24 | +{ |
| 25 | + public function __construct( |
| 26 | + public string $summary, |
| 27 | + public string $description, |
| 28 | + public DateTimeImmutable $dueDate |
| 29 | + ) { |
| 30 | + } |
| 31 | +} |
| 32 | +``` |
| 33 | + |
| 34 | +And we have our `Todo` data class: |
| 35 | + |
| 36 | +```php |
| 37 | +use Nuxtifyts\PhpDto\Data; |
| 38 | + |
| 39 | +final readonly class Todo extends Data |
| 40 | +{ |
| 41 | + public function __construct( |
| 42 | + public string $title, |
| 43 | + public string $content, |
| 44 | + public Status $status, |
| 45 | + public ?DateTimeImmutable $dueDate |
| 46 | + ) { |
| 47 | + } |
| 48 | +} |
| 49 | +``` |
| 50 | + |
| 51 | +And we want to hydrate instances of this class to using a normalizer. first, we need to create |
| 52 | +a normalizer class, we'll call it `GoalTodoNormalizer`: |
| 53 | + |
| 54 | +```php |
| 55 | +use Nuxtifyts\PhpDto\Normalizers\Normalizer; |
| 56 | + |
| 57 | +final readonly class GoalTodoNormalizer extends Normalizer |
| 58 | +{ |
| 59 | + public function normalize() : array|false{ |
| 60 | + if (!$this->value instanceof Goal) { |
| 61 | + return false; |
| 62 | + } |
| 63 | + |
| 64 | + return [ |
| 65 | + 'title' => $this->value->summary, |
| 66 | + 'content' => $this->value->description, |
| 67 | + 'status' => 'ready', |
| 68 | + 'dueDate' => $this->value->dueDate->format(DateTimeInterface::ATOM) |
| 69 | + ]; |
| 70 | + } |
| 71 | +} |
| 72 | +``` |
| 73 | + |
| 74 | +Next step is to add this new normalizer to the todo class: |
| 75 | + |
| 76 | +```php |
| 77 | +use Nuxtifyts\PhpDto\Data; |
| 78 | + |
| 79 | +final readonly class Todo extends Data |
| 80 | +{ |
| 81 | + // ... |
| 82 | + |
| 83 | + protected static function normalizers() : array{ |
| 84 | + return GoalTodoNormalizer::class; |
| 85 | + } |
| 86 | +} |
| 87 | +``` |
| 88 | + |
| 89 | +This will add the `GoalTodoNormalizer` on top of the default normalizers. Now it'll be possible to hydrate |
| 90 | +a `Todo` instance from a `Goal` instance. |
| 91 | + |
| 92 | +```php |
| 93 | +$goal = new Goal( |
| 94 | + title: 'Learn PHP DTO', |
| 95 | + description: 'Learn how to use PHP DTO', |
| 96 | + dueDate: new DateTimeImmutable() |
| 97 | +); |
| 98 | + |
| 99 | +$todo = Todo::from($goal); |
| 100 | +``` |
0 commit comments