Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit adba41a

Browse filesBrowse files
committed
feature #30904 [Serializer] provide new ObjectPropertyListExtractorInterface (dmaicher)
This PR was merged into the 4.3-dev branch. Discussion ---------- [Serializer] provide new ObjectPropertyListExtractorInterface | Q | A | ------------- | --- | Branch? | master | Bug fix? | no | New feature? | yes | BC breaks? | no | Deprecations? | no | Tests pass? | yes | Fixed tickets | related to #30818 | License | MIT | Doc PR | - EUFOSSA Hackathon As discussed with @joelwurtz this adds a new `ObjectPropertyListExtractorInterface` and a default implementation. See #30818 > A new interface will be provided ObjectPropertyListExtractorInterface (name can change), that allow getting attributes based on an object. A default implementation will be provided that use the PropertyListExtractorInterface and a class resolver, as the latter one only rely on class name, it may be important to have this distinction (this will allow features that rely on a specific value of the object to get the current property list, like exclusion policy) Commits ------- 997270f [Serializer] provide new ObjectPropertyListExtractorInterface
2 parents e2e38de + 997270f commit adba41a
Copy full SHA for adba41a

File tree

Expand file treeCollapse file tree

3 files changed

+128
-0
lines changed
Open diff view settings
Filter options
Expand file treeCollapse file tree

3 files changed

+128
-0
lines changed
Open diff view settings
Collapse file
+39Lines 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 = null)
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 ? ($this->objectClassResolver)($object) : \get_class($object);
36+
37+
return $this->propertyListExtractor->getProperties($class, $context);
38+
}
39+
}
Collapse file
+28Lines 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+
}
Collapse file
+61Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
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 testGetPropertiesWithoutObjectClassResolver(): void
21+
{
22+
$object = new \stdClass();
23+
$context = ['bar' => true];
24+
$properties = ['prop1', 'prop2'];
25+
26+
$propertyListExtractor = $this->createMock(PropertyListExtractorInterface::class);
27+
$propertyListExtractor->expects($this->once())
28+
->method('getProperties')
29+
->with(\get_class($object), $context)
30+
->willReturn($properties);
31+
32+
$this->assertSame(
33+
$properties,
34+
(new ObjectPropertyListExtractor($propertyListExtractor))->getProperties($object, $context)
35+
);
36+
}
37+
38+
public function testGetPropertiesWithObjectClassResolver(): void
39+
{
40+
$object = new \stdClass();
41+
$classResolver = function ($objectArg) use ($object): string {
42+
$this->assertSame($object, $objectArg);
43+
44+
return 'foo';
45+
};
46+
47+
$context = ['bar' => true];
48+
$properties = ['prop1', 'prop2'];
49+
50+
$propertyListExtractor = $this->createMock(PropertyListExtractorInterface::class);
51+
$propertyListExtractor->expects($this->once())
52+
->method('getProperties')
53+
->with('foo', $context)
54+
->willReturn($properties);
55+
56+
$this->assertSame(
57+
$properties,
58+
(new ObjectPropertyListExtractor($propertyListExtractor, $classResolver))->getProperties($object, $context)
59+
);
60+
}
61+
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.