Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 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
 
12
namespace Symfony\Component\HttpKernel\ControllerMetadata;
13
 
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
 
23
    private string $name;
24
    private ?string $type;
25
    private bool $isVariadic;
26
    private bool $hasDefaultValue;
27
    private mixed $defaultValue;
28
    private bool $isNullable;
29
    private array $attributes;
30
 
31
    /**
32
     * @param object[] $attributes
33
     */
34
    public function __construct(string $name, ?string $type, bool $isVariadic, bool $hasDefaultValue, mixed $defaultValue, bool $isNullable = false, array $attributes = [])
35
    {
36
        $this->name = $name;
37
        $this->type = $type;
38
        $this->isVariadic = $isVariadic;
39
        $this->hasDefaultValue = $hasDefaultValue;
40
        $this->defaultValue = $defaultValue;
41
        $this->isNullable = $isNullable || null === $type || ($hasDefaultValue && null === $defaultValue);
42
        $this->attributes = $attributes;
43
    }
44
 
45
    /**
46
     * Returns the name as given in PHP, $foo would yield "foo".
47
     */
48
    public function getName(): string
49
    {
50
        return $this->name;
51
    }
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
    public function getType(): ?string
59
    {
60
        return $this->type;
61
    }
62
 
63
    /**
64
     * Returns whether the argument is defined as "...$variadic".
65
     */
66
    public function isVariadic(): bool
67
    {
68
        return $this->isVariadic;
69
    }
70
 
71
    /**
72
     * Returns whether the argument has a default value.
73
     *
74
     * Implies whether an argument is optional.
75
     */
76
    public function hasDefaultValue(): bool
77
    {
78
        return $this->hasDefaultValue;
79
    }
80
 
81
    /**
82
     * Returns whether the argument accepts null values.
83
     */
84
    public function isNullable(): bool
85
    {
86
        return $this->isNullable;
87
    }
88
 
89
    /**
90
     * Returns the default value of the argument.
91
     *
92
     * @throws \LogicException if no default value is present; {@see self::hasDefaultValue()}
93
     */
94
    public function getDefaultValue(): mixed
95
    {
96
        if (!$this->hasDefaultValue) {
97
            throw new \LogicException(sprintf('Argument $%s does not have a default value. Use "%s::hasDefaultValue()" to avoid this exception.', $this->name, __CLASS__));
98
        }
99
 
100
        return $this->defaultValue;
101
    }
102
 
103
    /**
104
     * @param class-string          $name
105
     * @param self::IS_INSTANCEOF|0 $flags
106
     *
107
     * @return array<object>
108
     */
109
    public function getAttributes(string $name = null, int $flags = 0): array
110
    {
111
        if (!$name) {
112
            return $this->attributes;
113
        }
114
 
115
        return $this->getAttributesOfType($name, $flags);
116
    }
117
 
118
    /**
119
     * @template T of object
120
     *
121
     * @param class-string<T>       $name
122
     * @param self::IS_INSTANCEOF|0 $flags
123
     *
124
     * @return array<T>
125
     */
126
    public function getAttributesOfType(string $name, int $flags = 0): array
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 ($attribute::class === $name) {
138
                    $attributes[] = $attribute;
139
                }
140
            }
141
        }
142
 
143
        return $attributes;
144
    }
145
}