ArgumentMetadata.php 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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. private $name;
  20. private $type;
  21. private $isVariadic;
  22. private $hasDefaultValue;
  23. private $defaultValue;
  24. private $isNullable;
  25. private $attribute;
  26. public function __construct(string $name, ?string $type, bool $isVariadic, bool $hasDefaultValue, $defaultValue, bool $isNullable = false, ?ArgumentInterface $attribute = null)
  27. {
  28. $this->name = $name;
  29. $this->type = $type;
  30. $this->isVariadic = $isVariadic;
  31. $this->hasDefaultValue = $hasDefaultValue;
  32. $this->defaultValue = $defaultValue;
  33. $this->isNullable = $isNullable || null === $type || ($hasDefaultValue && null === $defaultValue);
  34. $this->attribute = $attribute;
  35. }
  36. /**
  37. * Returns the name as given in PHP, $foo would yield "foo".
  38. *
  39. * @return string
  40. */
  41. public function getName()
  42. {
  43. return $this->name;
  44. }
  45. /**
  46. * Returns the type of the argument.
  47. *
  48. * The type is the PHP class in 5.5+ and additionally the basic type in PHP 7.0+.
  49. *
  50. * @return string|null
  51. */
  52. public function getType()
  53. {
  54. return $this->type;
  55. }
  56. /**
  57. * Returns whether the argument is defined as "...$variadic".
  58. *
  59. * @return bool
  60. */
  61. public function isVariadic()
  62. {
  63. return $this->isVariadic;
  64. }
  65. /**
  66. * Returns whether the argument has a default value.
  67. *
  68. * Implies whether an argument is optional.
  69. *
  70. * @return bool
  71. */
  72. public function hasDefaultValue()
  73. {
  74. return $this->hasDefaultValue;
  75. }
  76. /**
  77. * Returns whether the argument accepts null values.
  78. *
  79. * @return bool
  80. */
  81. public function isNullable()
  82. {
  83. return $this->isNullable;
  84. }
  85. /**
  86. * Returns the default value of the argument.
  87. *
  88. * @throws \LogicException if no default value is present; {@see self::hasDefaultValue()}
  89. *
  90. * @return mixed
  91. */
  92. public function getDefaultValue()
  93. {
  94. if (!$this->hasDefaultValue) {
  95. throw new \LogicException(sprintf('Argument $%s does not have a default value. Use "%s::hasDefaultValue()" to avoid this exception.', $this->name, __CLASS__));
  96. }
  97. return $this->defaultValue;
  98. }
  99. /**
  100. * Returns the attribute (if any) that was set on the argument.
  101. */
  102. public function getAttribute(): ?ArgumentInterface
  103. {
  104. return $this->attribute;
  105. }
  106. }