diff --git a/src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractor.php b/src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractor.php new file mode 100644 index 0000000000000..8424a2ae730df --- /dev/null +++ b/src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractor.php @@ -0,0 +1,39 @@ + + * + * 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 + */ +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); + } +} diff --git a/src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractorInterface.php b/src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractorInterface.php new file mode 100644 index 0000000000000..d422e79f82b84 --- /dev/null +++ b/src/Symfony/Component/Serializer/Extractor/ObjectPropertyListExtractorInterface.php @@ -0,0 +1,28 @@ + + * + * 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 + */ +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 = []); +} diff --git a/src/Symfony/Component/Serializer/Tests/Extractor/ObjectPropertyListExtractorTest.php b/src/Symfony/Component/Serializer/Tests/Extractor/ObjectPropertyListExtractorTest.php new file mode 100644 index 0000000000000..9701628e9008e --- /dev/null +++ b/src/Symfony/Component/Serializer/Tests/Extractor/ObjectPropertyListExtractorTest.php @@ -0,0 +1,61 @@ + + * + * 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