1111
1212/**
1313 * @method SchemaContract __get($key)
14- * @method Schema[] toArray()
1514 */
1615class Properties extends ObjectItem implements Constraint
1716{
@@ -23,18 +22,84 @@ class Properties extends ObjectItem implements Constraint
2322 /** @var Schema */
2423 protected $ __schema ;
2524
25+ /**
26+ * @var Schema[]
27+ */
28+ private $ __mappedProperties ;
29+
30+ /**
31+ * @var array
32+ */
33+ private $ __dataKeyMaps = array ();
34+
35+ /**
36+ * Data to property mapping, example ["$ref" => "ref"]
37+ * @var array
38+ */
39+ public $ __dataToProperty = array ();
40+
2641 /**
2742 * Property to data mapping, example ["ref" => "$ref"]
2843 * @var array
2944 */
30- public $ __defaultMapping = array ();
45+ public $ __propertyToData = array ();
46+
47+ /**
48+ * Returns a map of properties by default data name
49+ * @return Schema[]
50+ */
51+ public function &toArray ()
52+ {
53+ if (!isset ($ this ->__propertyToData [Schema::DEFAULT_MAPPING ])) {
54+ return $ this ->__arrayOfData ;
55+ }
56+ if (null === $ this ->__mappedProperties ) {
57+ $ properties = array ();
58+ foreach ($ this ->__arrayOfData as $ propertyName => $ property ) {
59+ if (isset ($ this ->__propertyToData [Schema::DEFAULT_MAPPING ][$ propertyName ])) {
60+ $ propertyName = $ this ->__propertyToData [Schema::DEFAULT_MAPPING ][$ propertyName ];
61+ }
62+ $ properties [$ propertyName ] = $ property ;
63+ }
64+ $ this ->__mappedProperties = $ properties ;
65+ }
66+ return $ this ->__mappedProperties ;
67+ }
68+
69+ /**
70+ * @param string $mapping
71+ * @return string[] a map of propertyName to dataName
72+ */
73+ public function getDataKeyMap ($ mapping = Schema::DEFAULT_MAPPING )
74+ {
75+ if (!isset ($ this ->__dataKeyMaps [$ mapping ])) {
76+ $ map = array ();
77+ foreach ($ this ->__arrayOfData as $ propertyName => $ property ) {
78+ if (isset ($ this ->__propertyToData [$ mapping ][$ propertyName ])) {
79+ $ map [$ propertyName ] = $ this ->__propertyToData [$ mapping ][$ propertyName ];
80+ } else {
81+ $ map [$ propertyName ] = $ propertyName ;
82+ }
83+ }
84+ $ this ->__dataKeyMaps [$ mapping ] = $ map ;
85+ }
86+
87+ return $ this ->__dataKeyMaps [$ mapping ];
88+ }
3189
3290 public function lock ()
3391 {
3492 $ this ->__isReadOnly = true ;
3593 return $ this ;
3694 }
3795
96+ public function addPropertyMapping ($ dataName , $ propertyName , $ mapping = Schema::DEFAULT_MAPPING )
97+ {
98+ $ this ->__dataToProperty [$ mapping ][$ dataName ] = $ propertyName ;
99+ $ this ->__propertyToData [$ mapping ][$ propertyName ] = $ dataName ;
100+ }
101+
102+
38103 /**
39104 * @param string $name
40105 * @param mixed $column
@@ -101,7 +166,8 @@ public function isEmpty()
101166
102167 public function jsonSerialize ()
103168 {
104- $ result = $ this ->__arrayOfData ;
169+ $ result = $ this ->toArray ();
170+
105171 if ($ this ->__nestedObjects ) {
106172 foreach ($ this ->__nestedObjects as $ object ) {
107173 foreach ($ object ->toArray () as $ key => $ value ) {
@@ -110,18 +176,6 @@ public function jsonSerialize()
110176 }
111177 }
112178
113- if (isset ($ this ->__defaultMapping )) {
114- $ mappedResult = new \stdClass ();
115- foreach ($ result as $ key => $ value ) {
116- if (isset ($ this ->__defaultMapping [$ key ])) {
117- $ mappedResult ->{$ this ->__defaultMapping [$ key ]} = $ value ;
118- } else {
119- $ mappedResult ->$ key = $ value ;
120- }
121- }
122- return $ mappedResult ;
123- }
124-
125179 return (object )$ result ;
126180 }
127181
0 commit comments