@@ -27,19 +27,38 @@ public static function fromArray(array &$mappedClass)
27
27
$ idProperties = self ::getIdProperties ($ mappedClass );
28
28
29
29
$ mapping = new Mapping ($ className , $ resourceUrl , $ idProperties );
30
-
31
30
$ mapping ->setClassAlias ((empty ($ mappedClass ['alias ' ])) ? $ className : $ mappedClass ['alias ' ]);
32
31
33
32
if (false === empty ($ mappedClass ['aliased_properties ' ])) {
34
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
+ }
35
41
}
36
42
37
43
if (false === empty ($ mappedClass ['hide_properties ' ])) {
38
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
+ }
39
52
}
40
53
41
54
if (!empty ($ mappedClass ['relationships ' ])) {
42
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
+
43
62
$ mapping ->setRelationshipUrls ($ propertyName , $ urls );
44
63
}
45
64
}
@@ -99,8 +118,7 @@ private static function getSelfUrl(array &$mappedClass)
99
118
*/
100
119
private static function getIdProperties (array &$ mappedClass )
101
120
{
102
-
103
- return (!empty ($ mappedClass ['id_properties ' ]))? $ mappedClass ['id_properties ' ] : [];
121
+ return (!empty ($ mappedClass ['id_properties ' ])) ? $ mappedClass ['id_properties ' ] : [];
104
122
}
105
123
106
124
/**
@@ -116,4 +134,33 @@ private static function getOtherUrls(array $mappedClass)
116
134
117
135
return $ mappedClass ['urls ' ];
118
136
}
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
+ }
119
166
}
0 commit comments