1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of slick/cqrs-tools |
5
|
|
|
* |
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
7
|
|
|
* file that was distributed with this source code. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Slick\CQRSTools\Domain; |
11
|
|
|
|
12
|
|
|
use Exception; |
13
|
|
|
use JsonSerializable; |
14
|
|
|
use Ramsey\Uuid\Uuid; |
15
|
|
|
use Ramsey\Uuid\UuidInterface; |
16
|
|
|
use Slick\CQRSTools\Domain\Common\Comparable; |
17
|
|
|
use Slick\CQRSTools\Domain\Common\Stringable; |
18
|
|
|
use Slick\CQRSTools\Domain\Exception\IdentifierCreationException; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Aggregate Root Identifier |
22
|
|
|
* |
23
|
|
|
* @package Slick\CQRSTools\Domain |
24
|
|
|
*/ |
25
|
|
|
abstract class AggregateRootIdentifier implements Stringable, Comparable, JsonSerializable |
26
|
|
|
{ |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* @var UuidInterface |
30
|
|
|
*/ |
31
|
|
|
protected $uuid; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Creates an AggregateRootIdentifier |
35
|
|
|
* |
36
|
|
|
* @param string|null $uuidStr |
37
|
|
|
* |
38
|
|
|
* @throws IdentifierCreationException when internal UUID cannot be created |
39
|
|
|
*/ |
40
|
|
|
public function __construct(string $uuidStr = null) |
41
|
|
|
{ |
42
|
|
|
try { |
43
|
|
|
$this->uuid = Uuid::uuid4(); |
44
|
|
|
if ($uuidStr) { |
|
|
|
|
45
|
|
|
$this->uuid = $this->createFromString($uuidStr); |
46
|
|
|
} |
47
|
|
|
} catch (Exception $caught) { |
48
|
|
|
throw new IdentifierCreationException($caught->getMessage(), 1, $caught); |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Internal UUID |
54
|
|
|
* |
55
|
|
|
* @return UuidInterface |
56
|
|
|
*/ |
57
|
|
|
public function wrappedUuid(): UuidInterface |
58
|
|
|
{ |
59
|
|
|
return $this->uuid; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* Compares object for equality |
64
|
|
|
* |
65
|
|
|
* @param mixed $other |
66
|
|
|
* |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public function equalsTo($other): bool |
70
|
|
|
{ |
71
|
|
|
$sameType = is_object($other) && is_a($other, get_called_class()); |
72
|
|
|
return $sameType && $this->uuid->equals($other->uuid); |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* String version of UUID that will be converted to a JSON string |
77
|
|
|
* |
78
|
|
|
* @return string data which can be serialized by json_encode(), |
79
|
|
|
* which is a value of any type other than a resource. |
80
|
|
|
*/ |
81
|
|
|
public function jsonSerialize() |
82
|
|
|
{ |
83
|
|
|
return (string) $this->uuid; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* String representation of the object |
88
|
|
|
* |
89
|
|
|
* @return string |
90
|
|
|
*/ |
91
|
|
|
public function __toString() |
92
|
|
|
{ |
93
|
|
|
return (string) $this->uuid; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Creates an UUID with provided UUID string |
98
|
|
|
* |
99
|
|
|
* @param string $uuidStr |
100
|
|
|
* |
101
|
|
|
* @return UuidInterface |
102
|
|
|
* |
103
|
|
|
* @throws IdentifierCreationException If the provided text is not a valid UUID string |
104
|
|
|
*/ |
105
|
|
|
protected function createFromString(string $uuidStr): UuidInterface |
106
|
|
|
{ |
107
|
|
|
if (!Uuid::isValid($uuidStr)) { |
108
|
|
|
throw new IdentifierCreationException( |
109
|
|
|
"Invalid aggregate root ID '{$uuidStr}'." |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return Uuid::fromString($uuidStr); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
In PHP, under loose comparison (like
==
, or!=
, orswitch
conditions), values of different types might be equal.For
string
values, the empty string''
is a special case, in particular the following results might be unexpected: