Uuid::isValid()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2.0625

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 7
ccs 3
cts 4
cp 0.75
rs 10
cc 2
nc 2
nop 1
crap 2.0625
1
<?php
2
3
namespace Nip\Utility;
4
5
use Ramsey\Uuid\Uuid as RamseyUuid;
6
7
/**
8
 * Class Uuid
9
 * @package Nip\Utility
10
 */
11
class Uuid
12
{
13
    /**
14
     * The callback that should be used to generate UUIDs.
15
     *
16
     * @var callable
17
     */
18
    protected static $uuidFactory;
19
20
    /**
21
     * Generate a UUID (version 4).
22
     *
23
     * @return \Ramsey\Uuid\UuidInterface
24
     */
25 1
    public static function uuid()
26
    {
27 1
        return static::$uuidFactory
28
            ? call_user_func(static::$uuidFactory)
29 1
            : RamseyUuid::uuid4();
30
    }
31
32
    /**
33
     * Generate a UUID (version 4).
34
     *
35
     * @return \Ramsey\Uuid\UuidInterface
36
     */
37
    public static function v4()
38
    {
39
        return RamseyUuid::uuid4();
40
    }
41
42
    /**
43
     * Generate a UUID (version 5).
44
     *
45
     * @return \Ramsey\Uuid\UuidInterface
46
     */
47
    public static function v5($ns, string $name)
48
    {
49
        return RamseyUuid::uuid5($ns, $name);
50
    }
51
52
    /**
53
     * @param $uuid
54
     * @return mixed
55
     */
56 10
    public static function fromString($uuid)
57
    {
58 10
        return RamseyUuid::fromString($uuid);
59
    }
60
61
    /**
62
     * @param $uuid
63
     * @return mixed
64
     */
65
    public static function fromBytes($uuid)
66
    {
67
        return RamseyUuid::fromBytes($uuid);
68
    }
69
70
    /**
71
     * @param $uuid
72
     * @return bool
73
     */
74 21
    public static function isValid($uuid)
75
    {
76 21
        if (!is_string($uuid)) {
77
            return false;
78
        }
79
80 21
        return preg_match('/^[\da-f]{8}-[\da-f]{4}-[\da-f]{4}-[\da-f]{4}-[\da-f]{12}$/iD', $uuid) > 0;
81
    }
82
83
    /**
84
     * Set the callable that will be used to generate UUIDs.
85
     *
86
     * @param callable|null $factory
87
     * @return void
88
     */
89
    public static function createUsing(callable $factory = null)
90
    {
91
        static::$uuidFactory = $factory;
92
    }
93
94
    /**
95
     * Indicate that UUIDs should be created normally and not using a custom factory.
96
     *
97
     * @return void
98
     */
99
    public static function createNormally()
100
    {
101
        static::$uuidFactory = null;
102
    }
103
}
104