*/
class PropertyNormalizer extends AbstractObjectNormalizer
{
+ private $cache = array();
+
/**
* {@inheritdoc}
*/
public function supportsNormalization($data, $format = null)
{
- return parent::supportsNormalization($data, $format) && $this->supports(get_class($data));
+ return parent::supportsNormalization($data, $format) && (isset($this->cache[$type = \get_class($data)]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
}
/**
*/
public function supportsDenormalization($data, $type, $format = null)
{
- return parent::supportsDenormalization($data, $type, $format) && $this->supports($type);
+ return parent::supportsDenormalization($data, $type, $format) && (isset($this->cache[$type]) ? $this->cache[$type] : $this->cache[$type] = $this->supports($type));
}
/**
$class = new \ReflectionClass($class);
// We look for at least one non-static property
- foreach ($class->getProperties() as $property) {
- if (!$property->isStatic()) {
- return true;
+ do {
+ foreach ($class->getProperties() as $property) {
+ if (!$property->isStatic()) {
+ return true;
+ }
}
- }
+ } while ($class = $class->getParentClass());
return false;
}
}
try {
- $reflectionProperty = new \ReflectionProperty(is_string($classOrObject) ? $classOrObject : get_class($classOrObject), $attribute);
+ $reflectionProperty = $this->getReflectionProperty($classOrObject, $attribute);
if ($reflectionProperty->isStatic()) {
return false;
}
$reflectionObject = new \ReflectionObject($object);
$attributes = array();
- foreach ($reflectionObject->getProperties() as $property) {
- if (!$this->isAllowedAttribute($object, $property->name)) {
- continue;
- }
+ do {
+ foreach ($reflectionObject->getProperties() as $property) {
+ if (!$this->isAllowedAttribute($reflectionObject->getName(), $property->name)) {
+ continue;
+ }
- $attributes[] = $property->name;
- }
+ $attributes[] = $property->name;
+ }
+ } while ($reflectionObject = $reflectionObject->getParentClass());
return $attributes;
}
protected function getAttributeValue($object, $attribute, $format = null, array $context = array())
{
try {
- $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
+ $reflectionProperty = $this->getReflectionProperty($object, $attribute);
} catch (\ReflectionException $reflectionException) {
return;
}
protected function setAttributeValue($object, $attribute, $value, $format = null, array $context = array())
{
try {
- $reflectionProperty = new \ReflectionProperty(get_class($object), $attribute);
+ $reflectionProperty = $this->getReflectionProperty($object, $attribute);
} catch (\ReflectionException $reflectionException) {
return;
}
$reflectionProperty->setValue($object, $value);
}
+
+ /**
+ * @param string|object $classOrObject
+ * @param string $attribute
+ *
+ * @return \ReflectionProperty
+ *
+ * @throws \ReflectionException
+ */
+ private function getReflectionProperty($classOrObject, $attribute)
+ {
+ $reflectionClass = new \ReflectionClass($classOrObject);
+ while (true) {
+ try {
+ return $reflectionClass->getProperty($attribute);
+ } catch (\ReflectionException $e) {
+ if (!$reflectionClass = $reflectionClass->getParentClass()) {
+ throw $e;
+ }
+ }
+ }
+ }
}