|
| 1 | +Default Values |
| 2 | += |
| 3 | + |
| 4 | +Sometimes, we may need to specify that a property has a default value, |
| 5 | +we can achieve that using plain PHP for some property types but not all of them. |
| 6 | + |
| 7 | +```php |
| 8 | +use Nuxtifyts\PhpDto\Data; |
| 9 | + |
| 10 | +final readonly class User extends Data |
| 11 | +{ |
| 12 | + public function __construct( |
| 13 | + public string $firstName, |
| 14 | + public string $lastName, |
| 15 | + public string $email, |
| 16 | + public UserType $type = UserType::DEFAULT, |
| 17 | + public UserConfigData $config, |
| 18 | + ) {} |
| 19 | +} |
| 20 | +``` |
| 21 | + |
| 22 | +On the other hand, if we want to specify, for example, a default value for UserType depending |
| 23 | +on the provided email address, or if you want to provide a default value for complex data such as |
| 24 | +`UserConfigData` which is another DTO, there is no way to do it using plain PHP, |
| 25 | +that's where `DefaultsTo` attribute comes in. |
| 26 | + |
| 27 | +```php |
| 28 | +use Nuxtifyts\PhpDto\Data; |
| 29 | +use Nuxtifyts\PhpDto\Attributes\Property\DefaultsTo; |
| 30 | + |
| 31 | +final readonly class User extends Data |
| 32 | +{ |
| 33 | + public function __construct( |
| 34 | + public string $firstName, |
| 35 | + public string $lastName, |
| 36 | + public string $email, |
| 37 | + #[DefaultsTo(UserType::DEFAULT)] |
| 38 | + public UserType $type, |
| 39 | + #[DefaultsTo(UserConfigDataFallbackResolver::class)] |
| 40 | + public UserConfigData $config, |
| 41 | + ) {} |
| 42 | +} |
| 43 | +``` |
| 44 | + |
| 45 | +The `DefaultsTo` attribute provides the ability to specify default values for complex types, |
| 46 | +such as DateTimes and DTOs. |
| 47 | + |
| 48 | +For more details checkout the [DefaultValues](https://github.com/nuxtifyts/php-dto/blob/main/docs/DefaultValues.md) |
| 49 | +guide. |
| 50 | + |
| 51 | +In this example, the `UserConfigDataFallbackResolver` would look like this: |
| 52 | + |
| 53 | +```php |
| 54 | +use Nuxtifyts\PhpDto\Contexts\PropertyContext; |
| 55 | +use Nuxtifyts\PhpDto\FallbackResolver\FallbackResolver; |
| 56 | + |
| 57 | +class UserConfigDataFallbackResolver implements FallbackResolver |
| 58 | +{ |
| 59 | + /** |
| 60 | + * @param array<string, mixed> $rawData |
| 61 | + */ |
| 62 | + public static function resolve(array $rawData, PropertyContext $property) : mixed{ |
| 63 | + $email = $rawData['email'] ?? null; |
| 64 | + |
| 65 | + return match(true) { |
| 66 | + str_contains($email, 'example.com') => new UserConfigData(/** Admin configs */), |
| 67 | + default => new UserConfigData(/** User configs */) |
| 68 | + } |
| 69 | + } |
| 70 | +} |
| 71 | +``` |
| 72 | + |
| 73 | +>! When using `DefaultsTo` attribute, priority is given to the attribute instead of the parameter's default value. |
| 74 | +
|
| 75 | +If ever needed to create a new instance of a DTO with complex default value, |
| 76 | +using the constructor is no longer possible, instead, you can make use of the |
| 77 | +`create` function provided by the DTO class. |
| 78 | + |
| 79 | +Using the same example above, we can create a new instance of `User` with the default value for `config`: |
| 80 | + |
| 81 | +```php |
| 82 | +$user = User::create( |
| 83 | + firstName: 'John', |
| 84 | + lastName: 'Doe', |
| 85 | + |
| 86 | +); |
| 87 | +``` |
0 commit comments