Skip to content

Commit 76c748c

Browse files
committed
[Serializer] provide new ObjectPropertyListExtractorInterface
1 parent fe7363f commit 76c748c

File tree

3 files changed

+110
-0
lines changed

3 files changed

+110
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Extractor;
13+
14+
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
15+
16+
/**
17+
* @author David Maicher <mail@dmaicher.de>
18+
*/
19+
class ObjectPropertyListExtractor implements ObjectPropertyListExtractorInterface
20+
{
21+
private $propertyListExtractor;
22+
private $objectClassResolver;
23+
24+
public function __construct(PropertyListExtractorInterface $propertyListExtractor, callable $objectClassResolver)
25+
{
26+
$this->propertyListExtractor = $propertyListExtractor;
27+
$this->objectClassResolver = $objectClassResolver;
28+
}
29+
30+
/**
31+
* {@inheritdoc}
32+
*/
33+
public function getProperties($object, array $context = [])
34+
{
35+
$class = ($this->objectClassResolver)($object);
36+
37+
return $this->propertyListExtractor->getProperties($class, $context);
38+
}
39+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Extractor;
13+
14+
/**
15+
* @author David Maicher <mail@dmaicher.de>
16+
*/
17+
interface ObjectPropertyListExtractorInterface
18+
{
19+
/**
20+
* Gets the list of properties available for the given object.
21+
*
22+
* @param object $object
23+
* @param array $context
24+
*
25+
* @return string[]|null
26+
*/
27+
public function getProperties($object, array $context = []);
28+
}
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
/*
4+
* This file is part of the Symfony package.
5+
*
6+
* (c) Fabien Potencier <fabien@symfony.com>
7+
*
8+
* For the full copyright and license information, please view the LICENSE
9+
* file that was distributed with this source code.
10+
*/
11+
12+
namespace Symfony\Component\Serializer\Tests\Extractor;
13+
14+
use PHPUnit\Framework\TestCase;
15+
use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
16+
use Symfony\Component\Serializer\Extractor\ObjectPropertyListExtractor;
17+
18+
class ObjectPropertyListExtractorTest extends TestCase
19+
{
20+
public function testGetProperties(): void
21+
{
22+
$object = new \stdClass();
23+
$classResolver = function ($objectArg) use ($object): string {
24+
$this->assertSame($object, $objectArg);
25+
26+
return 'foo';
27+
};
28+
29+
$context = ['bar' => true];
30+
$properties = ['prop1', 'prop2'];
31+
32+
$propertyListExtractor = $this->createMock(PropertyListExtractorInterface::class);
33+
$propertyListExtractor->expects($this->once())
34+
->method('getProperties')
35+
->with('foo', $context)
36+
->willReturn($properties);
37+
38+
$this->assertSame(
39+
$properties,
40+
(new ObjectPropertyListExtractor($propertyListExtractor, $classResolver))->getProperties($object, $context)
41+
);
42+
}
43+
}

0 commit comments

Comments
 (0)
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