vendor/api-platform/core/src/Core/Metadata/Property/Factory/DefaultPropertyMetadataFactory.php line 26

Open in your IDE?
  1. <?php
  2. /*
  3. * This file is part of the API Platform project.
  4. *
  5. * (c) Kévin Dunglas <dunglas@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. declare(strict_types=1);
  11. namespace ApiPlatform\Core\Metadata\Property\Factory;
  12. use ApiPlatform\Core\Exception\PropertyNotFoundException;
  13. use ApiPlatform\Core\Metadata\Property\PropertyMetadata;
  14. /**
  15. * Populates defaults values of the ressource properties using the default PHP values of properties.
  16. */
  17. final class DefaultPropertyMetadataFactory implements PropertyMetadataFactoryInterface
  18. {
  19. private $decorated;
  20. public function __construct(PropertyMetadataFactoryInterface $decorated = null)
  21. {
  22. $this->decorated = $decorated;
  23. }
  24. public function create(string $resourceClass, string $property, array $options = []): PropertyMetadata
  25. {
  26. if (null === $this->decorated) {
  27. $propertyMetadata = new PropertyMetadata();
  28. } else {
  29. try {
  30. $propertyMetadata = $this->decorated->create($resourceClass, $property, $options);
  31. } catch (PropertyNotFoundException $propertyNotFoundException) {
  32. $propertyMetadata = new PropertyMetadata();
  33. }
  34. }
  35. try {
  36. $reflectionClass = new \ReflectionClass($resourceClass);
  37. } catch (\ReflectionException $reflectionException) {
  38. return $propertyMetadata;
  39. }
  40. $defaultProperties = $reflectionClass->getDefaultProperties();
  41. if (!\array_key_exists($property, $defaultProperties) || null === ($defaultProperty = $defaultProperties[$property])) {
  42. return $propertyMetadata;
  43. }
  44. return $propertyMetadata->withDefault($defaultProperty);
  45. }
  46. }