8000 [Serializer] Properties extractor implementations by joelwurtz · Pull Request #30980 · symfony/symfony · GitHub
[go: up one dir, main page]

Skip to content

[Serializer] Properties extractor implementations #30980

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
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
Add a group property list extractor
  • Loading branch information
joelwurtz committed Apr 8, 2019
commit fc2508c84db23bf9baacb8373eaa1980b87e438d
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Extractor;

use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;

/**
* Filter properties given a specific set of groups
*
* @author Joel Wurtz <joel.wurtz@gmail.com>
*/
class GroupPropertyListExtractor implements PropertyListExtractorInterface
{
public const GROUPS = 'groups';

private $classMetadataFactory;

private $extractor;

public function __construct(ClassMetadataFactoryInterface $classMetadataFactory, PropertyListExtractorInterface $extractor = null)
{
$this->extractor = $extractor;
$this->classMetadataFactory = $classMetadataFactory;
}

public function getProperties($class, array $context = [])
{
$properties = null;

if (null !== $this->extractor) {
$properties = $this->extractor->getProperties($class, $context);

if (null === $properties) {
return null;
}
}

$groups = $context[self::GROUPS] ?? null;
$groups = (\is_array($groups) || is_scalar($groups)) ? (array) $groups : false;
$groupProperties = [];

foreach ($this->classMetadataFactory->getMetadataFor($class)->getAttributesMetadata() as $attributeMetadata) {
$name = $attributeMetadata->getName();

if (false === $groups || array_intersect($attributeMetadata->getGroups(), $groups)) {
$groupProperties[] = $name;
}
}

if (null === $properties) {
return $groupProperties;
}

return array_intersect($properties, $groupProperties);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,157 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\Serializer\Tests\Extractor;

use Doctrine\Common\Annotations\AnnotationReader;
use PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\Extractor\ReflectionExtractor;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\Serializer\Annotation\Groups;
use Symfony\Component\Serializer\Extractor\GroupPropertyListExtractor;
use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactory;
use Symfony\Component\Serializer\Mapping\Loader\AnnotationLoader;

class GroupPropertyListExtractorTest extends TestCase
{
public function testNoGroups()
{
$extractor = new ReflectionExtractor();
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$groupExtractor = new GroupPropertyListExtractor($factory, $extractor);

$properties = $groupExtractor->getProperties(DummyNoGroups::class);

$this->assertInternalType('array', $properties);
$this->assertContains('foo', $properties);
$this->assertContains('bar', $properties);
$this->assertContains('baz', $properties);

$properties = $groupExtractor->getProperties(DummyNoGroups::class, [
GroupPropertyListExtractor::GROUPS => ['dummy']
]);

$this->assertInternalType('array', $properties);
$this->assertEmpty($properties);
}

public function testGroups()
{
$extractor = new ReflectionExtractor();
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$groupExtractor = new GroupPropertyListExtractor($factory, $extractor);

$properties = $groupExtractor->getProperties(DummyGroups::class);

$this->assertInternalType('array', $properties);
$this->assertContains('foo', $properties);
$this->assertContains('bar', $properties);
$this->assertContains('baz', $properties);

$properties = $groupExtractor->getProperties(DummyGroups::class, [
GroupPropertyListExtractor::GROUPS => ['dummy']
]);

$this->assertInternalType('array', $properties);
$this->assertEmpty($properties);

$properties = $groupExtractor->getProperties(DummyGroups::class, [
GroupPropertyListExtractor::GROUPS => ['foo']
]);

$this->assertInternalType('array', $properties);
$this->assertContains('foo', $properties);
$this->assertNotContains('bar', $properties);
$this->assertNotContains('baz', $properties);

$properties = $groupExtractor->getProperties(DummyGroups::class, [
GroupPropertyListExtractor::GROUPS => ['foo', 'bar']
]);

$this->assertInternalType('array', $properties);
$this->assertContains('foo', $properties);
$this->assertContains('bar', $properties);
$this->assertContains('baz', $properties);

$properties = $groupExtractor->getProperties(DummyGroups::class, [
GroupPropertyListExtractor::GROUPS => ['bar']
]);

$this->assertInternalType('array', $properties);
$this->assertNotContains('foo', $properties);
$this->assertContains('bar', $properties);
$this->assertContains('baz', $properties);

$properties = $groupExtractor->getProperties(DummyGroups::class, [
GroupPropertyListExtractor::GROUPS => ['baz']
]);

$this->assertInternalType('array', $properties);
$this->assertNotContains('foo', $properties);
$this->assertNotContains('bar', $properties);
$this->assertContains('baz', $properties);
}

public function testNullProperties()
{
$extractor = $this
->getMockBuilder(PropertyListExtractorInterface::class)
->getMock()
;
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));

$extractor->method('getProperties')->willReturn(null);

$ignored = new GroupPropertyListExtractor($factory, $extractor);
$properties = $ignored->getProperties(DummyNoGroups::class);

$this->assertNull($properties);
}

public function testNoExtractor()
{
$factory = new ClassMetadataFactory(new AnnotationLoader(new AnnotationReader()));
$ignored = new GroupPropertyListExtractor($factory);
$properties = $ignored->getProperties(DummyNoGroups::class);

$this->assertInternalType('array', $properties);
$this->assertContains('foo', $properties);
$this->assertContains('bar', $properties);
$this->assertContains('baz', $properties);
}
}

class DummyNoGroups
{
public $foo;

public $bar;

public $baz;
}

class DummyGroups
{
/**
* @Groups("foo")
*/
public $foo;

/**
* @Groups({"bar"})
*/
public $bar;

/**
* @Groups({"bar", "baz"})
*/
public $baz;
}
0