@@ -98,6 +98,16 @@ class Schema extends MagicMap
98
98
public $ objectItemClass ;
99
99
private $ useObjectAsArray = false ;
100
100
101
+ private $ __dataToProperty = array ();
102
+ private $ __propertyToData = array ();
103
+
104
+ public function addPropertyMapping ($ dataName , $ propertyName )
105
+ {
106
+ $ this ->__dataToProperty [$ dataName ] = $ propertyName ;
107
+ $ this ->__propertyToData [$ propertyName ] = $ dataName ;
108
+ return $ this ;
109
+ }
110
+
101
111
public function import ($ data , DataPreProcessor $ preProcessor = null )
102
112
{
103
113
return $ this ->process ($ data , true , $ preProcessor );
@@ -216,7 +226,7 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
216
226
}
217
227
}
218
228
if ($ this ->pattern !== null ) {
219
- if (0 === preg_match ($ this ->pattern , $ data )) {
229
+ if (0 === preg_match (Helper:: toPregPattern ( $ this ->pattern ) , $ data )) {
220
230
$ this ->fail (new StringException ('Does not match to '
221
231
. $ this ->pattern , StringException::PATTERN_MISMATCH ), $ path );
222
232
}
@@ -298,7 +308,24 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
298
308
$ nestedProperties = $ this ->properties ->getNestedProperties ();
299
309
}
300
310
301
- $ array = (array )$ data ;
311
+ $ array = array ();
312
+ if (!empty ($ this ->__dataToProperty )) {
313
+ foreach ((array )$ data as $ key => $ value ) {
314
+ if ($ import ) {
315
+ if (isset ($ this ->__dataToProperty [$ key ])) {
316
+ $ key = $ this ->__dataToProperty [$ key ];
317
+ }
318
+ } else {
319
+ if (isset ($ this ->__propertyToData [$ key ])) {
320
+ $ key = $ this ->__propertyToData [$ key ];
321
+ }
322
+ }
323
+ $ array [$ key ] = $ value ;
324
+ }
325
+ } else {
326
+ $ array = (array )$ data ;
327
+ }
328
+
302
329
if ($ this ->minProperties !== null && count ($ array ) < $ this ->minProperties ) {
303
330
$ this ->fail (new ObjectException ("Not enough properties " , ObjectException::TOO_FEW ), $ path );
304
331
}
@@ -343,7 +370,7 @@ private function process($data, $import = true, DataPreProcessor $preProcessor =
343
370
344
371
if ($ this ->patternProperties !== null ) {
345
372
foreach ($ this ->patternProperties as $ pattern => $ propertySchema ) {
346
- if (preg_match ($ pattern , $ key )) {
373
+ if (preg_match (Helper:: toPregPattern ( $ pattern) , $ key )) {
347
374
$ found = true ;
348
375
$ value = $ propertySchema ->process ($ value , $ import , $ preProcessor , $ path . '->patternProperties: ' . $ pattern );
349
376
if ($ import ) {
0 commit comments