1 | <?php |
||
2 | |||
3 | declare(strict_types=1); |
||
4 | |||
5 | namespace DBUnt1tled\VO\VObjects\Identity; |
||
6 | |||
7 | use DBUnt1tled\VO\Exception\InvalidVOArgumentException; |
||
8 | use DBUnt1tled\VO\VObjects\Scalar\Strings; |
||
9 | use DBUnt1tled\VO\VObjects\ValueObjectComplex; |
||
10 | use DBUnt1tled\VO\VObjects\ValueObjectInterface; |
||
11 | |||
12 | class Uuid extends ValueObjectComplex |
||
13 | { |
||
14 | /** @var null|string */ |
||
15 | protected $version; |
||
16 | |||
17 | /** |
||
18 | * UUID NIL & version binary masks |
||
19 | */ |
||
20 | public const UUID_VALID = 'valid'; |
||
21 | public const UUID_NIL = 'nil'; |
||
22 | public const UUID_V1 = 'v1'; |
||
23 | public const UUID_V2 = 'v2'; |
||
24 | public const UUID_V3 = 'v3'; |
||
25 | public const UUID_V4 = 'v4'; |
||
26 | public const UUID_V5 = 'v5'; |
||
27 | |||
28 | /** |
||
29 | * @var array |
||
30 | */ |
||
31 | protected $regexes = [ |
||
32 | self::UUID_NIL => '/^[0]{8}-[0]{4}-[0]{4}-[0]{4}-[0]{12}$/i', |
||
33 | self::UUID_V1 => '/^[0-9A-F]{8}-[0-9A-F]{4}-1[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i', |
||
34 | self::UUID_V2 => '/^[0-9A-F]{8}-[0-9A-F]{4}-2[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i', |
||
35 | self::UUID_V3 => '/^[0-9A-F]{8}-[0-9A-F]{4}-3[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i', |
||
36 | self::UUID_V4 => '/^[0-9A-F]{8}-[0-9A-F]{4}-4[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i', |
||
37 | self::UUID_V5 => '/^[0-9A-F]{8}-[0-9A-F]{4}-5[0-9A-F]{3}-[89AB][0-9A-F]{3}-[0-9A-F]{12}$/i', |
||
38 | self::UUID_VALID => '/^[0-9A-F]{8}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{4}-[0-9A-F]{12}$/i', |
||
39 | ]; |
||
40 | |||
41 | /** |
||
42 | * An array of all validation regexes. |
||
43 | * |
||
44 | * @var array |
||
45 | */ |
||
46 | protected $versionNames = [ |
||
47 | self::UUID_V1, |
||
48 | self::UUID_V2, |
||
49 | self::UUID_V3, |
||
50 | self::UUID_V4, |
||
51 | self::UUID_V5, |
||
52 | self::UUID_VALID, |
||
53 | self::UUID_NIL, |
||
54 | ]; |
||
55 | |||
56 | /** |
||
57 | * Uuid constructor. |
||
58 | * @param Strings $uuid |
||
59 | * @param string|null $version |
||
60 | * @throws \ReflectionException |
||
61 | */ |
||
62 | 2 | public function __construct(Strings $uuid, string $version = null) |
|
63 | { |
||
64 | 2 | $this->guard($uuid, $version); |
|
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
65 | 1 | $this->value = $uuid; |
|
66 | 1 | } |
|
67 | |||
68 | /** |
||
69 | * @param mixed $value |
||
70 | * @param null $vesion |
||
0 ignored issues
–
show
|
|||
71 | * @param mixed ...$other |
||
72 | * @throws \ReflectionException |
||
73 | */ |
||
74 | 2 | public function guard($value, $vesion = null, ...$other): void |
|
75 | { |
||
76 | /** @var ValueObjectInterface $value*/ |
||
77 | 2 | parent::guard($value); |
|
78 | 2 | if (is_string($vesion) && in_array($vesion, $this->versionNames, true)) { |
|
0 ignored issues
–
show
|
|||
79 | 2 | if (true === (bool)preg_match($this->regexes[$vesion], $value->getValue())) { |
|
80 | 1 | $this->version = $vesion; |
|
81 | 1 | return; |
|
82 | } |
||
83 | 1 | throw new InvalidVOArgumentException(sprintf('Value is not a valid uuid %s.', $vesion), $value); |
|
84 | } |
||
85 | 2 | foreach ($this->regexes as $ver => $regex) { |
|
86 | 2 | if (true === (bool)preg_match($regex, $value->getValue())) { |
|
87 | 1 | $this->version = $ver; |
|
88 | 2 | return; |
|
89 | } |
||
90 | } |
||
91 | 1 | throw new InvalidVOArgumentException('Value is not a valid uuid.', $value); |
|
92 | } |
||
93 | |||
94 | /** |
||
95 | * @param string $value |
||
96 | * @param string|null $version |
||
97 | * @return Uuid |
||
98 | * @throws \ReflectionException |
||
99 | */ |
||
100 | 2 | public static function createFromString(string $value, ?string $version = null): self |
|
101 | { |
||
102 | 2 | return new static(new Strings($value), $version); |
|
103 | } |
||
104 | |||
105 | /** |
||
106 | * @return null|string |
||
107 | */ |
||
108 | 1 | public function getVersion(): ?string |
|
109 | { |
||
110 | 1 | return $this->version; |
|
111 | } |
||
112 | } |
||
113 |