ArgumentMetadata.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.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. namespace Symfony\Component\HttpKernel\ControllerMetadata;
  11. /**
  12. * Responsible for storing metadata of an argument.
  13. *
  14. * @author Iltar van der Berg <kjarli@gmail.com>
  15. */
  16. class ArgumentMetadata
  17. {
  18. public const IS_INSTANCEOF = 2;
  19. /**
  20. * @param object[] $attributes
  21. */
  22. public function __construct(
  23. private string $name,
  24. private ?string $type,
  25. private bool $isVariadic,
  26. private bool $hasDefaultValue,
  27. private mixed $defaultValue,
  28. private bool $isNullable = false,
  29. private array $attributes = [],
  30. private string $controllerName = 'n/a',
  31. ) {
  32. $this->isNullable = $isNullable || null === $type || ($hasDefaultValue && null === $defaultValue);
  33. }
  34. /**
  35. * Returns the name as given in PHP, $foo would yield "foo".
  36. */
  37. public function getName(): string
  38. {
  39. return $this->name;
  40. }
  41. /**
  42. * Returns the type of the argument.
  43. */
  44. public function getType(): ?string
  45. {
  46. return $this->type;
  47. }
  48. /**
  49. * Returns whether the argument is defined as "...$variadic".
  50. */
  51. public function isVariadic(): bool
  52. {
  53. return $this->isVariadic;
  54. }
  55. /**
  56. * Returns whether the argument has a default value.
  57. *
  58. * Implies whether an argument is optional.
  59. */
  60. public function hasDefaultValue(): bool
  61. {
  62. return $this->hasDefaultValue;
  63. }
  64. /**
  65. * Returns whether the argument accepts null values.
  66. */
  67. public function isNullable(): bool
  68. {
  69. return $this->isNullable;
  70. }
  71. /**
  72. * Returns the default value of the argument.
  73. *
  74. * @throws \LogicException if no default value is present; {@see self::hasDefaultValue()}
  75. */
  76. public function getDefaultValue(): mixed
  77. {
  78. if (!$this->hasDefaultValue) {
  79. throw new \LogicException(\sprintf('Argument $%s does not have a default value. Use "%s::hasDefaultValue()" to avoid this exception.', $this->name, __CLASS__));
  80. }
  81. return $this->defaultValue;
  82. }
  83. /**
  84. * @param class-string $name
  85. * @param self::IS_INSTANCEOF|0 $flags
  86. *
  87. * @return array<object>
  88. */
  89. public function getAttributes(?string $name = null, int $flags = 0): array
  90. {
  91. if (!$name) {
  92. return $this->attributes;
  93. }
  94. return $this->getAttributesOfType($name, $flags);
  95. }
  96. /**
  97. * @template T of object
  98. *
  99. * @param class-string<T> $name
  100. * @param self::IS_INSTANCEOF|0 $flags
  101. *
  102. * @return array<T>
  103. */
  104. public function getAttributesOfType(string $name, int $flags = 0): array
  105. {
  106. $attributes = [];
  107. if ($flags & self::IS_INSTANCEOF) {
  108. foreach ($this->attributes as $attribute) {
  109. if ($attribute instanceof $name) {
  110. $attributes[] = $attribute;
  111. }
  112. }
  113. } else {
  114. foreach ($this->attributes as $attribute) {
  115. if ($attribute::class === $name) {
  116. $attributes[] = $attribute;
  117. }
  118. }
  119. }
  120. return $attributes;
  121. }
  122. public function getControllerName(): string
  123. {
  124. return $this->controllerName;
  125. }
  126. }