Passed
Push — master ( fc14ff...049ae2 )
by
unknown
01:15 queued 13s
created

UuidStrategy::getAvailableVersions()   A

Complexity

Conditions 2
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 8
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 6
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 12
ccs 8
cts 8
cp 1
crap 2
rs 10
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) {
0 ignored issues
show
introduced by
The condition '' === $original is always false.
Loading history...
66 1
            return $availableVersions;
67
        }
68
69
        $versions = $original;
70
        if (\is_string($versions)) {
0 ignored issues
show
introduced by
The condition is_string($versions) is always false.
Loading history...
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)));
0 ignored issues
show
introduced by
The condition is_array($original) is always true.
Loading history...
81
    }
82
}
83