@@ -72,7 +72,9 @@ protected function decorateClass(ClassType $class): void
72
72
{
73
73
$ constants = [];
74
74
75
- $ denormalizeBody = "\$dataToPopulate = \$this->convertDataToInternalFormat( \$data); \n" ;
75
+ $ denormalizeBody = "if (!is_array( \$data)) { \n" ;
76
+ $ denormalizeBody .= " throw new InvalidArgumentException('Bad data parameter. Array instance is required'); \n" ;
77
+ $ denormalizeBody .= "} \n\n" ;
76
78
$ denormalizeBody .= '$type = trim($type, " \t\n\r\0\x0B \\\\/"); ' . "\n\n" ;
77
79
$ denormalizeBody .= "\$entity = \$context[AbstractNormalizer::OBJECT_TO_POPULATE] ?? new \$type(); \n\n" ;
78
80
$ denormalizeBody .= "if (!( \$entity instanceof Model)) { \n" ;
@@ -85,13 +87,12 @@ protected function decorateClass(ClassType $class): void
85
87
$ className = $ this ->unifyClassName ($ descriptor ->getName ());
86
88
$ constants [] = new Literal ("{$ className }::class => true " );
87
89
$ denormalizeBody .= " case {$ className }::class: \n" ;
88
- $ denormalizeBody .= " \$extractedData = \$this->model {$ className }DataExtractor( \$dataToPopulate ); \n" ;
90
+ $ denormalizeBody .= " \$extractedData = \$this->model {$ className }DataExtractor( \$data ); \n" ;
89
91
$ denormalizeBody .= " break; \n" ;
90
92
}
91
93
92
94
$ denormalizeBody .= " default: \n" ;
93
95
$ denormalizeBody .= " throw new InvalidArgumentException( \"Can't find data extractor for '{ \$type}' type \"); \n" ;
94
- $ denormalizeBody .= " break; \n" ;
95
96
$ denormalizeBody .= "} \n\n" ;
96
97
$ denormalizeBody .= "\$entity->setRawAttributes( \$extractedData); \n" ;
97
98
$ denormalizeBody .= "\n" ;
@@ -128,25 +129,6 @@ protected function decorateClass(ClassType $class): void
128
129
->setBody ('return FiasSerializerFormat::XML->isEqual($format) ? self::ALLOWED_ENTITIES : []; ' );
129
130
$ getSupportedTypes ->addParameter ('format ' )->setType ('string ' )->setNullable (true );
130
131
131
- $ convertDataToInternalFormatBody = <<<EOT
132
- \$result = [];
133
- if (!is_array( \$data)) {
134
- return \$result;
135
- }
136
-
137
- foreach ( \$data as \$key => \$value) {
138
- \$newKey = strtolower(trim((string) \$key, " \\n\\r\\t\\v\\x00@"));
139
- \$result[ \$newKey] = \$value;
140
- }
141
-
142
- return \$result;
143
- EOT ;
144
- $ convertDataToInternalFormat = $ class ->addMethod ('convertDataToInternalFormat ' )
145
- ->setVisibility ('private ' )
146
- ->setReturnType ('array ' )
147
- ->setBody ($ convertDataToInternalFormatBody );
148
- $ convertDataToInternalFormat ->addParameter ('data ' )->setType ('mixed ' )->setNullable (false );
149
-
150
132
foreach ($ descriptors as $ descriptor ) {
151
133
$ className = $ this ->unifyClassName ($ descriptor ->getName ());
152
134
$ entityMethod = $ class ->addMethod ("model {$ className }DataExtractor " );
@@ -164,19 +146,23 @@ protected function decorateModelDataExtractor(Method $method, EntityDescriptor $
164
146
$ body = "return [ \n" ;
165
147
foreach ($ descriptor ->getFields () as $ field ) {
166
148
$ column = $ this ->unifyColumnName ($ field ->getName ());
149
+ $ arrayParamName = '@ ' . strtoupper ($ field ->getName ());
167
150
$ type = trim ($ field ->getType () . '_ ' . $ field ->getSubType (), ' _ ' );
168
151
switch ($ type ) {
169
152
case 'int ' :
170
- $ varType = "(int) \$data[' {$ column }'] " ;
153
+ $ varType = "(int) \$data[' {$ arrayParamName }'] " ;
154
+ $ defaultValue = $ field ->isNullable () ? 'null ' : '0 ' ;
171
155
break ;
172
156
case 'string_date ' :
173
- $ varType = "new DateTimeImmutable((string) \$data[' {$ column }']) " ;
157
+ $ varType = "new DateTimeImmutable((string) \$data[' {$ arrayParamName }']) " ;
158
+ $ defaultValue = 'null ' ;
174
159
break ;
175
160
default :
176
- $ varType = "trim((string) \$data[' {$ column }']) " ;
161
+ $ varType = "trim((string) \$data[' {$ arrayParamName }']) " ;
162
+ $ defaultValue = $ field ->isNullable () ? 'null ' : "'' " ;
177
163
break ;
178
164
}
179
- $ body .= " ' {$ column }' => isset( \$data[' {$ column }']) ? {$ varType } : null , \n" ;
165
+ $ body .= " ' {$ column }' => isset( \$data[' {$ arrayParamName }']) ? {$ varType } : { $ defaultValue } , \n" ;
180
166
}
181
167
$ body .= ']; ' ;
182
168
0 commit comments