4 * This file is part of the Symfony package.
6 * (c) Fabien Potencier <fabien@symfony.com>
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
12 namespace Symfony\Component\HttpKernel\ControllerMetadata;
15 * Responsible for storing metadata of an argument.
17 * @author Iltar van der Berg <kjarli@gmail.com>
19 class ArgumentMetadata
24 private $hasDefaultValue;
25 private $defaultValue;
31 * @param bool $isVariadic
32 * @param bool $hasDefaultValue
33 * @param mixed $defaultValue
34 * @param bool $isNullable
36 public function __construct($name, $type, $isVariadic, $hasDefaultValue, $defaultValue, $isNullable = false)
40 $this->isVariadic = $isVariadic;
41 $this->hasDefaultValue = $hasDefaultValue;
42 $this->defaultValue = $defaultValue;
43 $this->isNullable = $isNullable || null === $type || ($hasDefaultValue && null === $defaultValue);
47 * Returns the name as given in PHP, $foo would yield "foo".
51 public function getName()
57 * Returns the type of the argument.
59 * The type is the PHP class in 5.5+ and additionally the basic type in PHP 7.0+.
63 public function getType()
69 * Returns whether the argument is defined as "...$variadic".
73 public function isVariadic()
75 return $this->isVariadic;
79 * Returns whether the argument has a default value.
81 * Implies whether an argument is optional.
85 public function hasDefaultValue()
87 return $this->hasDefaultValue;
91 * Returns whether the argument accepts null values.
95 public function isNullable()
97 return $this->isNullable;
101 * Returns the default value of the argument.
103 * @throws \LogicException if no default value is present; {@see self::hasDefaultValue()}
107 public function getDefaultValue()
109 if (!$this->hasDefaultValue) {
110 throw new \LogicException(sprintf('Argument $%s does not have a default value. Use %s::hasDefaultValue() to avoid this exception.', $this->name, __CLASS__));
113 return $this->defaultValue;