ArgumentMetadata.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. * The type is the PHP class in 5.5+ and additionally the basic type in PHP 7.0+.
  45. */
  46. public function getType(): ?string
  47. {
  48. return $this->type;
  49. }
  50. /**
  51. * Returns whether the argument is defined as "...$variadic".
  52. */
  53. public function isVariadic(): bool
  54. {
  55. return $this->isVariadic;
  56. }
  57. /**
  58. * Returns whether the argument has a default value.
  59. *
  60. * Implies whether an argument is optional.
  61. */
  62. public function hasDefaultValue(): bool
  63. {
  64. return $this->hasDefaultValue;
  65. }
  66. /**
  67. * Returns whether the argument accepts null values.
  68. */
  69. public function isNullable(): bool
  70. {
  71. return $this->isNullable;
  72. }
  73. /**
  74. * Returns the default value of the argument.
  75. *
  76. * @throws \LogicException if no default value is present; {@see self::hasDefaultValue()}
  77. */
  78. public function getDefaultValue(): mixed
  79. {
  80. if (!$this->hasDefaultValue) {
  81. throw new \LogicException(\sprintf('Argument $%s does not have a default value. Use "%s::hasDefaultValue()" to avoid this exception.', $this->name, __CLASS__));
  82. }
  83. return $this->defaultValue;
  84. }
  85. /**
  86. * @param class-string $name
  87. * @param self::IS_INSTANCEOF|0 $flags
  88. *
  89. * @return array<object>
  90. */
  91. public function getAttributes(?string $name = null, int $flags = 0): array
  92. {
  93. if (!$name) {
  94. return $this->attributes;
  95. }
  96. return $this->getAttributesOfType($name, $flags);
  97. }
  98. /**
  99. * @template T of object
  100. *
  101. * @param class-string<T> $name
  102. * @param self::IS_INSTANCEOF|0 $flags
  103. *
  104. * @return array<T>
  105. */
  106. public function getAttributesOfType(string $name, int $flags = 0): array
  107. {
  108. $attributes = [];
  109. if ($flags & self::IS_INSTANCEOF) {
  110. foreach ($this->attributes as $attribute) {
  111. if ($attribute instanceof $name) {
  112. $attributes[] = $attribute;
  113. }
  114. }
  115. } else {
  116. foreach ($this->attributes as $attribute) {
  117. if ($attribute::class === $name) {
  118. $attributes[] = $attribute;
  119. }
  120. }
  121. }
  122. return $attributes;
  123. }
  124. public function getControllerName(): string
  125. {
  126. return $this->controllerName;
  127. }
  128. }