10
10
*/
11
11
namespace NilPortugues \Api \Mapping ;
12
12
13
+ use ReflectionClass ;
14
+
13
15
/**
14
16
* Class MappingFactory.
15
17
*/
16
18
class MappingFactory
17
19
{
20
+ /**
21
+ * @var array
22
+ */
23
+ private static $ classProperties = [];
24
+
18
25
/**
19
26
* @param array $mappedClass
20
27
*
28
+ * @throws MappingException
29
+ *
21
30
* @return Mapping
22
31
*/
23
32
public static function fromArray (array &$ mappedClass )
@@ -29,43 +38,10 @@ public static function fromArray(array &$mappedClass)
29
38
$ mapping = new Mapping ($ className , $ resourceUrl , $ idProperties );
30
39
$ mapping ->setClassAlias ((empty ($ mappedClass ['alias ' ])) ? $ className : $ mappedClass ['alias ' ]);
31
40
32
- if (false === empty ($ mappedClass ['aliased_properties ' ])) {
33
- $ mapping ->setPropertyNameAliases ($ mappedClass ['aliased_properties ' ]);
34
- foreach (array_keys ($ mapping ->getAliasedProperties ()) as $ propertyName ) {
35
- if (false === in_array ($ propertyName , self ::getClassProperties ($ className ), true )) {
36
- throw new MappingException (
37
- sprintf ('Could not alias property %s in class %s because it does not exist. ' , $ propertyName , $ className )
38
- );
39
- }
40
- }
41
- }
42
-
43
- if (false === empty ($ mappedClass ['hide_properties ' ])) {
44
- $ mapping ->setHiddenProperties ($ mappedClass ['hide_properties ' ]);
45
- foreach ($ mapping ->getHiddenProperties () as $ propertyName ) {
46
- if (false === in_array ($ propertyName , self ::getClassProperties ($ className ), true )) {
47
- throw new MappingException (
48
- sprintf ('Could not hide property %s in class %s because it does not exist. ' , $ propertyName , $ className )
49
- );
50
- }
51
- }
52
- }
53
-
54
- if (!empty ($ mappedClass ['relationships ' ])) {
55
- foreach ($ mappedClass ['relationships ' ] as $ propertyName => $ urls ) {
56
- if (false === in_array ($ propertyName , self ::getClassProperties ($ className ), true )) {
57
- throw new MappingException (
58
- sprintf ('Could not find property %s in class %s because it does not exist. ' , $ propertyName , $ className )
59
- );
60
- }
61
-
62
- $ mapping ->setRelationshipUrls ($ propertyName , $ urls );
63
- }
64
- }
65
-
66
- if (false === empty ($ mappedClass ['curies ' ])) {
67
- $ mapping ->setCuries ($ mappedClass ['curies ' ]);
68
- }
41
+ self ::setAliasedProperties ($ mappedClass , $ mapping , $ className );
42
+ self ::setHideProperties ($ mappedClass , $ mapping , $ className );
43
+ self ::setRelationships ($ mappedClass , $ mapping , $ className );
44
+ self ::setCuries ($ mappedClass , $ mapping );
69
45
70
46
$ otherUrls = self ::getOtherUrls ($ mappedClass );
71
47
if (!empty ($ otherUrls )) {
@@ -122,17 +98,28 @@ private static function getIdProperties(array &$mappedClass)
122
98
}
123
99
124
100
/**
125
- * @param array $mappedClass
101
+ * @param array $mappedClass
102
+ * @param Mapping $mapping
103
+ * @param string $className
126
104
*
127
- * @return mixed
105
+ * @throws MappingException
128
106
*/
129
- private static function getOtherUrls (array $ mappedClass )
107
+ protected static function setAliasedProperties (array & $ mappedClass, Mapping $ mapping , $ className )
130
108
{
131
- if (!empty ($ mappedClass ['urls ' ]['self ' ])) {
132
- unset($ mappedClass ['urls ' ]['self ' ]);
109
+ if (false === empty ($ mappedClass ['aliased_properties ' ])) {
110
+ $ mapping ->setPropertyNameAliases ($ mappedClass ['aliased_properties ' ]);
111
+ foreach (array_keys ($ mapping ->getAliasedProperties ()) as $ propertyName ) {
112
+ if (false === in_array ($ propertyName , self ::getClassProperties ($ className ), true )) {
113
+ throw new MappingException (
114
+ sprintf (
115
+ 'Could not alias property %s in class %s because it does not exist. ' ,
116
+ $ propertyName ,
117
+ $ className
118
+ )
119
+ );
120
+ }
121
+ }
133
122
}
134
-
135
- return $ mappedClass ['urls ' ];
136
123
}
137
124
138
125
/**
@@ -147,20 +134,99 @@ private static function getOtherUrls(array $mappedClass)
147
134
*/
148
135
private static function getClassProperties ($ className )
149
136
{
150
- $ ref = new \ReflectionClass ($ className );
151
- $ properties = array ();
152
- foreach ($ ref ->getProperties () as $ prop ) {
153
- $ f = $ prop ->getName ();
154
- $ properties [$ f ] = $ prop ;
137
+ if (empty (self ::$ classProperties [$ className ])) {
138
+ $ ref = new ReflectionClass ($ className );
139
+ $ properties = [];
140
+ foreach ($ ref ->getProperties () as $ prop ) {
141
+ $ f = $ prop ->getName ();
142
+ $ properties [$ f ] = $ prop ;
143
+ }
144
+
145
+ if ($ parentClass = $ ref ->getParentClass ()) {
146
+ $ parentPropsArr = self ::getClassProperties ($ parentClass ->getName ());
147
+ if (count ($ parentPropsArr ) > 0 ) {
148
+ $ properties = array_merge ($ parentPropsArr , $ properties );
149
+ }
150
+ }
151
+ self ::$ classProperties [$ className ] = array_keys ($ properties );
152
+ }
153
+
154
+ return self ::$ classProperties [$ className ];
155
+ }
156
+
157
+ /**
158
+ * @param array $mappedClass
159
+ * @param Mapping $mapping
160
+ * @param string $className
161
+ *
162
+ * @throws MappingException
163
+ */
164
+ protected static function setHideProperties (array &$ mappedClass , Mapping $ mapping , $ className )
165
+ {
166
+ if (false === empty ($ mappedClass ['hide_properties ' ])) {
167
+ $ mapping ->setHiddenProperties ($ mappedClass ['hide_properties ' ]);
168
+ foreach ($ mapping ->getHiddenProperties () as $ propertyName ) {
169
+ if (false === in_array ($ propertyName , self ::getClassProperties ($ className ), true )) {
170
+ throw new MappingException (
171
+ sprintf (
172
+ 'Could not hide property %s in class %s because it does not exist. ' ,
173
+ $ propertyName ,
174
+ $ className
175
+ )
176
+ );
177
+ }
178
+ }
155
179
}
180
+ }
181
+
182
+ /**
183
+ * @param array $mappedClass
184
+ * @param Mapping $mapping
185
+ * @param string $className
186
+ *
187
+ * @throws MappingException
188
+ */
189
+ protected static function setRelationships (array &$ mappedClass , Mapping $ mapping , $ className )
190
+ {
191
+ if (!empty ($ mappedClass ['relationships ' ])) {
192
+ foreach ($ mappedClass ['relationships ' ] as $ propertyName => $ urls ) {
193
+ if (false === in_array ($ propertyName , self ::getClassProperties ($ className ), true )) {
194
+ throw new MappingException (
195
+ sprintf (
196
+ 'Could not find property %s in class %s because it does not exist. ' ,
197
+ $ propertyName ,
198
+ $ className
199
+ )
200
+ );
201
+ }
156
202
157
- if ($ parentClass = $ ref ->getParentClass ()) {
158
- $ parentPropsArr = self ::getClassProperties ($ parentClass ->getName ());
159
- if (count ($ parentPropsArr ) > 0 ) {
160
- $ properties = array_merge ($ parentPropsArr , $ properties );
203
+ $ mapping ->setRelationshipUrls ($ propertyName , $ urls );
161
204
}
162
205
}
206
+ }
207
+
208
+ /**
209
+ * @param array $mappedClass
210
+ * @param Mapping $mapping
211
+ */
212
+ protected static function setCuries (array &$ mappedClass , Mapping $ mapping )
213
+ {
214
+ if (false === empty ($ mappedClass ['curies ' ])) {
215
+ $ mapping ->setCuries ($ mappedClass ['curies ' ]);
216
+ }
217
+ }
218
+
219
+ /**
220
+ * @param array $mappedClass
221
+ *
222
+ * @return mixed
223
+ */
224
+ private static function getOtherUrls (array $ mappedClass )
225
+ {
226
+ if (!empty ($ mappedClass ['urls ' ]['self ' ])) {
227
+ unset($ mappedClass ['urls ' ]['self ' ]);
228
+ }
163
229
164
- return array_keys ( $ properties ) ;
230
+ return $ mappedClass [ ' urls ' ] ;
165
231
}
166
232
}
0 commit comments