@@ -79,7 +79,7 @@ protected function isAllowedAttribute($classOrObject, $attribute, $format = null
79
79
}
80
80
81
81
try {
82
- $ reflectionProperty = new \ ReflectionProperty ( is_string ( $ classOrObject ) ? $ classOrObject : get_class ($ classOrObject) , $ attribute );
82
+ $ reflectionProperty = $ this -> getReflectionProperty ($ classOrObject , $ attribute );
83
83
if ($ reflectionProperty ->isStatic ()) {
84
84
return false ;
85
85
}
@@ -98,13 +98,15 @@ protected function extractAttributes($object, $format = null, array $context = a
98
98
$ reflectionObject = new \ReflectionObject ($ object );
99
99
$ attributes = array ();
100
100
101
- foreach ($ reflectionObject ->getProperties () as $ property ) {
102
- if (!$ this ->isAllowedAttribute ($ object , $ property ->name )) {
103
- continue ;
104
- }
101
+ do {
102
+ foreach ($ reflectionObject ->getProperties () as $ property ) {
103
+ if (!$ this ->isAllowedAttribute ($ reflectionObject ->getName (), $ property ->name )) {
104
+ continue ;
105
+ }
105
106
106
- $ attributes [] = $ property ->name ;
107
- }
107
+ $ attributes [] = $ property ->name ;
108
+ }
109
+ } while ($ reflectionObject = $ reflectionObject ->getParentClass ());
108
110
109
111
return $ attributes ;
110
112
}
@@ -115,7 +117,7 @@ protected function extractAttributes($object, $format = null, array $context = a
115
117
protected function getAttributeValue ($ object , $ attribute , $ format = null , array $ context = array ())
116
118
{
117
119
try {
118
- $ reflectionProperty = new \ ReflectionProperty ( get_class ( $ object) , $ attribute );
120
+ $ reflectionProperty = $ this -> getReflectionProperty ( $ object , $ attribute );
119
121
} catch (\ReflectionException $ reflectionException ) {
120
122
return ;
121
123
}
@@ -134,7 +136,7 @@ protected function getAttributeValue($object, $attribute, $format = null, array
134
136
protected function setAttributeValue ($ object , $ attribute , $ value , $ format = null , array $ context = array ())
135
137
{
136
138
try {
137
- $ reflectionProperty = new \ ReflectionProperty ( get_class ( $ object) , $ attribute );
139
+ $ reflectionProperty = $ this -> getReflectionProperty ( $ object , $ attribute );
138
140
} catch (\ReflectionException $ reflectionException ) {
139
141
return ;
140
142
}
@@ -150,4 +152,26 @@ protected function setAttributeValue($object, $attribute, $value, $format = null
150
152
151
153
$ reflectionProperty ->setValue ($ object , $ value );
152
154
}
155
+
156
+ /**
157
+ * @param string|object $classOrObject
158
+ * @param string $attribute
159
+ *
160
+ * @return \ReflectionProperty
161
+ *
162
+ * @throws \ReflectionException
163
+ */
164
+ private function getReflectionProperty ($ classOrObject , $ attribute )
165
+ {
166
+ $ reflectionClass = new \ReflectionClass ($ classOrObject );
167
+ while (true ) {
168
+ try {
169
+ return $ reflectionClass ->getProperty ($ attribute );
170
+ } catch (\ReflectionException $ e ) {
171
+ if (!$ reflectionClass = $ reflectionClass ->getParentClass ()) {
172
+ throw $ e ;
173
+ }
174
+ }
175
+ }
176
+ }
153
177
}
0 commit comments