1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/* |
6
|
|
|
* This file is part of the Micro framework package. |
7
|
|
|
* |
8
|
|
|
* (c) Stanislau Komar <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Micro\Library\DTO\Preparation\Processor\Property\Assert; |
15
|
|
|
|
16
|
|
|
use Symfony\Component\Validator\Constraints\Uuid; |
17
|
|
|
|
18
|
|
|
class UuidStrategy extends AbstractConstraintStrategy |
19
|
|
|
{ |
20
|
|
|
/** |
21
|
|
|
* @return array<int> |
22
|
|
|
*/ |
23
|
1 |
|
public function getAvailableVersions(): array |
24
|
|
|
{ |
25
|
|
|
// Supports 5.4 version |
26
|
1 |
|
$constants = [ |
27
|
1 |
|
'V1_MAC', 'V2_DCE', 'V3_MD5', 'V4_RANDOM', 'V5_SHA1', 'V6_SORTABLE', 'V7_MONOTONIC', |
28
|
1 |
|
]; |
29
|
|
|
|
30
|
1 |
|
return array_filter(array_map(function (string $constName) { |
31
|
1 |
|
$cName = sprintf('%s::%s', Uuid::class, $constName); |
32
|
|
|
|
33
|
1 |
|
return \defined($cName) ? \constant($cName) : null; |
34
|
1 |
|
}, $constants)); |
35
|
|
|
} |
36
|
|
|
|
37
|
1 |
|
protected function generateArguments(array $config): array |
38
|
|
|
{ |
39
|
|
|
/** @psalm-suppress PossiblyInvalidArgument */ |
40
|
1 |
|
return array_filter([ |
41
|
1 |
|
...parent::generateArguments($config), |
42
|
1 |
|
'versions' => $this->parseVersions(trim($config['versions'] ?? '')), |
43
|
1 |
|
'strict' => $this->stringToBool($config['strict'] ?? 'true'), |
44
|
1 |
|
]); |
45
|
|
|
} |
46
|
|
|
|
47
|
1 |
|
protected function getValidatorProperty(): string |
48
|
|
|
{ |
49
|
1 |
|
return 'uuid'; |
50
|
|
|
} |
51
|
|
|
|
52
|
1 |
|
protected function getAttributeClassName(): string |
53
|
|
|
{ |
54
|
1 |
|
return Uuid::class; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* @param array<int|string>|string $original |
59
|
|
|
* |
60
|
|
|
* @return array<int> |
61
|
|
|
*/ |
62
|
1 |
|
private function parseVersions(array|string $original): array |
63
|
|
|
{ |
64
|
1 |
|
$availableVersions = $this->getAvailableVersions(); |
65
|
1 |
|
if ('' === $original) { |
|
|
|
|
66
|
1 |
|
return $availableVersions; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
$versions = $original; |
70
|
|
|
if (\is_string($versions)) { |
|
|
|
|
71
|
|
|
$versions = $this->explodeString($versions); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
$versions = array_map('intval', $versions); |
75
|
|
|
$isValid = !array_diff($versions, $availableVersions); |
76
|
|
|
if ($isValid) { |
77
|
|
|
return $versions; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
throw new \InvalidArgumentException(sprintf('UUID versions is not allowed. Actual: `%s`, Available: `%s`', \is_array($original) ? implode(', ', $original) : $original, implode(',', $availableVersions))); |
|
|
|
|
81
|
|
|
} |
82
|
|
|
} |
83
|
|
|
|