@@ -46,12 +46,12 @@ public function map(DataType|string $type, array|string $data): ?object
46
46
return $ class ::from ($ data );
47
47
}
48
48
49
- $ functionName = self ::MAPPER_FUNCTION_PREFIX . \md5 ($ class );
49
+ $ functionName = self ::MAPPER_FUNCTION_PREFIX . \md5 ($ class . ( $ type instanceof DataType && $ type -> isNullable ? ' 1 ' : ' 0 ' ) );
50
50
if ($ this ->mapper ->config ->classCacheKeySource === 'md5 ' || $ this ->mapper ->config ->classCacheKeySource === 'modified ' ) {
51
51
$ reflection = new ReflectionClass ($ class );
52
52
$ functionName = match ($ this ->mapper ->config ->classCacheKeySource ) {
53
- 'md5 ' => self ::MAPPER_FUNCTION_PREFIX . \md5_file ($ reflection ->getFileName ()),
54
- 'modified ' => self ::MAPPER_FUNCTION_PREFIX . \md5 (\filemtime ($ reflection ->getFileName ())),
53
+ 'md5 ' => self ::MAPPER_FUNCTION_PREFIX . \md5 ( \ md5_file ($ reflection ->getFileName ()) . $ functionName ),
54
+ 'modified ' => self ::MAPPER_FUNCTION_PREFIX . \md5 (\filemtime ($ reflection ->getFileName ()) . $ functionName ),
55
55
};
56
56
}
57
57
@@ -62,6 +62,7 @@ public function map(DataType|string $type, array|string $data): ?object
62
62
$ this ->createObjectMappingFunction (
63
63
$ this ->classBluePrinter ->print ($ class ),
64
64
$ functionName ,
65
+ $ type instanceof DataType && $ type ->isNullable ,
65
66
),
66
67
);
67
68
}
@@ -88,14 +89,24 @@ public function mapperDirectory(): string
88
89
return \rtrim ($ dir , \DIRECTORY_SEPARATOR );
89
90
}
90
91
91
- private function createObjectMappingFunction (ClassBluePrint $ blueprint , string $ mapFunctionName ): string
92
+ private function createObjectMappingFunction (ClassBluePrint $ blueprint , string $ mapFunctionName, bool $ isNullable ): string
92
93
{
93
94
$ tab = ' ' ;
95
+ $ content = '' ;
96
+
97
+ if ($ isNullable ) {
98
+ $ content .= $ tab . $ tab . 'if ($data === [] && $mapper->config->nullObjectFromEmptyArray) { ' . \PHP_EOL ;
99
+ $ content .= $ tab . $ tab . $ tab . 'return null; ' . \PHP_EOL ;
100
+ $ content .= $ tab . $ tab . '} ' . \PHP_EOL . \PHP_EOL ;
101
+ }
94
102
95
103
// Instantiate a new object
96
104
$ args = [];
97
105
foreach ($ blueprint ->constructorArguments as $ name => $ argument ) {
98
106
$ arg = "\$data[' {$ name }'] " ;
107
+ if ($ argument ['type ' ]->isNullable ()) {
108
+ $ arg = "( {$ arg } ?? null) " ;
109
+ }
99
110
100
111
if ($ argument ['type ' ] !== null ) {
101
112
$ arg = $ this ->castInMapperFunction ($ arg , $ argument ['type ' ], $ blueprint );
@@ -107,7 +118,7 @@ private function createObjectMappingFunction(ClassBluePrint $blueprint, string $
107
118
108
119
$ args [] = $ arg ;
109
120
}
110
- $ content = '$x = new ' . $ blueprint ->namespacedClassName . '( ' . \implode (', ' , $ args ) . '); ' ;
121
+ $ content .= $ tab . $ tab . '$x = new ' . $ blueprint ->namespacedClassName . '( ' . \implode (', ' , $ args ) . '); ' ;
111
122
112
123
// Map properties
113
124
foreach ($ blueprint ->properties as $ name => $ property ) {
@@ -118,7 +129,12 @@ private function createObjectMappingFunction(ClassBluePrint $blueprint, string $
118
129
continue ;
119
130
}
120
131
121
- $ propertyMap = $ this ->castInMapperFunction ("\$data[' {$ name }'] " , $ property ['type ' ], $ blueprint );
132
+ $ propertyName = "\$data[' {$ name }'] " ;
133
+ if ($ property ['type ' ]->isNullable ()) {
134
+ $ propertyName = "( {$ propertyName } ?? null) " ;
135
+ }
136
+
137
+ $ propertyMap = $ this ->castInMapperFunction ($ propertyName , $ property ['type ' ], $ blueprint );
122
138
if (\array_key_exists ('default ' , $ property )) {
123
139
$ propertyMap = $ this ->wrapDefault ($ propertyMap , $ name , $ property ['default ' ]);
124
140
}
@@ -151,7 +167,7 @@ private function createObjectMappingFunction(ClassBluePrint $blueprint, string $
151
167
if (! \\function_exists(' {$ mapFunctionName }')) {
152
168
function {$ mapFunctionName }( {$ mapperClass } \$mapper, array \$data)
153
169
{
154
- {$ content }
170
+ {$ content }
155
171
156
172
return \$x;
157
173
}
0 commit comments