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\Routing\Requirement;
13
 
14
use Symfony\Component\Routing\Exception\InvalidArgumentException;
15
 
16
final class EnumRequirement implements \Stringable
17
{
18
    private string $requirement;
19
 
20
    /**
21
     * @template T of \BackedEnum
22
     *
23
     * @param class-string<T>|list<T> $cases
24
     */
25
    public function __construct(string|array $cases = [])
26
    {
27
        if (\is_string($cases)) {
28
            if (!is_subclass_of($cases, \BackedEnum::class, true)) {
29
                throw new InvalidArgumentException(sprintf('"%s" is not a "BackedEnum" class.', $cases));
30
            }
31
 
32
            $cases = $cases::cases();
33
        } else {
34
            $class = null;
35
 
36
            foreach ($cases as $case) {
37
                if (!$case instanceof \BackedEnum) {
38
                    throw new InvalidArgumentException(sprintf('Case must be a "BackedEnum" instance, "%s" given.', get_debug_type($case)));
39
                }
40
 
41
                $class ??= $case::class;
42
 
43
                if (!$case instanceof $class) {
44
                    throw new InvalidArgumentException(sprintf('"%s::%s" is not a case of "%s".', get_debug_type($case), $case->name, $class));
45
                }
46
            }
47
        }
48
 
49
        $this->requirement = implode('|', array_map(static fn ($e) => preg_quote($e->value), $cases));
50
    }
51
 
52
    public function __toString(): string
53
    {
54
        return $this->requirement;
55
    }
56
}