|
| 1 | +# Basic Usage |
| 2 | + |
| 3 | +## Creating a Value Object |
| 4 | + |
| 5 | +To create a new Value Object, extend the `Bag\Bag` class and define your properties in the constructor: |
| 6 | + |
| 7 | +```php |
| 8 | +use Bag\Bag; |
| 9 | + |
| 10 | +readonly class MyValue extends Bag { |
| 11 | + public function __construct( |
| 12 | + public string $name, |
| 13 | + public int $age, |
| 14 | + ) { |
| 15 | + } |
| 16 | +} |
| 17 | +``` |
| 18 | + |
| 19 | +> [!TIP] |
| 20 | +> You can add an `@method` annotation to your class to provide auto-complete for the `::from()` method, or use the [Artisan Command with the --doc option](laravel-artisan-make-bag-command.md#updating-documentation) to generate it for you. |
| 21 | +
|
| 22 | + |
| 23 | +## Instantiating a Value Object |
| 24 | + |
| 25 | +To create a new instance of your Value Object, call the `::from()` method. You can use an array, a Collection, named arguments, or positional arguments. |
| 26 | + |
| 27 | + |
| 28 | +### Named Arguments |
| 29 | + |
| 30 | +```php |
| 31 | +$value = MyValue::from( |
| 32 | + name: 'Davey Shafik', |
| 33 | + age: => 40, |
| 34 | +); |
| 35 | +``` |
| 36 | + |
| 37 | +### Array or Collection of Arguments |
| 38 | + |
| 39 | +```php |
| 40 | +$value = MyValue::from([ |
| 41 | + 'name' => 'Davey Shafik', |
| 42 | + 'age' => 40, |
| 43 | +]); |
| 44 | +``` |
| 45 | + |
| 46 | +or: |
| 47 | + |
| 48 | +```php |
| 49 | +$value = MyValue::from(collect([ |
| 50 | + 'name' => 'Davey Shafik', |
| 51 | + 'age' => 40, |
| 52 | +])); |
| 53 | +``` |
| 54 | + |
| 55 | +### Positional Arguments |
| 56 | + |
| 57 | +```php |
| 58 | +$value = MyValue::from('Davey Shafik', 40); |
| 59 | +``` |
| 60 | + |
| 61 | +> [!WARNING] |
| 62 | +> If you use positional arguments, you must ensure they are in the same order as the constructor. |
| 63 | +
|
| 64 | +> [!WARNING] |
| 65 | +> If you have a single array argument, **and** an array [transformer](./transformers), the transformer will be applied to the array, potentially causing unwanted side-effects. |
| 66 | +
|
| 67 | +## Type Casting |
| 68 | + |
| 69 | +If the input value matches the type of the property (including [union types](https://www.php.net/manual/en/language.types.type-system.php#language.types.type-system.composite.union)), it will be used as-is. Otherwise, Bag will cast all values to their defined type _automatically_ for all scalar types, as well as the following: |
| 70 | + |
| 71 | +- `Bag` objects |
| 72 | +- `\Bag\Collection` and `\Illuminate\Support\Collection` objects |
| 73 | +- `\DateTimeInterface` objects will be cast using standard [PHP Date/Time formats](https://www.php.net/manual/en/datetime.formats.php) |
| 74 | + - This includes `\DateTime`, `\DateTimeImmutable`, `\Carbon\Carbon` and `\Carbon\CarbonImmutable` |
| 75 | +- Enums |
| 76 | + |
| 77 | +> [!TIP] |
| 78 | +> We recommend using `\Carbon\CarbonImmutable` for all date times. |
| 79 | +
|
| 80 | +## Default Values |
| 81 | + |
| 82 | +You can define default values for your properties by setting them in the constructor: |
| 83 | + |
| 84 | +```php |
| 85 | +use Bag\Bag; |
| 86 | + |
| 87 | +readonly class MyValue extends Bag { |
| 88 | + public function __construct( |
| 89 | + public string $name, |
| 90 | + public int $age = 40, |
| 91 | + ) { |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +$value = MyValue::from([ |
| 96 | + 'name' => 'Davey Shafik', |
| 97 | +])->toArray(); // ['name' => 'Davey Shafik', 'age' => 40] |
| 98 | +``` |
| 99 | + |
| 100 | +## Nullable Values |
| 101 | + |
| 102 | +Bag will fill missing nullable values without a default value with `null`: |
| 103 | + |
| 104 | +```php |
| 105 | +use Bag\Bag; |
| 106 | + |
| 107 | +readonly class MyValue extends Bag { |
| 108 | + public function __construct( |
| 109 | + public string $name, |
| 110 | + public ?int $age, |
| 111 | + ) { |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +$value = MyValue::from([ |
| 116 | + 'name' => 'Davey Shafik', |
| 117 | +])->toArray(); // ['name' => 'Davey Shafik', 'age' => null] |
| 118 | +``` |
| 119 | + |
| 120 | +## Stripping Extra Parameters |
| 121 | + |
| 122 | +By default, Bag will throw a `\Bag\Exceptions\AdditionalPropertiesException` exception if you try to instantiate a non-variadic Bag with extra parameters. You can disable this behavior by adding the `StripExtraParameters` attribute to the class: |
| 123 | + |
| 124 | +```php |
| 125 | +use Bag\Attributes\StripExtraParameters; |
| 126 | +use Bag\Bag; |
| 127 | + |
| 128 | +#[StripExtraParameters] |
| 129 | +readonly class MyValue extends Bag { |
| 130 | + public function __construct( |
| 131 | + public string $name, |
| 132 | + public ?int $age, |
| 133 | + ) { |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +$value = MyValue::from([ |
| 138 | + 'name' => 'Davey Shafik', |
| 139 | + 'test' => true, |
| 140 | +])->toArray(); // [ 'name' => 'Davey Shafik', 'age' => null ] (note that 'test' is stripped) |
| 141 | +``` |
| 142 | + |
| 143 | +> [!TIP] |
| 144 | +> You can also use the `StripExtraParameters` attribute when [injecting a Bag object into a controller](./laravel-controller-injection.md#avoiding-extra-parameters). |
| 145 | +
|
| 146 | +### Modifying a Value Object |
| 147 | + |
| 148 | +Value Objects are immutable, so you cannot change their properties directly. Instead, you can create a new instance with the updated values using the `Bag->with()` or `Bag->append()` methods: |
| 149 | + |
| 150 | +```php |
| 151 | +$value = MyValue::from([ |
| 152 | + 'name' => 'Davey Shafik', |
| 153 | + 'age' => 40, |
| 154 | +]); |
| 155 | + |
| 156 | +$newValue = $value->with(age: 41); |
| 157 | + |
| 158 | +dump($newValue->toArray()); // ['name' => 'Davey Shafik', 'age' => 41] |
| 159 | +``` |
| 160 | + |
| 161 | +You can pass either named arguments, or an array or Collection of key-value pairs to the `Bag->with()` method. |
| 162 | + |
| 163 | +> [!TIP] |
| 164 | +> The `Bag->append()` method works the same way as `Bag->with()`, but it will not validate the new value object. You can manually validate the object using `Bag->valid()`. |
0 commit comments