Skip to content

[Serializer] provide new ObjectPropertyListExtractorInterface #30904

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

Merged
merged 1 commit into from
Apr 6, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?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;

/**
* @author David Maicher <mail@dmaicher.de>
*/
class ObjectPropertyListExtractor implements ObjectPropertyListExtractorInterface
{
private $propertyListExtractor;
private $objectClassResolver;

public function __construct(PropertyListExtractorInterface $propertyListExtractor, ?callable $objectClassResolver = null)
{
$this->propertyListExtractor = $propertyListExtractor;
$this->objectClassResolver = $objectClassResolver;
}

/**
* {@inheritdoc}
*/
public function getProperties($object, array $context = [])
{
$class = $this->objectClassResolver ? ($this->objectClassResolver)($object) : \get_class($object);

return $this->propertyListExtractor->getProperties($class, $context);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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;

/**
* @author David Maicher <mail@dmaicher.de>
*/
interface ObjectPropertyListExtractorInterface
{
/**
* Gets the list of properties available for the given object.
*
* @param object $object
* @param array $context
*
* @return string[]|null
*/
public function getProperties($object, array $context = []);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?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 PHPUnit\Framework\TestCase;
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
use Symfony\Component\Serializer\Extractor\ObjectPropertyListExtractor;

class ObjectPropertyListExtractorTest extends TestCase
{
public function testGetPropertiesWithoutObjectClassResolver(): void
{
$object = new \stdClass();
$context = ['bar' => true];
$properties = ['prop1', 'prop2'];

$propertyListExtractor = $this->createMock(PropertyListExtractorInterface::class);
$propertyListExtractor->expects($this->once())
->method('getProperties')
->with(\get_class($object), $context)
->willReturn($properties);

$this->assertSame(
$properties,
(new ObjectPropertyListExtractor($propertyListExtractor))->getProperties($object, $context)
);
}

public function testGetPropertiesWithObjectClassResolver(): void
{
$object = new \stdClass();
$classResolver = function ($objectArg) use ($object): string {
$this->assertSame($object, $objectArg);

return 'foo';
};

$context = ['bar' => true];
$properties = ['prop1', 'prop2'];

$propertyListExtractor = $this->createMock(PropertyListExtractorInterface::class);
$propertyListExtractor->expects($this->once())
->method('getProperties')
->with('foo', $context)
->willReturn($properties);

$this->assertSame(
$properties,
(new ObjectPropertyListExtractor($propertyListExtractor, $classResolver))->getProperties($object, $context)
);
}
}
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy