Skip to content

Commit

Permalink
Merge pull request #714 from erikaraujo/no-cannotsetcomputedvalue-exc…
Browse files Browse the repository at this point in the history
…eption

Adds a config option to silently ignore when a Computed Property is being set
  • Loading branch information
rubenvanassche authored Apr 4, 2024
2 parents 20aa4aa + dc823f6 commit 1b01160
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 1 deletion.
7 changes: 7 additions & 0 deletions config/data.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,13 @@
*/
'features' => [
'cast_and_transform_iterables' => false,

/**
* When trying to set a computed property value, the package will throw an exception.
* You can disable this behaviour by setting this option to true, which will then just
* ignore the value being passed into the computed property and recalculate it.
*/
'ignore_exception_when_trying_to_set_computed_property_value' => false,
],

/**
Expand Down
1 change: 1 addition & 0 deletions docs/as-a-data-transfer-object/computed.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,5 @@ Again there are a few conditions for this approach:

- You must always use a sole property, a property within the constructor definition won't work
- Computed properties cannot be defined in the payload, a `CannotSetComputedValue` will be thrown if this is the case
- If the `ignore_exception_when_trying_to_set_computed_property_value` configuration option is set to `true`, the computed property will be silently ignored when trying to set it in the payload and no `CannotSetComputedValue` exception will be thrown.

6 changes: 5 additions & 1 deletion src/Resolvers/DataFromArrayResolver.php
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,11 @@ public function execute(string $class, array $properties): BaseData
}

if ($property->computed) {
throw CannotSetComputedValue::create($property);
if (! config('data.features.ignore_exception_when_trying_to_set_computed_property_value')) {
throw CannotSetComputedValue::create($property);
}

continue; // Ignore the value being passed into the computed property and let it be recalculated
}

$data->{$property->name} = $properties[$property->name];
Expand Down
12 changes: 12 additions & 0 deletions tests/CreationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -657,6 +657,18 @@ public function __construct(
}
};

expect($dataObject::from(['first_name' => 'Ruben', 'last_name' => 'Van Assche', 'full_name' => 'Something to Be Ignored']))
->first_name->toBe('Ruben')
->last_name->toBe('Van Assche')
->full_name->toBe('Ruben Van Assche');

expect($dataObject::validateAndCreate(['first_name' => 'Ruben', 'last_name' => 'Van Assche', 'full_name' => 'Something to Be Ignored']))
->first_name->toBe('Ruben')
->last_name->toBe('Van Assche')
->full_name->toBe('Ruben Van Assche');

config()->set('data.features.ignore_exception_when_trying_to_set_computed_property_value', false);

expect($dataObject::from(['first_name' => 'Ruben', 'last_name' => 'Van Assche']))
->first_name->toBe('Ruben')
->last_name->toBe('Van Assche')
Expand Down

0 comments on commit 1b01160

Please sign in to comment.