UUID::next()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
dl 0
loc 4
rs 10
c 1
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
3
/*
4
 * This file is part of the Cubiche package.
5
 *
6
 * Copyright (c) Cubiche
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 Cubiche\Domain\Identity;
13
14
use Ramsey\Uuid\Uuid as BaseUuid;
15
16
/**
17
 * Universally Unique Id Class (UUID).
18
 *
19
 * @author Karel Osorio Ramírez <[email protected]>
20
 */
21
class UUID extends StringId
22
{
23
    /**
24
     * @param string $value
25
     *
26
     * @return bool
27
     */
28
    public static function isValidUUID($value)
29
    {
30
        $pattern = '/'.BaseUuid::VALID_PATTERN.'/';
31
32
        return \preg_match($pattern, $value) === 1;
33
    }
34
35
    /**
36
     * @param string $value
37
     *
38
     * @throws \InvalidArgumentException
39
     */
40
    public function __construct($value = null)
41
    {
42
        if ($value !== null && !self::isValidUUID($value)) {
43
            throw new \InvalidArgumentException(sprintf('Argument "%s" is an invalid UUID.', $value));
44
        }
45
46
        $this->value = $value === null ? self::nextUUIDValue() :  $value;
47
    }
48
49
    /**
50
     * @return static
51
     */
52
    public static function next()
53
    {
54
        return new static();
55
    }
56
57
    /**
58
     * @return string
59
     */
60
    public static function nextUUIDValue()
61
    {
62
        return BaseUuid::uuid4()->toString();
63
    }
64
}
65