ConstructorArgumentTypeExtractorInterface.php
1 year ago
ConstructorExtractor.php
1 year ago
PhpDocExtractor.php
1 year ago
PhpStanExtractor.php
1 year ago
ReflectionExtractor.php
1 year ago
SerializerExtractor.php
1 year ago
SerializerExtractor.php
59 lines
| 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\PropertyInfo\Extractor; |
| 13 | |
| 14 | use Symfony\Component\PropertyInfo\PropertyListExtractorInterface; |
| 15 | use 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 | */ |
| 24 | class 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 (!\array_key_exists('serializer_groups', $context) || (null !== $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($serializerAttributeMetadata, 'isIgnored') && $serializerAttributeMetadata->isIgnored(); |
| 51 | if (!$ignored && (null === $context['serializer_groups'] || array_intersect($context['serializer_groups'], $serializerAttributeMetadata->getGroups()))) { |
| 52 | $properties[] = $serializerAttributeMetadata->getName(); |
| 53 | } |
| 54 | } |
| 55 | |
| 56 | return $properties; |
| 57 | } |
| 58 | } |
| 59 |