File tree Expand file tree Collapse file tree 6 files changed +76
-3
lines changed Expand file tree Collapse file tree 6 files changed +76
-3
lines changed Original file line number Diff line number Diff line change 17
17
}
18
18
],
19
19
"minimum-stability" : " dev" ,
20
- "version" : " 0.1 .0" ,
20
+ "version" : " 0.2 .0" ,
21
21
"autoload" : {
22
22
"psr-4" : {
23
23
"Shureban\\ LaravelObjectMapper\\ " : " src/"
29
29
}
30
30
},
31
31
"require" : {
32
- "php" : " ^8.0 "
32
+ "php" : " ^8.1 "
33
33
},
34
34
"prefer-stable" : true ,
35
35
"extra" : {
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Shureban \LaravelObjectMapper \Types \Custom ;
4
+
5
+ use BackedEnum ;
6
+ use Shureban \LaravelObjectMapper \Types \SimpleTypes \ObjectType ;
7
+
8
+ class EnumType extends ObjectType
9
+ {
10
+ private string $ enumNamespace ;
11
+
12
+ public function __construct (string $ enumNamespace )
13
+ {
14
+ $ this ->enumNamespace = $ enumNamespace ;
15
+ }
16
+
17
+ /**
18
+ * @param mixed $value
19
+ *
20
+ * @return BackedEnum
21
+ */
22
+ public function convert (mixed $ value ): BackedEnum
23
+ {
24
+ return call_user_func ([$ this ->enumNamespace , 'from ' ], $ value );
25
+ }
26
+ }
Original file line number Diff line number Diff line change 15
15
use Shureban \LaravelObjectMapper \Types \BoxTypes \CollectionType ;
16
16
use Shureban \LaravelObjectMapper \Types \BoxTypes \DateTimeType ;
17
17
use Shureban \LaravelObjectMapper \Types \Custom \CustomType ;
18
+ use Shureban \LaravelObjectMapper \Types \Custom \EnumType ;
18
19
use Shureban \LaravelObjectMapper \Types \SimpleTypes \ArrayType ;
19
20
use Shureban \LaravelObjectMapper \Types \SimpleTypes \BoolType ;
20
21
use Shureban \LaravelObjectMapper \Types \SimpleTypes \FloatType ;
@@ -68,13 +69,21 @@ public static function make(ReflectionProperty $property): Type
68
69
return $ boxType ;
69
70
}
70
71
72
+ if (enum_exists ($ type )) {
73
+ return new EnumType ($ type );
74
+ }
75
+
71
76
if (class_exists ($ type )) {
72
77
return new CustomType ((new ReflectionClass ($ type ))->newInstanceWithoutConstructor ());
73
78
}
74
79
75
80
$ classUses = new ClassExtraInformation ($ property ->getDeclaringClass ());
76
81
$ namespace = $ classUses ->getFullObjectUseNamespace ($ type );
77
82
83
+ if (enum_exists ($ namespace )) {
84
+ return new EnumType ($ namespace );
85
+ }
86
+
78
87
return new CustomType ((new ReflectionClass ($ namespace ))->newInstanceWithoutConstructor ());
79
88
}
80
89
}
Original file line number Diff line number Diff line change 17
17
18
18
class CustomTypeTest extends TestCase
19
19
{
20
- public function _test_StrictTypes ()
20
+ public function test_StrictTypes ()
21
21
{
22
22
$ simpleClass = new CustomTypeStrictSimpleClass ();
23
23
$ simpleClass ->int = 10 ;
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Shureban \LaravelObjectMapper \Tests \Structs ;
4
+
5
+ use Shureban \LaravelObjectMapper \ObjectMapper ;
6
+ use Shureban \LaravelObjectMapper \Tests \Unit \Structs \EnumTypeClass ;
7
+ use Shureban \LaravelObjectMapper \Tests \Unit \Structs \TestEnum ;
8
+ use Tests \TestCase ;
9
+
10
+ class EnumTypeTest extends TestCase
11
+ {
12
+ public function test_Enum ()
13
+ {
14
+ $ this ->assertEquals (TestEnum::Hearts, (new ObjectMapper (new EnumTypeClass ()))->mapFromJson ('{"strictEnum": "Hearts"} ' )->strictEnum );
15
+ $ this ->assertEquals (TestEnum::Hearts, (new ObjectMapper (new EnumTypeClass ()))->mapFromJson ('{"phpDocEnum": "Hearts"} ' )->phpDocEnum );
16
+ $ this ->assertEquals (TestEnum::Hearts, (new ObjectMapper (new EnumTypeClass ()))->mapFromJson ('{"bothEnum": "Hearts"} ' )->bothEnum );
17
+ }
18
+ }
Original file line number Diff line number Diff line change
1
+ <?php
2
+
3
+ namespace Shureban \LaravelObjectMapper \Tests \Unit \Structs ;
4
+
5
+ enum TestEnum: string
6
+ {
7
+ case Hearts = 'Hearts ' ;
8
+ case Diamonds = 'Diamonds ' ;
9
+ case Clubs = 'Clubs ' ;
10
+ case Spades = 'Spades ' ;
11
+ }
12
+
13
+ class EnumTypeClass
14
+ {
15
+ public TestEnum $ strictEnum ;
16
+ /** @var TestEnum */
17
+ public $ phpDocEnum ;
18
+ /** @var TestEnum */
19
+ public TestEnum $ bothEnum ;
20
+ }
You can’t perform that action at this time.
0 commit comments