UuidHelper::uuid()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 0
dl 0
loc 4
ccs 2
cts 2
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/*
3
 * Copyright (C) 2015 Thamtech, LLC
4
 *
5
 * This software is copyrighted. No part of this work may be
6
 * reproduced in whole or in part in any manner without the
7
 * permission of the Copyright owner, unless specifically authorized
8
 * by a license obtained from the Copyright owner.
9
**/
10
11
namespace thamtech\uuid\helpers;
12
13
use Ramsey\Uuid\Uuid;
14
15
/**
16
 * UuidHelper provides UUID functions that you can use in your application.
17
 *
18
 * @author Tyler Ham <[email protected]>
19
 */
20
class UuidHelper
21
{
22
    /**
23
     * Generate a UUID string (version 4 by default).
24
     *
25
     * @return string canonical format UUID, i.e. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a
26
     */
27 2
    public static function uuid()
28
    {
29 2
        return Uuid::uuid4()->toString();
30
    }
31
32
    /**
33
     * Checks if the given string looks like a UUID in the canonical format,
34
     * i.e. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a. This check is case
35
     * insensitive.
36
     *
37
     * @param  string  $uuid UUID String to check
38
     *
39
     * @return bool       whether or not it matches the pattern.
40
     */
41 1
    public static function isValid($uuid)
42
    {
43 1
        $validator = new \thamtech\uuid\validators\UuidValidator();
44
45 1
        return $validator->validate($uuid, $error);
46
    }
47
48
    /**
49
     * Converts a UUID string into a compact binary string.
50
     *
51
     * @param  string $uuid UUID in canonical format, i.e. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a
52
     *
53
     * @return string compact 16-byte binary representation of the UUID.
54
     */
55 2
    public static function uuid2bin($uuid)
56
    {
57
        // normalize to uppercase, remove hyphens
58 2
        $hex = str_replace('-', '', strtoupper($uuid));
59
60
        // H for big-endian to behave similarly to MySQL's
61
        // HEX() and UNHEX() functions.
62 2
        $bin = pack('H*', $hex);
63
64 2
        return $bin;
65
    }
66
67
    /**
68
     * Converts a compact 16-byte binary representation of the UUID into
69
     * a string in canonical format, i.e. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a.
70
     *
71
     * @param  string $uuidBin compact 16-byte binary representation of the UUID.
72
     *
73
     * @return string UUID in canonical format, i.e. 25769c6c-d34d-4bfe-ba98-e0ee856f3e7a
74
     */
75 2
    public static function bin2uuid($uuidBin)
76
    {
77
        // H for big-endian to behave similarly to MySQL's
78
        // HEX() and UNHEX() functions.
79 2
        $hexArray = unpack('H*', $uuidBin);
80
81 2
        $hex = strtolower(array_shift($hexArray));
82
83
        // break into components
84
        $components = [
85 2
          substr($hex, 0, 8),
86 2
          substr($hex, 8, 4),
87 2
          substr($hex, 12, 4),
88 2
          substr($hex, 16, 4),
89 2
          substr($hex, 20, 12),
90 2
        ];
91
92 2
        return implode('-', $components);
93
    }
94
}
95