@@ -27,19 +27,38 @@ public static function fromArray(array &$mappedClass)
2727 $ idProperties = self ::getIdProperties ($ mappedClass );
2828
2929 $ mapping = new Mapping ($ className , $ resourceUrl , $ idProperties );
30-
3130 $ mapping ->setClassAlias ((empty ($ mappedClass ['alias ' ])) ? $ className : $ mappedClass ['alias ' ]);
3231
3332 if (false === empty ($ mappedClass ['aliased_properties ' ])) {
3433 $ 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+ }
3541 }
3642
3743 if (false === empty ($ mappedClass ['hide_properties ' ])) {
3844 $ 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+ }
3952 }
4053
4154 if (!empty ($ mappedClass ['relationships ' ])) {
4255 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+
4362 $ mapping ->setRelationshipUrls ($ propertyName , $ urls );
4463 }
4564 }
@@ -99,8 +118,7 @@ private static function getSelfUrl(array &$mappedClass)
99118 */
100119 private static function getIdProperties (array &$ mappedClass )
101120 {
102-
103- return (!empty ($ mappedClass ['id_properties ' ]))? $ mappedClass ['id_properties ' ] : [];
121+ return (!empty ($ mappedClass ['id_properties ' ])) ? $ mappedClass ['id_properties ' ] : [];
104122 }
105123
106124 /**
@@ -116,4 +134,33 @@ private static function getOtherUrls(array $mappedClass)
116134
117135 return $ mappedClass ['urls ' ];
118136 }
137+
138+ /**
139+ * Recursive function to get an associative array of class properties by
140+ * property name, including inherited ones from extended classes.
141+ *
142+ * @param string $className Class name
143+ *
144+ * @return array
145+ *
146+ * @link http://php.net/manual/es/reflectionclass.getproperties.php#88405
147+ */
148+ private static function getClassProperties ($ className )
149+ {
150+ $ ref = new \ReflectionClass ($ className );
151+ $ properties = array ();
152+ foreach ($ ref ->getProperties () as $ prop ) {
153+ $ f = $ prop ->getName ();
154+ $ properties [$ f ] = $ prop ;
155+ }
156+
157+ if ($ parentClass = $ ref ->getParentClass ()) {
158+ $ parentPropsArr = self ::getClassProperties ($ parentClass ->getName ());
159+ if (count ($ parentPropsArr ) > 0 ) {
160+ $ properties = array_merge ($ parentPropsArr , $ properties );
161+ }
162+ }
163+
164+ return array_keys ($ properties );
165+ }
119166}
0 commit comments