File tree Expand file tree Collapse file tree 2 files changed +23
-12
lines changed
src/FreeElephants/JsonApi/DTO Expand file tree Collapse file tree 2 files changed +23
-12
lines changed Original file line number Diff line number Diff line change @@ -6,6 +6,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66
77## [ Unreleased]
88
9+ ## Added
10+ - BaseKeyValueStructure::assignFieldValue() method for inject custom field instantiation logic.
11+
912## [ 0.0.7] - 2025-06-26
1013
1114### Fixed
Original file line number Diff line number Diff line change @@ -6,21 +6,29 @@ class BaseKeyValueStructure
66{
77 public function __construct (array $ attributes )
88 {
9- $ concreteClass = new \ReflectionClass ($ this );
109 foreach ($ attributes as $ name => $ value ) {
11- $ property = $ concreteClass ->getProperty ($ name );
12- if ($ property ->hasType ()) {
13- $ propertyType = $ property ->getType ();
14- if ($ propertyType instanceof \ReflectionNamedType && !$ propertyType ->isBuiltin ()) {
15- if ($ propertyType ->allowsNull () && is_null ($ value )) {
16- $ value = null ;
17- } else {
18- $ propertyClassName = $ propertyType ->getName ();
19- $ value = new $ propertyClassName ($ value );
20- }
10+ $ this ->assignFieldValue ($ name , $ value );
11+ }
12+ }
13+
14+ protected function assignFieldValue (string $ name , $ value ): self
15+ {
16+ $ concreteClass = new \ReflectionClass ($ this );
17+ $ property = $ concreteClass ->getProperty ($ name );
18+ if ($ property ->hasType ()) {
19+ $ propertyType = $ property ->getType ();
20+ if ($ propertyType instanceof \ReflectionNamedType && !$ propertyType ->isBuiltin ()) {
21+ if ($ propertyType ->allowsNull () && is_null ($ value )) {
22+ $ value = null ;
23+ } else {
24+ $ propertyClassName = $ propertyType ->getName ();
25+ $ value = new $ propertyClassName ($ value );
2126 }
2227 }
23- $ this ->{$ name } = $ value ;
2428 }
29+
30+ $ this ->$ name = $ value ;
31+
32+ return $ this ;
2533 }
2634}
You can’t perform that action at this time.
0 commit comments