File tree Expand file tree Collapse file tree 2 files changed +18
-1
lines changed
Expand file tree Collapse file tree 2 files changed +18
-1
lines changed Original file line number Diff line number Diff line change 1212use Ltaooo \Data \Traits \ArrayAccessTrait ;
1313use Ltaooo \Data \Util \Str ;
1414use ReflectionClass ;
15+ use ReflectionIntersectionType ;
1516use ReflectionProperty ;
17+ use ReflectionUnionType ;
1618
1719class Data implements ArrayAble, ArrayAccess, JsonSerializable
1820{
@@ -67,7 +69,9 @@ public function fill(array $data): static
6769 }
6870 $ type = $ property ->getType ();
6971 $ value = $ data [$ camelCasePropertyName ] ?? ($ data [$ snakePropertyName ] ?? null );
70- if ($ type ->isBuiltin () && !is_null ($ value )) {
72+ if ($ type instanceof ReflectionUnionType || $ type instanceof ReflectionIntersectionType) {
73+ $ property ->setValue ($ this , $ value );
74+ } elseif ($ type ->isBuiltin () && !is_null ($ value )) {
7175 $ property ->setValue ($ this , $ value );
7276 } elseif (PHP_VERSION_ID > 80100 && enum_exists ($ type ->getName ())) {
7377 $ property ->setValue ($ this , $ value );
Original file line number Diff line number Diff line change 3232});
3333
3434test ('str ' , function () {
35+ $ value = ['name ' => 'hello ' ];
36+ $ d = D::from ($ value );
37+ expect ($ d )->toBeInstanceOf (Data::class)
38+ ->and ($ d ->name )
39+ ->toEqual ($ value ['name ' ]);
40+ });
41+
42+
43+ test ('union type ' , function () {
3544 expect (Str::snake ('HelloWorld ' ))->toBe ('hello_world ' )
3645 ->and (Str::camel ('hello_world ' ))->toBe ('helloWorld ' )
3746 ->and (Str::camel ('' ))->toBe ('' )
@@ -66,4 +75,8 @@ class C extends Data
6675enum enumA: string
6776{
6877 case A = 'a ' ;
78+ }
79+
80+ class D extends Data {
81+ public string |int $ name ;
6982}
You can’t perform that action at this time.
0 commit comments