ArgumentMetadata.php 2.7 KB

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