1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Factories; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use Ramsey\Uuid\Codec\OrderedTimeCodec; |
7
|
|
|
use Ramsey\Uuid\UuidInterface; |
8
|
|
|
|
9
|
|
|
class UuidFactory |
10
|
|
|
{ |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* @var \Ramsey\Uuid\UuidFactory |
14
|
|
|
*/ |
15
|
|
|
private $orderedTimeFactory; |
16
|
|
|
/** |
17
|
|
|
* @var \Ramsey\Uuid\UuidFactory |
18
|
|
|
*/ |
19
|
|
|
private $uuidFactory; |
20
|
|
|
|
21
|
|
|
public function __construct(\Ramsey\Uuid\UuidFactory $uuidFactory) |
22
|
|
|
{ |
23
|
|
|
|
24
|
|
|
$this->orderedTimeFactory = $this->createOrderedTimeFactory($uuidFactory); |
25
|
|
|
$this->uuidFactory = $uuidFactory; |
26
|
|
|
} |
27
|
|
|
|
28
|
|
|
private function createOrderedTimeFactory(\Ramsey\Uuid\UuidFactory $uuidFactory): \Ramsey\Uuid\UuidFactory |
29
|
|
|
{ |
30
|
|
|
$this->orderedTimeFactory = clone $uuidFactory; |
31
|
|
|
$codec = new OrderedTimeCodec( |
32
|
|
|
$this->orderedTimeFactory->getUuidBuilder() |
33
|
|
|
); |
34
|
|
|
$this->orderedTimeFactory->setCodec($codec); |
35
|
|
|
|
36
|
|
|
return $this->orderedTimeFactory; |
37
|
|
|
} |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Get a UUID interface from an ordered time UUID string |
41
|
|
|
* |
42
|
|
|
* @param string $uuidString |
43
|
|
|
* |
44
|
|
|
* @return UuidInterface |
45
|
|
|
*/ |
46
|
1 |
|
public function getOrderedTimeUuidFromString(string $uuidString): UuidInterface |
47
|
|
|
{ |
48
|
1 |
|
return $this->orderedTimeFactory->fromString($uuidString); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* This is used to get ordered time UUIDs as used in: |
53
|
|
|
* \EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\PrimaryKey\UuidFieldTrait |
54
|
|
|
* |
55
|
|
|
* @return UuidInterface |
56
|
|
|
* @throws Exception |
57
|
|
|
*/ |
58
|
2 |
|
public function getOrderedTimeUuid(): UuidInterface |
59
|
|
|
{ |
60
|
2 |
|
return $this->orderedTimeFactory->uuid1(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* This is used to generate standard UUIDs, as used in |
65
|
|
|
* \EdmondsCommerce\DoctrineStaticMeta\Entity\Fields\Traits\PrimaryKey\NonBinaryUuidFieldTrait |
66
|
|
|
* |
67
|
|
|
* @return UuidInterface |
68
|
|
|
* @throws Exception |
69
|
|
|
*/ |
70
|
1 |
|
public function getUuid(): UuidInterface |
71
|
|
|
{ |
72
|
1 |
|
return $this->uuidFactory->uuid4(); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* @return \Ramsey\Uuid\UuidFactory |
77
|
|
|
*/ |
78
|
|
|
public function getOrderedTimeFactory(): \Ramsey\Uuid\UuidFactory |
79
|
|
|
{ |
80
|
|
|
return $this->orderedTimeFactory; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* @return \Ramsey\Uuid\UuidFactory |
85
|
|
|
*/ |
86
|
|
|
public function getUuidFactory(): \Ramsey\Uuid\UuidFactory |
87
|
|
|
{ |
88
|
|
|
return $this->uuidFactory; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|