|
1
|
|
|
<?php declare(strict_types=1); |
|
2
|
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Factories; |
|
4
|
|
|
|
|
5
|
|
|
use Exception; |
|
6
|
|
|
use InvalidArgumentException; |
|
7
|
|
|
use Ramsey\Uuid\Codec\OrderedTimeCodec; |
|
8
|
|
|
use Ramsey\Uuid\UuidInterface; |
|
9
|
|
|
|
|
10
|
|
|
class UuidFactory |
|
11
|
|
|
{ |
|
12
|
|
|
|
|
13
|
|
|
/** |
|
14
|
|
|
* @var \Ramsey\Uuid\UuidFactory |
|
15
|
|
|
*/ |
|
16
|
|
|
private $orderedTimeFactory; |
|
17
|
|
|
/** |
|
18
|
|
|
* @var \Ramsey\Uuid\UuidFactory |
|
19
|
|
|
*/ |
|
20
|
|
|
private $uuidFactory; |
|
21
|
|
|
|
|
22
|
|
|
public function __construct(\Ramsey\Uuid\UuidFactory $uuidFactory) |
|
23
|
|
|
{ |
|
24
|
|
|
|
|
25
|
|
|
$this->orderedTimeFactory = $this->createOrderedTimeFactory($uuidFactory); |
|
26
|
|
|
$this->uuidFactory = $uuidFactory; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
private function createOrderedTimeFactory(\Ramsey\Uuid\UuidFactory $uuidFactory): \Ramsey\Uuid\UuidFactory |
|
30
|
|
|
{ |
|
31
|
|
|
$this->orderedTimeFactory = clone $uuidFactory; |
|
32
|
|
|
$codec = new OrderedTimeCodec( |
|
33
|
|
|
$this->orderedTimeFactory->getUuidBuilder() |
|
34
|
|
|
); |
|
35
|
|
|
$this->orderedTimeFactory->setCodec($codec); |
|
36
|
|
|
|
|
37
|
|
|
return $this->orderedTimeFactory; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* Get a UUID interface from an ordered time UUID string |
|
42
|
|
|
* |
|
43
|
|
|
* @param string $uuidString |
|
44
|
|
|
* |
|
45
|
|
|
* @return UuidInterface |
|
46
|
|
|
*/ |
|
47
|
1 |
|
public function getOrderedTimeUuidFromString(string $uuidString): UuidInterface |
|
48
|
|
|
{ |
|
49
|
1 |
|
$uuid = $this->orderedTimeFactory->fromString($uuidString); |
|
50
|
1 |
|
if (1 !== $uuid->getVersion()) { |
|
51
|
|
|
throw new InvalidArgumentException( |
|
52
|
|
|
'UUID version is invalid, shoudl be version 1 when creating from string ' . |
|
53
|
|
|
$uuidString |
|
54
|
|
|
. |
|
55
|
|
|
"\n\n Make sure when querying the db you are using `bin_to_uuid(id, true)` " |
|
56
|
|
|
. "\ni.e passing in the second param to true to make MySQL use ordered time\n" |
|
57
|
|
|
); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
1 |
|
return $uuid; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* This is used to get ordered time UUIDs as used in: |
|
65
|
|
|
* \EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\PrimaryKey\UuidFieldTrait |
|
66
|
|
|
* |
|
67
|
|
|
* @return UuidInterface |
|
68
|
|
|
* @throws Exception |
|
69
|
|
|
*/ |
|
70
|
2 |
|
public function getOrderedTimeUuid(): UuidInterface |
|
71
|
|
|
{ |
|
72
|
2 |
|
return $this->orderedTimeFactory->uuid1(); |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
/** |
|
76
|
|
|
* This is used to generate standard UUIDs, as used in |
|
77
|
|
|
* \EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\PrimaryKey\NonBinaryUuidFieldTrait |
|
78
|
|
|
* |
|
79
|
|
|
* @return UuidInterface |
|
80
|
|
|
* @throws Exception |
|
81
|
|
|
*/ |
|
82
|
1 |
|
public function getUuid(): UuidInterface |
|
83
|
|
|
{ |
|
84
|
1 |
|
return $this->uuidFactory->uuid4(); |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
|
|
/** |
|
88
|
|
|
* @return \Ramsey\Uuid\UuidFactory |
|
89
|
|
|
*/ |
|
90
|
|
|
public function getOrderedTimeFactory(): \Ramsey\Uuid\UuidFactory |
|
91
|
|
|
{ |
|
92
|
|
|
return $this->orderedTimeFactory; |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* @return \Ramsey\Uuid\UuidFactory |
|
97
|
|
|
*/ |
|
98
|
|
|
public function getUuidFactory(): \Ramsey\Uuid\UuidFactory |
|
99
|
|
|
{ |
|
100
|
|
|
return $this->uuidFactory; |
|
101
|
|
|
} |
|
102
|
|
|
} |
|
103
|
|
|
|