Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds a config option to silently ignore when a Computed Property is being set #714

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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