Skip to content

Commit 1b01160

Browse files
Merge pull request #714 from erikaraujo/no-cannotsetcomputedvalue-exception
Adds a config option to silently ignore when a Computed Property is being set
2 parents 20aa4aa + dc823f6 commit 1b01160

File tree

4 files changed

+25
-1
lines changed

4 files changed

+25
-1
lines changed

config/data.php

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,13 @@
1717
*/
1818
'features' => [
1919
'cast_and_transform_iterables' => false,
20+
21+
/**
22+
* When trying to set a computed property value, the package will throw an exception.
23+
* You can disable this behaviour by setting this option to true, which will then just
24+
* ignore the value being passed into the computed property and recalculate it.
25+
*/
26+
'ignore_exception_when_trying_to_set_computed_property_value' => false,
2027
],
2128

2229
/**

docs/as-a-data-transfer-object/computed.md

+1
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,5 @@ Again there are a few conditions for this approach:
3434

3535
- You must always use a sole property, a property within the constructor definition won't work
3636
- Computed properties cannot be defined in the payload, a `CannotSetComputedValue` will be thrown if this is the case
37+
- 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.
3738

src/Resolvers/DataFromArrayResolver.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,11 @@ public function execute(string $class, array $properties): BaseData
5454
}
5555

5656
if ($property->computed) {
57-
throw CannotSetComputedValue::create($property);
57+
if (! config('data.features.ignore_exception_when_trying_to_set_computed_property_value')) {
58+
throw CannotSetComputedValue::create($property);
59+
}
60+
61+
continue; // Ignore the value being passed into the computed property and let it be recalculated
5862
}
5963

6064
$data->{$property->name} = $properties[$property->name];

tests/CreationTest.php

+12
Original file line numberDiff line numberDiff line change
@@ -657,6 +657,18 @@ public function __construct(
657657
}
658658
};
659659

660+
expect($dataObject::from(['first_name' => 'Ruben', 'last_name' => 'Van Assche', 'full_name' => 'Something to Be Ignored']))
661+
->first_name->toBe('Ruben')
662+
->last_name->toBe('Van Assche')
663+
->full_name->toBe('Ruben Van Assche');
664+
665+
expect($dataObject::validateAndCreate(['first_name' => 'Ruben', 'last_name' => 'Van Assche', 'full_name' => 'Something to Be Ignored']))
666+
->first_name->toBe('Ruben')
667+
->last_name->toBe('Van Assche')
668+
->full_name->toBe('Ruben Van Assche');
669+
670+
config()->set('data.features.ignore_exception_when_trying_to_set_computed_property_value', false);
671+
660672
expect($dataObject::from(['first_name' => 'Ruben', 'last_name' => 'Van Assche']))
661673
->first_name->toBe('Ruben')
662674
->last_name->toBe('Van Assche')

0 commit comments

Comments
 (0)