|
| 1 | +Cloneable Data |
| 2 | += |
| 3 | + |
| 4 | +Sometimes we may want to alter the data of a `Data` object (Partially or completely). |
| 5 | +And since `Data` objects are immutable by default, we can't change the data directly. |
| 6 | + |
| 7 | +To solve this, we can use the `with` function that will return a new instance of the `Data` object with the new data. |
| 8 | +Let take the `TodoData` class as an example: |
| 9 | + |
| 10 | +```php |
| 11 | +use Nuxtifyts\PhpDto\Data; |
| 12 | +use DateTimeImmutable; |
| 13 | + |
| 14 | +final readonly class TodoData extends Data |
| 15 | +{ |
| 16 | + public function __construct( |
| 17 | + public string $title, |
| 18 | + public string $content, |
| 19 | + public Status $status, |
| 20 | + public ?DateTimeImmutable $dueDate |
| 21 | + ) {} |
| 22 | +} |
| 23 | +``` |
| 24 | + |
| 25 | +The `Status` enum is defined as follows: |
| 26 | + |
| 27 | +```php |
| 28 | +enum Status: string |
| 29 | +{ |
| 30 | + case DEFAULT = 'default'; |
| 31 | + case IN_PROGRESS = 'in_progress'; |
| 32 | + case DONE = 'done'; |
| 33 | +} |
| 34 | +``` |
| 35 | + |
| 36 | +Using `with` function, we can easily create new instances of the `TodoData` class with the new data: |
| 37 | + |
| 38 | +```php |
| 39 | +$emptyTodo = Todo::empty(); |
| 40 | + |
| 41 | +// ... |
| 42 | + |
| 43 | +$todo = $emptyTodo->with( |
| 44 | + title: 'Learn PHP DTO', |
| 45 | + content: 'Learn how to use PHP DTO', |
| 46 | + status: Status::IN_PROGRESS |
| 47 | +); |
| 48 | + |
| 49 | +// ... |
| 50 | + |
| 51 | +$todoWithDueDate = $todo->with( |
| 52 | + dueDate: new DateTimeImmutable('2025-01-06') |
| 53 | +); |
| 54 | +``` |
| 55 | + |
| 56 | +> We are using the `empty` method |
| 57 | +> from [Empty Data](https://github.com/nuxtifyts/php-dto/blob/main/docs/EmptyData.md) |
| 58 | +> here |
| 59 | +
|
| 60 | +> `emptyTodo`, `todo` and `todoWithDueDate` are all different instances. |
| 61 | +
|
| 62 | +Computed properties |
| 63 | +- |
| 64 | + |
| 65 | +When cloning a `Data` object, computed properties are automatically updated with the new data. |
| 66 | + |
| 67 | +```php |
| 68 | +use Nuxtifyts\PhpDto\Data; |
| 69 | +use Nuxtifyts\PhpDto\Attributes\Property\Computed; |
| 70 | + |
| 71 | +final readonly class PersonData extends Data |
| 72 | +{ |
| 73 | + #[Computed] |
| 74 | + public string $fullName; |
| 75 | + |
| 76 | + public function __construct( |
| 77 | + public string $firstName, |
| 78 | + public string $lastName |
| 79 | + ) {} |
| 80 | +} |
| 81 | +``` |
| 82 | + |
| 83 | +For example: |
| 84 | + |
| 85 | +```php |
| 86 | +$johnDoe = new PersonData(firstName: 'John', lastName: 'Doe'); |
| 87 | + |
| 88 | +$janeDoe = $johnDoe->with(firstName: 'Jane'); |
| 89 | + |
| 90 | +$janeDoe->fullName; // 'Jane Doe' |
| 91 | +``` |
| 92 | + |
| 93 | +Normalizers |
| 94 | +- |
| 95 | + |
| 96 | +When cloning a `Data` object, normalizers that are typically used when hydrating a `Data` object |
| 97 | +using `from` method are also used. |
| 98 | + |
| 99 | +This will allow the ability to pass `json` data, `ArrayAccess` or `stdClass` objects for example to the `with` method. |
| 100 | +If a custom normalizer is implemented for the `Data` class, it can be used as well. |
| 101 | + |
| 102 | +```php |
| 103 | +$johnDoe = new PersonDaa('John', 'Doe'); |
| 104 | + |
| 105 | +$janeDoe = $johnDoe->with('{"firstName": "Jane"}'); |
| 106 | + |
| 107 | +$janeDoe->fullName; // 'Jane Doe' |
| 108 | +``` |
| 109 | + |
| 110 | +Using an `stdClass` object: |
| 111 | + |
| 112 | +```php |
| 113 | +$object = new stdClass(); |
| 114 | +$object->firstName = 'Jake'; |
| 115 | + |
| 116 | +$jakeDoe = $janeDoe->with($object); |
| 117 | + |
| 118 | +$jakeDoe->fullName; // 'Jake Doe' |
| 119 | +``` |
0 commit comments