Code Coverage
 
Classes and Traits
Functions and Methods
Lines
Total
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
1 / 2
CRAP
84.62% covered (warning)
84.62%
11 / 13
SerializerExtractor
0.00% covered (danger)
0.00%
0 / 1
50.00% covered (danger)
50.00%
1 / 2
9.29
84.62% covered (warning)
84.62%
11 / 13
 __construct
100.00% covered (success)
100.00%
1 / 1
1
100.00% covered (success)
100.00%
2 / 2
 getProperties
0.00% covered (danger)
0.00%
0 / 1
8.38
81.82% covered (warning)
81.82%
9 / 11
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
12namespace Symfony\Component\PropertyInfo\Extractor;
13
14use Symfony\Component\PropertyInfo\PropertyListExtractorInterface;
15use Symfony\Component\Serializer\Mapping\Factory\ClassMetadataFactoryInterface;
16
17/**
18 * Lists available properties using Symfony Serializer Component metadata.
19 *
20 * @author K√©vin Dunglas <dunglas@gmail.com>
21 *
22 * @final
23 */
24class SerializerExtractor implements PropertyListExtractorInterface
25{
26    private $classMetadataFactory;
27
28    public function __construct(ClassMetadataFactoryInterface $classMetadataFactory)
29    {
30        $this->classMetadataFactory = $classMetadataFactory;
31    }
32
33    /**
34     * {@inheritdoc}
35     */
36    public function getProperties(string $class, array $context = []): ?array
37    {
38        if (!isset($context['serializer_groups']) || !\is_array($context['serializer_groups'])) {
39            return null;
40        }
41
42        if (!$this->classMetadataFactory->getMetadataFor($class)) {
43            return null;
44        }
45
46        $properties = [];
47        $serializerClassMetadata = $this->classMetadataFactory->getMetadataFor($class);
48
49        foreach ($serializerClassMetadata->getAttributesMetadata() as $serializerAttributeMetadata) {
50            $ignored = method_exists($serializerClassMetadata, 'isIgnored') && $serializerAttributeMetadata->isIgnored();
51            if (!$ignored && array_intersect($context['serializer_groups'], $serializerAttributeMetadata->getGroups())) {
52                $properties[] = $serializerAttributeMetadata->getName();
53            }
54        }
55
56        return $properties;
57    }
58}