8000 [WIP][Serializer]Use PropertyInfo to extract properties by fbourigault · Pull Request #28775 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[WIP][Serializer]Use PropertyInfo to extract properties #28775

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 8 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
[Serializer] use property info component to extract class properties
  • Loading branch information
fbourigault committed Nov 6, 2018
commit 16322679787a5038ba5ef3d87a75d92f3d043bcb
57 changes: 9 additions & 48 deletions src/Symfony/Component/Serializer/Normalizer/ObjectNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccess;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\PropertyInfo\PropertyTypeExtractorInterface;
use Symfony\Component\Serializer\Exception\LogicException;
use Symfony\Component\Serializer\Mapping\AttributeMetadata;
Expand All @@ -32,6 +34,8 @@ class ObjectNormalizer extends AbstractObjectNormalizer

private $discriminatorCache = array();

private $propertyListExtractor;

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory = null, NameConverterInterface $nameConverter = null, PropertyAccessorInterface $propertyAccessor = null, PropertyTypeExtractorInterface $propertyTypeExtractor = null, ClassDiscriminatorResolverInterface $classDiscriminatorResolver = null, callable $objectClassResolver = null, array $defaultContext = array())
{
if (!\class_exists(PropertyAccess::class)) {
Expand All @@ -41,6 +45,7 @@ public function __construct(ClassMetadataFactoryInterface $classMetadataFactory
parent::__construct($classMetadataFactory, $nameConverter, $propertyTypeExtractor, $classDiscriminatorResolver, $objectClassResolver, $defaultContext);

$this->propertyAccessor = $propertyAccessor ?: PropertyAccess::createPropertyAccessor();
$this->propertyListExtractor = $propertyListExtractor ?? new ReflectionExtractor();
}

/**
Expand All @@ -56,55 +61,11 @@ public function hasCacheableSupportsMethod(): bool
*/
protected function extractAttributes($object, $format = null, array $context = array())
{
// If not using groups, detect manually
$attributes = array();

// methods
$reflClass = new \ReflectionClass($object);
foreach ($reflClass->getMethods(\ReflectionMethod::IS_PUBLIC) as $reflMethod) {
if (
0 !== $reflMethod->getNumberOfRequiredParameters() ||
$reflMethod->isStatic() ||
$reflMethod->isConstructor() ||
$reflMethod->isDestructor()
) {
continue;
}

$name = $reflMethod->name;
$attributeName = null;

if (0 === strpos($name, 'get') || 0 === strpos($name, 'has')) {
// getters and hassers
$attributeName = substr($name, 3);

if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
} elseif (0 === strpos($name, 'is')) {
// issers
$attributeName = substr($name, 2);

if (!$reflClass->hasProperty($attributeName)) {
$attributeName = lcfirst($attributeName);
}
}

if (null !== $attributeName && $this->isAllowedAttribute($object, $attributeName, $format, $context)) {
$attributes[$attributeName] = true;
}
}

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

$attributes[$reflProperty->name] = true;
}
$properties = $this->propertyListExtractor->getProperties(get_class($object), ['exclude_static_properties' => true]);

return array_keys($attributes);
return array_filter($properties, function (string $attribute) use ($object, $format, $context) {
return $this->isAllowedAttribute($object, $attribute, $format, $context);
});
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,7 @@ public function testExtractAttributesRespectsFormat()
$data->setFoo('bar');
$data->bar = 'foo';

$this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, 'foo_and_bar_included'));
$this->assertSame(array('bar' => 'foo', 'foo' => 'bar'), $normalizer->normalize($data, 'foo_and_bar_included'));
}

public function testExtractAttributesRespectsContext()
Expand All @@ -878,7 +878,7 @@ public function testExtractAttributesRespectsContext()
$data->setFoo('bar');
$data->bar = 'foo';

$this->assertSame(array('foo' => 'bar', 'bar' => 'foo'), $normalizer->normalize($data, null, array('include_foo_and_bar' => true)));
$this->assertSame(array('bar' => 'foo', 'foo' => 'bar'), $normalizer->normalize($data, null, array('include_foo_and_bar' => true)));
}

public function testAttributesContextNormalize()
Expand Down
4 changes: 2 additions & 2 deletions < 9573 span class="Truncate"> src/Symfony/Component/Serializer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
],
"require": {
"php": "^7.1.3",
"symfony/polyfill-ctype": "~1.8"
"symfony/polyfill-ctype": "~1.8",
"symfony/property-info": "~4.2"
},
"require-dev": {
"symfony/yaml": "~3.4|~4.0",
"symfony/config": "~3.4|~4.0",
"symfony/property-access": "~3.4|~4.0",
"symfony/http-foundation": "~3.4|~4.0",
"symfony/cache": "~3.4|~4.0",
"symfony/property-info": "~3.4|~4.0",
"symfony/validator": "~3.4|~4.0",
"doctrine/annotations": "~1.0",
"symfony/dependency-injection": "~3.4|~4.0",
Expand Down
0