Uuid   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 0

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 0
dl 0
loc 115
ccs 22
cts 22
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 10 3
A validate() 0 9 4
A getMessageParameters() 0 13 3
1
<?php
2
/**
3
 * Particle.
4
 *
5
 * @link      http://github.com/particle-php for the canonical source repository
6
 * @copyright Copyright (c) 2005-2016 Particle (http://particle-php.com)
7
 * @license   https://github.com/particle-php/validator/blob/master/LICENSE New BSD License
8
 */
9
namespace Particle\Validator\Rule;
10
11
/**
12
 * This rule is for validating if a the value is a valid UUIDv4.
13
 *
14
 * @package Particle\Validator\Rule
15
 */
16
class Uuid extends Regex
17
{
18
    /**
19
     * A constant that will be used if the value is not a valid UUIDv4.
20
     */
21
    const INVALID_UUID = 'Uuid::INVALID_UUID';
22
23
    /**
24
     * UUID NIL & version binary masks
25
     */
26
    const UUID_VALID = 0b0000100;
27
    const UUID_NIL   = 0b0000001;
28
    const UUID_V1    = 0b0000010;
29
    const UUID_V2    = 0b0001000;
30
    const UUID_V3    = 0b0010000;
31
    const UUID_V4    = 0b0100000;
32
    const UUID_V5    = 0b1000000;
33
34
    /**
35
     * An array of all validation regexes.
36
     *
37
     * @var array
38
     */
39
    protected $regexes = [
40
        self::UUID_VALID => '~^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$~i',
41
        self::UUID_NIL   => '~^[0]{8}-[0]{4}-[0]{4}-[0]{4}-[0]{12}$~i',
42
        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',
43
        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',
44
        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',
45
        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',
46
        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',
47
    ];
48
49
    /**
50
     * An array of names for all the versions
51
     *
52
     * @var array
53
     */
54
    protected $versionNames = [
55
        self::UUID_VALID => 'valid format',
56
        self::UUID_NIL => 'NIL',
57
        self::UUID_V1 => 'v1',
58
        self::UUID_V2 => 'v2',
59
        self::UUID_V3 => 'v3',
60
        self::UUID_V4 => 'v4',
61
        self::UUID_V5 => 'v5',
62
    ];
63
64
    /**
65
     * The message templates which can be returned by this validator.
66
     *
67
     * @var array
68
     */
69
    protected $messageTemplates = [
70
        self::INVALID_UUID => '{{ name }} must be a valid UUID ({{ version }})'
71
    ];
72
73
    /**
74
     * The version of the UUID you'd like to check.
75
     *
76
     * @var int
77
     */
78
    protected $version;
79
80
    /**
81
     * Construct the UUID validation rule.
82
     *
83
     * @param int $version
84
     */
85 42
    public function __construct($version = self::UUID_VALID)
86
    {
87 42
        if ($version >= (self::UUID_V5 * 2) || $version < 0) {
88 3
            throw new \InvalidArgumentException(
89
                'Invalid UUID version mask given. Please choose one of the constants on the Uuid class.'
90 3
            );
91
        }
92
93 39
        $this->version = $version;
94 39
    }
95
96
    /**
97
     * Validates if the value is a valid UUID of an allowed version.
98
     *
99
     * @param string $value
100
     * @return bool
101
     */
102 39
    public function validate($value)
103
    {
104 39
        foreach ($this->regexes as $version => $regex) {
105 39
            if (($version & $this->version) === $version && preg_match($regex, $value) > 0) {
106 31
                return true;
107
            }
108 28
        }
109 8
        return $this->error(self::INVALID_UUID);
110
    }
111
112
    /**
113
     * Returns the parameters that may be used in a validation message.
114
     *
115
     * @return array
116
     */
117 8
    protected function getMessageParameters()
118
    {
119 8
        $versions = [];
120 8
        foreach (array_keys($this->regexes) as $version) {
121 8
            if (($version & $this->version) === $version) {
122 8
                $versions[] = $this->versionNames[$version];
123 8
            }
124 8
        }
125
126 8
        return array_merge(parent::getMessageParameters(), [
127 8
            'version' => implode(', ', $versions)
128 8
        ]);
129
    }
130
}
131