1010 */
1111namespace NilPortugues \Api \Mapping ;
1212
13+ use ReflectionClass ;
14+
1315/**
1416 * Class MappingFactory.
1517 */
1618class MappingFactory
1719{
20+ /**
21+ * @var array
22+ */
23+ private static $ classProperties = [];
24+
1825 /**
1926 * @param array $mappedClass
2027 *
28+ * @throws MappingException
29+ *
2130 * @return Mapping
2231 */
2332 public static function fromArray (array &$ mappedClass )
@@ -29,43 +38,10 @@ public static function fromArray(array &$mappedClass)
2938 $ mapping = new Mapping ($ className , $ resourceUrl , $ idProperties );
3039 $ mapping ->setClassAlias ((empty ($ mappedClass ['alias ' ])) ? $ className : $ mappedClass ['alias ' ]);
3140
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 );
6945
7046 $ otherUrls = self ::getOtherUrls ($ mappedClass );
7147 if (!empty ($ otherUrls )) {
@@ -122,17 +98,28 @@ private static function getIdProperties(array &$mappedClass)
12298 }
12399
124100 /**
125- * @param array $mappedClass
101+ * @param array $mappedClass
102+ * @param Mapping $mapping
103+ * @param string $className
126104 *
127- * @return mixed
105+ * @throws MappingException
128106 */
129- private static function getOtherUrls (array $ mappedClass )
107+ protected static function setAliasedProperties (array & $ mappedClass, Mapping $ mapping , $ className )
130108 {
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+ }
133122 }
134-
135- return $ mappedClass ['urls ' ];
136123 }
137124
138125 /**
@@ -147,20 +134,99 @@ private static function getOtherUrls(array $mappedClass)
147134 */
148135 private static function getClassProperties ($ className )
149136 {
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+ }
155179 }
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+ }
156202
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 );
161204 }
162205 }
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+ }
163229
164- return array_keys ( $ properties ) ;
230+ return $ mappedClass [ ' urls ' ] ;
165231 }
166232}
0 commit comments