ArgumentMetadata.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. * @return object[]
  92. */
  93. public function getAttributes(string $name = null, int $flags = 0): array
  94. {
  95. if (!$name) {
  96. return $this->attributes;
  97. }
  98. $attributes = [];
  99. if ($flags & self::IS_INSTANCEOF) {
  100. foreach ($this->attributes as $attribute) {
  101. if ($attribute instanceof $name) {
  102. $attributes[] = $attribute;
  103. }
  104. }
  105. } else {
  106. foreach ($this->attributes as $attribute) {
  107. if (\get_class($attribute) === $name) {
  108. $attributes[] = $attribute;
  109. }
  110. }
  111. }
  112. return $attributes;
  113. }
  114. }