11
11
12
12
/**
13
13
* @method SchemaContract __get($key)
14
- * @method Schema[] toArray()
15
14
*/
16
15
class Properties extends ObjectItem implements Constraint
17
16
{
@@ -23,18 +22,84 @@ class Properties extends ObjectItem implements Constraint
23
22
/** @var Schema */
24
23
protected $ __schema ;
25
24
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
+
26
41
/**
27
42
* Property to data mapping, example ["ref" => "$ref"]
28
43
* @var array
29
44
*/
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
+ }
31
89
32
90
public function lock ()
33
91
{
34
92
$ this ->__isReadOnly = true ;
35
93
return $ this ;
36
94
}
37
95
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
+
38
103
/**
39
104
* @param string $name
40
105
* @param mixed $column
@@ -101,7 +166,8 @@ public function isEmpty()
101
166
102
167
public function jsonSerialize ()
103
168
{
104
- $ result = $ this ->__arrayOfData ;
169
+ $ result = $ this ->toArray ();
170
+
105
171
if ($ this ->__nestedObjects ) {
106
172
foreach ($ this ->__nestedObjects as $ object ) {
107
173
foreach ($ object ->toArray () as $ key => $ value ) {
@@ -110,18 +176,6 @@ public function jsonSerialize()
110
176
}
111
177
}
112
178
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
-
125
179
return (object )$ result ;
126
180
}
127
181
0 commit comments