10000 [Serializer] Move the normalization logic in an abstract class by dunglas · Pull Request #17191 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Move the normalization logic in an abstract class #17191

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Improve perfs
  • Loading branch information
dunglas committed Dec 30, 2015
commit 257670d7b4e0168c30bfddd89fcc24fa0f987d8c
24 changes: 22 additions & 2 deletions src/Symfony/Component/Serializer/Normalizer/AbstractNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -199,14 +199,34 @@ protected function getAllowedAttributes($classOrObject, array $context, $attribu

$allowedAttributes = array();
foreach ($this->classMetadataFactory->getMetadataFor($classOrObject)->getAttributesMetadata() as $attributeMetadata) {
if (count(array_intersect($attributeMetadata->getGroups(), $context['groups']))) {
$allowedAttributes[] = $attributesAsString ? $attributeMetadata->getName() : $attributeMetadata;
$name = $attributeMetadata->getName();

if (
count(array_intersect($attributeMetadata->getGroups(), $context['groups'])) &&
$this->isAllowedAttribute($classOrObject, $name, null, $context)
) {
$allowedAttributes[] = $attributesAsString ? $name : $attributeMetadata;
}
}

return $allowedAttributes;
}

/**
* Is this attribute allowed?
*
* @param object|string $classOrObject
* @param string $attribute
* @param string|null $format
* @param array $context
*
* @return bool
*/
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
{
return !in_array($attribute, $this->ignoredAttributes);
}

/**
* Normalizes the given data to an array. It's particularly useful during
* the denormalization process.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ public function normalize($object, $format = null, array $context = array())
$attributes = $this->getAttributes($object, $format, $context);

foreach ($attributes as $attribute) {
if (in_array($attribute, $this->ignoredAttributes)) {
continue;
}

$attributeValue = $this->getAttributeValue($object, $attribute, $format, $context);

if (isset($this->callbacks[$attribute])) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,8 +137,14 @@ protected function extractAttributes($object, $format = null, array $context = a

$attributes = array();
foreach ($reflectionMethods as $method) {
if ($this->isGetMethod($method)) {
$attributes[] = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));
if (!$this->isGetMethod($method)) {
continue;
}

$attributeName = lcfirst(substr($method->name, 0 === strpos($method->name, 'is') ? 2 : 3));

if ($this->isAllowedAttribute($object, $attributeName)) {
$attributes[] = $attributeName;
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,16 +60,20 @@ protected function extractAttributes($object, $format = null, array $context = a

if (strpos($name, 'get') === 0 || strpos($name, 'has') === 0) {
// getters and hassers
$attributes[lcfirst(substr($name, 3))] = true;
$attributeName = lcfirst(substr($name, 3));
} elseif (strpos($name, 'is') === 0) {
// issers
$attributes[lcfirst(substr($name, 2))] = true;
$attributeName = lcfirst(substr($name, 2));
}

if ($this->isAllowedAttribute($object, $attributeName)) {
$attributes[$attributeName] = true;
}
}

// properties
foreach ($reflClass->getProperties(\ReflectionProperty::IS_PUBLIC) as $reflProperty) {
if ($reflProperty->isStatic()) {
if ($reflProperty->isStatic() || !$this->isAllowedAttribute($object, $reflProperty->name)) {
continue;
}

Expand Down
31 changes: 10 additions & 21 deletions src/Symfony/Component/Serializer/Normalizer/PropertyNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,34 +69,23 @@ private function supports($class)

/**
* {@inheritdoc}
*
* Ignores allowed attributes not applicable on properties (e.g. groups set on a getter method).
*/
protected function getAllowedAttributes($classOrObject, array $context, $attributesAsString = false)
protected function isAllowedAttribute($classOrObject, $attribute, $format = null, array $context = array())
{
$allowedAttributes = parent::getAllowedAttributes($classOrObject, $context, $attributesAsString);

if (false === $allowedAttributes) {
if (!parent::isAllowedAttribute($classOrObject, $attribute, $format, $context)) {
return false;
}

$class = is_string($classOrObject) ? $classOrObject : get_class($classOrObject);

$attributes = array();
fo 929A reach ($allowedAttributes as $allowedAttribute) {
$propertyName = $attributesAsString ? $allowedAttribute : $allowedAttribute->getName();

try {
$reflectionProperty = new \ReflectionProperty($class, $propertyName);
if (!$reflectionProperty->isStatic()) {
$attributes[] = $allowedAttribute;
}
} catch (\ReflectionException $reflectionException) {
// Ignore attributes not applicable for this normalizer
try {
$reflectionProperty = new \ReflectionProperty(is_string($classOrObject) ? $classOrObject : get_class($classOrObject), $attribute);
if ($reflectionProperty->isStatic()) {
return false;
}
} catch (\ReflectionException $reflectionException) {
return false;
}

return $attributes;
return true;
}

/**
Expand All @@ -108,7 +97,7 @@ protected function extractAttributes($object, $format = null, array $context = a
$attributes = array();

foreach ($reflectionObject->getProperties() as $property) {
if ($property->isStatic()) {
if (!$this->isAllowedAttribute($object, $property->name)) {
continue;
}

Expand Down
0