ArgumentMetadata.php 3.7 KB

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