/
UuidHelper.php
91 lines (79 loc) · 2.08 KB
/
UuidHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
<?php
declare(strict_types = 1);
/**
* /src/Rest/UuidHelper.php
*
* @author TLe, Tarmo Leppänen <tarmo.leppanen@protacon.com>
*/
namespace App\Rest;
use Ramsey\Uuid\Codec\OrderedTimeCodec;
use Ramsey\Uuid\Doctrine\UuidBinaryOrderedTimeType;
use Ramsey\Uuid\Exception\InvalidUuidStringException;
use Ramsey\Uuid\Uuid;
use Ramsey\Uuid\UuidFactory;
use Ramsey\Uuid\UuidInterface;
use Throwable;
/**
* Class UuidHelper
*
* @package App\Rest
* @author TLe, Tarmo Leppänen <tarmo.leppanen@protacon.com>
*/
class UuidHelper
{
/**
* @return UuidFactory
*/
public static function getFactory(): UuidFactory
{
static $cache = null;
if ($cache === null) {
/** @var UuidFactory $factory */
$factory = clone Uuid::getFactory();
$codec = new OrderedTimeCodec($factory->getUuidBuilder());
$factory->setCodec($codec);
$cache = $factory;
}
return $cache;
}
/**
* Method to get proper doctrine parameter type for UuidBinaryOrderedTimeType values.
*
* @param string $value
*
* @return string|null
*/
public static function getType(string $value): ?string
{
$factory = self::getFactory();
$output = null;
try {
$factory->fromString($value);
$output = UuidBinaryOrderedTimeType::NAME;
} catch (InvalidUuidStringException $exception) {
// ok, so now we know that value isn't uuid
(fn (Throwable $exception): Throwable => $exception)($exception);
}
return $output;
}
/**
* @param string $value
*
* @return UuidInterface
*/
public static function fromString(string $value): UuidInterface
{
return self::getFactory()->fromString($value);
}
/**
* Method to get bytes value for specified UuidBinaryOrderedTimeType value.
*
* @param string $value
*
* @return string
*/
public static function getBytes(string $value): string
{
return self::fromString($value)->getBytes();
}
}