1
1
<?php
2
+
2
3
/**
3
4
* This file is part of event-engine/php-data.
4
- * (c) 2018-2019 prooph software GmbH <contact@prooph.de>
5
+ * (c) 2018-2020 prooph software GmbH <contact@prooph.de>
5
6
*
6
7
* For the full copyright and license information, please view the LICENSE
7
8
* file that was distributed with this source code.
@@ -36,7 +37,7 @@ private function init(): void
36
37
* @param array $recordData
37
38
* @return self
38
39
*/
39
- public static function fromRecordData (array $ recordData )
40
+ public static function fromRecordData (array $ recordData ): self
40
41
{
41
42
return new self ($ recordData );
42
43
}
@@ -45,7 +46,7 @@ public static function fromRecordData(array $recordData)
45
46
* @param array $nativeData
46
47
* @return self
47
48
*/
48
- public static function fromArray (array $ nativeData )
49
+ public static function fromArray (array $ nativeData ): self
49
50
{
50
51
return new self (null , $ nativeData );
51
52
}
@@ -73,7 +74,7 @@ private function __construct(array $recordData = null, array $nativeData = null)
73
74
* @param array $recordData
74
75
* @return self
75
76
*/
76
- public function with (array $ recordData )
77
+ public function with (array $ recordData ): self
77
78
{
78
79
$ copy = clone $ this ;
79
80
$ copy ->setRecordData ($ recordData );
@@ -87,31 +88,37 @@ public function toArray(): array
87
88
$ arrayPropItemTypeMap = self ::getArrayPropItemTypeMapFromMethodOrCache ();
88
89
89
90
foreach (self ::$ __propTypeMap as $ key => [$ type , $ isNative , $ isNullable ]) {
91
+ $ specialKey = $ key ;
92
+
93
+ if ($ this instanceof SpecialKeySupport) {
94
+ $ specialKey = $ this ->convertKeyForArray ($ key );
95
+ }
96
+
90
97
switch ($ type ) {
91
98
case ImmutableRecord::PHP_TYPE_STRING :
92
99
case ImmutableRecord::PHP_TYPE_INT :
93
100
case ImmutableRecord::PHP_TYPE_FLOAT :
94
101
case ImmutableRecord::PHP_TYPE_BOOL :
95
102
case ImmutableRecord::PHP_TYPE_ARRAY :
96
103
if (\array_key_exists ($ key , $ arrayPropItemTypeMap ) && ! self ::isScalarType ($ arrayPropItemTypeMap [$ key ])) {
97
- if ($ isNullable && $ this ->{$ key }() === null ) {
98
- $ nativeData [$ key ] = null ;
104
+ if ($ isNullable && $ this ->{$ key } === null ) {
105
+ $ nativeData [$ specialKey ] = null ;
99
106
continue 2 ;
100
107
}
101
108
102
- $ nativeData [$ key ] = \array_map (function ($ item ) use ($ key , &$ arrayPropItemTypeMap ) {
109
+ $ nativeData [$ specialKey ] = \array_map (function ($ item ) use ($ key , &$ arrayPropItemTypeMap ) {
103
110
return $ this ->voTypeToNative ($ item , $ key , $ arrayPropItemTypeMap [$ key ]);
104
- }, $ this ->{$ key }() );
111
+ }, $ this ->{$ key });
105
112
} else {
106
- $ nativeData [$ key ] = $ this ->{$ key }() ;
113
+ $ nativeData [$ specialKey ] = $ this ->{$ key };
107
114
}
108
115
break ;
109
116
default :
110
- if ($ isNullable && $ this ->{$ key }() === null ) {
111
- $ nativeData [$ key ] = null ;
117
+ if ($ isNullable && (! isset ( $ this ->{$ key })) ) {
118
+ $ nativeData [$ specialKey ] = null ;
112
119
continue 2 ;
113
120
}
114
- $ nativeData [$ key ] = $ this ->voTypeToNative ($ this ->{$ key }() , $ key , $ type );
121
+ $ nativeData [$ specialKey ] = $ this ->voTypeToNative ($ this ->{$ key }, $ key , $ type );
115
122
}
116
123
}
117
124
@@ -120,38 +127,50 @@ public function toArray(): array
120
127
121
128
public function equals (ImmutableRecord $ other ): bool
122
129
{
123
- if ( get_class ($ this ) !== get_class ($ other )) {
130
+ if ( \ get_class ($ this ) !== \ get_class ($ other )) {
124
131
return false ;
125
132
}
126
133
127
134
return $ this ->toArray () === $ other ->toArray ();
128
135
}
129
136
130
- private function setRecordData (array $ recordData )
137
+ private function setRecordData (array $ recordData ): void
131
138
{
132
139
foreach ($ recordData as $ key => $ value ) {
133
- $ this ->assertType ($ key , $ value );
134
- $ this ->{$ key } = $ value ;
140
+ $ specialKey = $ key ;
141
+
142
+ if ($ this instanceof SpecialKeySupport) {
143
+ $ specialKey = $ this ->convertKeyForRecord ($ key );
144
+ }
145
+
146
+ $ this ->assertType ($ specialKey , $ value );
147
+ $ this ->{$ specialKey } = $ value ;
135
148
}
136
149
}
137
150
138
- private function setNativeData (array $ nativeData )
151
+ private function setNativeData (array $ nativeData ): void
139
152
{
140
153
$ recordData = [];
141
154
$ arrayPropItemTypeMap = self ::getArrayPropItemTypeMapFromMethodOrCache ();
142
155
143
156
foreach ($ nativeData as $ key => $ val ) {
144
- if (! isset (self ::$ __propTypeMap [$ key ])) {
157
+ $ specialKey = $ key ;
158
+
159
+ if ($ this instanceof SpecialKeySupport) {
160
+ $ specialKey = $ this ->convertKeyForRecord ($ key );
161
+ }
162
+
163
+ if (! isset (self ::$ __propTypeMap [$ specialKey ])) {
145
164
throw new \InvalidArgumentException (\sprintf (
146
- 'Invalid property passed to Record %s. Got property with key ' . $ key ,
165
+ 'Invalid property passed to Record %s. Got property with key ' . $ specialKey ,
147
166
\get_called_class ()
148
167
));
149
168
}
150
- [$ type , $ isNative , $ isNullable ] = self ::$ __propTypeMap [$ key ];
169
+ [$ type , $ isNative , $ isNullable ] = self ::$ __propTypeMap [$ specialKey ];
151
170
152
171
if ($ val === null ) {
153
172
if (! $ isNullable ) {
154
- throw new \RuntimeException ("Got null for non nullable property $ key of Record " . \get_called_class ());
173
+ throw new \RuntimeException ("Got null for non nullable property $ specialKey of Record " . \get_called_class ());
155
174
}
156
175
157
176
$ recordData [$ key ] = null ;
@@ -182,10 +201,10 @@ private function setNativeData(array $nativeData)
182
201
$ this ->setRecordData ($ recordData );
183
202
}
184
203
185
- private function assertAllNotNull ()
204
+ private function assertAllNotNull (): void
186
205
{
187
206
foreach (self ::$ __propTypeMap as $ key => [$ type , $ isNative , $ isNullable ]) {
188
- if (null === $ this ->{$ key } && ! $ isNullable ) {
207
+ if (! isset ( $ this ->{$ key }) && ! $ isNullable ) {
189
208
throw new \InvalidArgumentException (\sprintf (
190
209
'Missing record data for key %s of record %s. ' ,
191
210
$ key ,
@@ -195,7 +214,7 @@ private function assertAllNotNull()
195
214
}
196
215
}
197
216
198
- private function assertType (string $ key , $ value )
217
+ private function assertType (string $ key , $ value ): void
199
218
{
200
219
if (! isset (self ::$ __propTypeMap [$ key ])) {
201
220
throw new \InvalidArgumentException (\sprintf (
@@ -264,7 +283,7 @@ private function isType(string $type, string $key, $value): bool
264
283
}
265
284
}
266
285
267
- private static function buildPropTypeMap ()
286
+ private static function buildPropTypeMap (): array
268
287
{
269
288
$ refObj = new \ReflectionClass (__CLASS__ );
270
289
@@ -384,12 +403,12 @@ private static function getArrayPropItemTypeMapFromMethodOrCache(): array
384
403
}
385
404
386
405
/**
387
- * @var array
406
+ * @var array|null
388
407
*/
389
408
private static $ __propTypeMap ;
390
409
391
410
/**
392
- * @var array
411
+ * @var array|null
393
412
*/
394
413
private static $ __arrayPropItemTypeMap ;
395
414
}
0 commit comments