@@ -23,6 +23,7 @@ class Data implements ArrayAble, ArrayAccess, JsonSerializable
2323 protected ?ReflectionClass $ _staticReflection = null ;
2424
2525 /**
26+ * @param mixed $data
2627 * @throws
2728 */
2829 public function __construct ($ data = [])
@@ -40,14 +41,23 @@ public function __debugInfo(): ?array
4041 return $ this ->objectToArray ($ this , false );
4142 }
4243
44+ public function __serialize (): array
45+ {
46+ $ data = [];
47+ foreach ($ this ->getStaticReflection ()->getProperties () as $ property ) {
48+ if (! $ this ->isInsideProperty ($ property )) {
49+ $ data [$ property ->getName ()] = $ property ->getValue ($ this );
50+ }
51+ }
52+ return $ data ;
53+ }
4354
4455 public static function from ($ data ): static
4556 {
4657 return new static ($ data );
4758 }
4859
4960 /**
50- * @param array $data
5161 * @return $this
5262 * @throws
5363 */
@@ -62,17 +72,17 @@ public function fill(array $data): static
6272 $ camelCasePropertyName = Str::camel ($ propertyName );
6373 $ snakePropertyName = Str::snake ($ propertyName );
6474 if (
65- !array_key_exists ($ camelCasePropertyName , $ data )
66- && !array_key_exists ($ snakePropertyName , $ data )
67- && !$ property ->isInitialized ($ this )
75+ ! array_key_exists ($ camelCasePropertyName , $ data )
76+ && ! array_key_exists ($ snakePropertyName , $ data )
77+ && ! $ property ->isInitialized ($ this )
6878 ) {
6979 throw new Exception ("Property {$ property ->getName ()} is not set in : " . get_class ($ this ));
7080 }
7181 $ type = $ property ->getType ();
7282 $ value = $ data [$ camelCasePropertyName ] ?? ($ data [$ snakePropertyName ] ?? null );
7383 if ($ type instanceof ReflectionUnionType || $ type instanceof ReflectionIntersectionType) {
7484 $ property ->setValue ($ this , $ value );
75- } elseif ($ type ->isBuiltin () && !is_null ($ value )) {
85+ } elseif ($ type ->isBuiltin () && ! is_null ($ value )) {
7686 $ property ->setValue ($ this , $ value );
7787 } elseif (PHP_VERSION_ID > 80100 && enum_exists ($ type ->getName ())) {
7888 if (is_int ($ value ) || is_string ($ value )) {
@@ -132,7 +142,7 @@ protected function propertyToArray(object $object, bool $toSnake, ReflectionProp
132142 protected function forValue (mixed $ value , bool $ toSnake )
133143 {
134144 if (is_array ($ value )) {
135- return array_map (fn ($ item ) => $ this ->forValue ($ item , $ toSnake ), $ value );
145+ return array_map (fn ($ item ) => $ this ->forValue ($ item , $ toSnake ), $ value );
136146 }
137147 if (is_object ($ value )) {
138148 if (PHP_VERSION_ID > 80100 && enum_exists (get_class ($ value ))) {
@@ -142,7 +152,6 @@ protected function forValue(mixed $value, bool $toSnake)
142152 return $ value ->toArray ();
143153 }
144154 return $ this ->objectToArray ($ value , $ toSnake );
145-
146155 }
147156 return $ value ;
148157 }
@@ -183,18 +192,5 @@ protected function beforeFill(array $data): array
183192 return $ data ;
184193 }
185194
186- protected function afterFill (array $ data )
187- {
188- }
189-
190- public function __serialize (): array
191- {
192- $ data = [];
193- foreach ($ this ->getStaticReflection ()->getProperties () as $ property ) {
194- if (!$ this ->isInsideProperty ($ property )) {
195- $ data [$ property ->getName ()] = $ property ->getValue ($ this );
196- }
197- }
198- return $ data ;
199- }
200- }
195+ protected function afterFill (array $ data ) {}
196+ }
0 commit comments