1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of the ddd-common. |
5
|
|
|
* |
6
|
|
|
* Copyright 2021 Evgenii Dudal <[email protected]>. |
7
|
|
|
* |
8
|
|
|
* This source file is subject to the MIT license that is bundled |
9
|
|
|
* with this source code in the file LICENSE. |
10
|
|
|
* @package ddd-common |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace RobotE13\DDD\Entities\Uuid; |
14
|
|
|
|
15
|
|
|
use Webmozart\Assert\Assert; |
16
|
|
|
use Ramsey\Uuid\{ |
17
|
|
|
UuidFactory, |
18
|
|
|
Codec\OrderedTimeCodec, |
19
|
|
|
Rfc4122\UuidV1 |
20
|
|
|
}; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Description of Id |
24
|
|
|
* |
25
|
|
|
* @method string getBytes() returns the binary string representation of the UUID |
26
|
|
|
* @method \Ramsey\Uuid\Type\Hexadecimal getHex() returns the hexadecimal representation of the UUID |
27
|
|
|
* @method \Ramsey\Uuid\Type\Integer getInteger() returns the integer representation of the UUID |
28
|
|
|
* @method string getString() returns the string standard representation of the UUID |
29
|
|
|
* @author Evgenii Dudal <[email protected]> |
30
|
|
|
*/ |
31
|
|
|
class Id |
32
|
|
|
{ |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var UuidV1 |
36
|
|
|
*/ |
37
|
|
|
private $uuid; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Constructor. |
41
|
|
|
* |
42
|
|
|
* @param string $representation A binary string |
43
|
|
|
* @throws \Ramsey\Uuid\Exception\InvalidUuidStringException if a valid UUID of type 1 cannot be build from string $reperesentation |
44
|
|
|
* @throws \InvalidArgumentException if resukt not UUID of type 1. |
45
|
|
|
*/ |
46
|
3 |
|
private function __construct(string $representation = null, $from = 'string') |
47
|
|
|
{ |
48
|
3 |
|
$factory = new UuidFactory(); |
49
|
3 |
|
$factory->setCodec(new OrderedTimeCodec($factory->getUuidBuilder())); |
50
|
|
|
|
51
|
3 |
|
$fromName = 'from' . ucfirst($from); |
52
|
3 |
|
$this->uuid = $representation === null ? $factory->uuid1() : $factory->{$fromName}($representation); |
53
|
|
|
|
54
|
2 |
|
Assert::isInstanceOf($this->uuid, UuidV1::class); |
55
|
2 |
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Creates a new ordered by time UUID. |
59
|
|
|
* @return self |
60
|
|
|
*/ |
61
|
2 |
|
public static function next() |
62
|
|
|
{ |
63
|
2 |
|
return new self(); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Initializes the UUID object from given bytes representation `$bytes`. |
68
|
|
|
* @param string $bytes |
69
|
|
|
* @return self |
70
|
|
|
* @throws \Ramsey\Uuid\Exception\InvalidUuidStringException if a valid UUID of type 1 cannot be build from bytes $reperesentation |
71
|
|
|
*/ |
72
|
3 |
|
public static function fromBytes($bytes) |
73
|
|
|
{ |
74
|
3 |
|
return new self($bytes, 'bytes'); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Initializes the UUID object from given string representation `$string`. |
79
|
|
|
* @param string $string |
80
|
|
|
* @return self |
81
|
|
|
* @throws \Ramsey\Uuid\Exception\InvalidUuidStringException if a valid UUID of type 1 cannot be build from string $reperesentation |
82
|
|
|
*/ |
83
|
1 |
|
public static function fromString($string) |
84
|
|
|
{ |
85
|
1 |
|
return new self($string, 'string'); |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* Calls the named method which is not a class method. |
90
|
|
|
* |
91
|
|
|
* This method creates wrapper methods over some methods of the Uuid1 getters. |
92
|
|
|
* |
93
|
|
|
* Do not call this method directly as it is a PHP magic method that |
94
|
|
|
* will be implicitly called when an unknown method is being invoked. |
95
|
|
|
* @param string $name the method name |
96
|
|
|
* @param array $arguments method parameters |
97
|
|
|
* @return mixed the method return value |
98
|
|
|
* @throws \BadMethodCallException when calling unknown method |
99
|
|
|
*/ |
100
|
2 |
|
public function __call($name, $arguments) |
101
|
|
|
{ |
102
|
2 |
|
$isAllowedGetter = strpos($name, 'get') === 0 && in_array($name, ['getBytes', 'getHex', 'getInteger']); |
103
|
2 |
|
if ($isAllowedGetter && method_exists($this->uuid, $name)) |
104
|
|
|
{ |
105
|
2 |
|
return $this->uuid->$name(); |
106
|
2 |
|
} elseif ($name === "getString") |
107
|
|
|
{ |
108
|
2 |
|
return $this->uuid->toString(); |
109
|
|
|
} |
110
|
1 |
|
throw new \BadMethodCallException('Calling unknown method: ' . get_class($this) . "::$name()"); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Returns -1, 0, or 1 if the UUID is less than, equal to, or greater than |
115
|
|
|
* the other UUID |
116
|
|
|
* |
117
|
|
|
* The first of two UUIDs is greater than the second if the most |
118
|
|
|
* significant field in which the UUIDs differ is greater for the first |
119
|
|
|
* UUID. |
120
|
|
|
* |
121
|
|
|
* @param self $other The UUID to compare |
122
|
|
|
* @return int -1, 0, or 1 if the UUID is less than, equal to, or greater than $other |
123
|
|
|
*/ |
124
|
1 |
|
public function compareTo(self $other): int |
125
|
|
|
{ |
126
|
1 |
|
return strcmp($this->getString(), $other->getString()) <=> 0; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Checks that the ID is equal to the provided object. |
131
|
|
|
* |
132
|
|
|
* @param self $other An object to test for equality with this ID |
133
|
|
|
* @return bool True if the other object is equal to this ID |
134
|
|
|
*/ |
135
|
1 |
|
public function isEqualTo(self $other): bool |
136
|
|
|
{ |
137
|
1 |
|
return $this->compareTo($other) === 0; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|