File tree Expand file tree Collapse file tree 3 files changed +110
-0
lines changed
src/Symfony/Component/Serializer Expand file tree Collapse file tree 3 files changed +110
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments