You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

163 lines
4.2 KiB

3 years ago
  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. use Symfony\Component\HttpKernel\Attribute\ArgumentInterface;
  12. /**
  13. * Responsible for storing metadata of an argument.
  14. *
  15. * @author Iltar van der Berg <kjarli@gmail.com>
  16. */
  17. class ArgumentMetadata
  18. {
  19. public const IS_INSTANCEOF = 2;
  20. private $name;
  21. private $type;
  22. private $isVariadic;
  23. private $hasDefaultValue;
  24. private $defaultValue;
  25. private $isNullable;
  26. private $attributes;
  27. /**
  28. * @param object[] $attributes
  29. */
  30. public function __construct(string $name, ?string $type, bool $isVariadic, bool $hasDefaultValue, $defaultValue, bool $isNullable = false, $attributes = [])
  31. {
  32. $this->name = $name;
  33. $this->type = $type;
  34. $this->isVariadic = $isVariadic;
  35. $this->hasDefaultValue = $hasDefaultValue;
  36. $this->defaultValue = $defaultValue;
  37. $this->isNullable = $isNullable || null === $type || ($hasDefaultValue && null === $defaultValue);
  38. if (null === $attributes || $attributes instanceof ArgumentInterface) {
  39. trigger_deprecation('symfony/http-kernel', '5.3', 'The "%s" constructor expects an array of PHP attributes as last argument, %s given.', __CLASS__, get_debug_type($attributes));
  40. $attributes = $attributes ? [$attributes] : [];
  41. }
  42. $this->attributes = $attributes;
  43. }
  44. /**
  45. * Returns the name as given in PHP, $foo would yield "foo".
  46. *
  47. * @return string
  48. */
  49. public function getName()
  50. {
  51. return $this->name;
  52. }
  53. /**
  54. * Returns the type of the argument.
  55. *
  56. * The type is the PHP class in 5.5+ and additionally the basic type in PHP 7.0+.
  57. *
  58. * @return string|null
  59. */
  60. public function getType()
  61. {
  62. return $this->type;
  63. }
  64. /**
  65. * Returns whether the argument is defined as "...$variadic".
  66. *
  67. * @return bool
  68. */
  69. public function isVariadic()
  70. {
  71. return $this->isVariadic;
  72. }
  73. /**
  74. * Returns whether the argument has a default value.
  75. *
  76. * Implies whether an argument is optional.
  77. *
  78. * @return bool
  79. */
  80. public function hasDefaultValue()
  81. {
  82. return $this->hasDefaultValue;
  83. }
  84. /**
  85. * Returns whether the argument accepts null values.
  86. *
  87. * @return bool
  88. */
  89. public function isNullable()
  90. {
  91. return $this->isNullable;
  92. }
  93. /**
  94. * Returns the default value of the argument.
  95. *
  96. * @throws \LogicException if no default value is present; {@see self::hasDefaultValue()}
  97. *
  98. * @return mixed
  99. */
  100. public function getDefaultValue()
  101. {
  102. if (!$this->hasDefaultValue) {
  103. throw new \LogicException(sprintf('Argument $%s does not have a default value. Use "%s::hasDefaultValue()" to avoid this exception.', $this->name, __CLASS__));
  104. }
  105. return $this->defaultValue;
  106. }
  107. /**
  108. * Returns the attribute (if any) that was set on the argument.
  109. */
  110. public function getAttribute(): ?ArgumentInterface
  111. {
  112. trigger_deprecation('symfony/http-kernel', '5.3', 'Method "%s()" is deprecated, use "getAttributes()" instead.', __METHOD__);
  113. if (!$this->attributes) {
  114. return null;
  115. }
  116. return $this->attributes[0] instanceof ArgumentInterface ? $this->attributes[0] : null;
  117. }
  118. /**
  119. * @return object[]
  120. */
  121. public function getAttributes(string $name = null, int $flags = 0): array
  122. {
  123. if (!$name) {
  124. return $this->attributes;
  125. }
  126. $attributes = [];
  127. if ($flags & self::IS_INSTANCEOF) {
  128. foreach ($this->attributes as $attribute) {
  129. if ($attribute instanceof $name) {
  130. $attributes[] = $attribute;
  131. }
  132. }
  133. } else {
  134. foreach ($this->attributes as $attribute) {
  135. if (\get_class($attribute) === $name) {
  136. $attributes[] = $attribute;
  137. }
  138. }
  139. }
  140. return $attributes;
  141. }
  142. }