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