1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Linna Framework. |
5
|
|
|
* |
6
|
|
|
* @author Sebastian Rapetti <[email protected]> |
7
|
|
|
* @copyright (c) 2018, Sebastian Rapetti |
8
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
9
|
|
|
*/ |
10
|
|
|
declare(strict_types=1); |
11
|
|
|
|
12
|
|
|
namespace Linna\DataMapper; |
13
|
|
|
|
14
|
|
|
use UnexpectedValueException; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* Abstract Class for Domain Object, UUID version. |
18
|
|
|
*/ |
19
|
|
|
abstract class UuidDomainObjectAbstract implements UuidDomainObjectInterface |
20
|
|
|
{ |
21
|
|
|
use DomainObjectTimeTrait; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @var string Read only object id on persistent storage. |
25
|
|
|
*/ |
26
|
|
|
protected string $uuid = ''; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Get the UUID of the object (unique to the object type). |
30
|
|
|
* Alias of $this->getId(); |
31
|
|
|
* |
32
|
|
|
* <pre><code class="php">$object = new DomainObject($dependencies); |
33
|
|
|
* |
34
|
|
|
* $object->getUuid(); |
35
|
|
|
* </code></pre> |
36
|
|
|
* |
37
|
|
|
* @return string Current object uuid. |
38
|
|
|
*/ |
39
|
1 |
|
public function getUuid(): string |
40
|
|
|
{ |
41
|
1 |
|
return $this->uuid; |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Get the id of the object (unique to the object type). |
46
|
|
|
* |
47
|
|
|
* <pre><code class="php">$object = new DomainObject($dependencies); |
48
|
|
|
* |
49
|
|
|
* $object->getId(); |
50
|
|
|
* </code></pre> |
51
|
|
|
* |
52
|
|
|
* @return string Current object id. |
53
|
|
|
*/ |
54
|
5 |
|
public function getId(): string |
55
|
|
|
{ |
56
|
5 |
|
return $this->uuid; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Set the uuid for the object. |
61
|
|
|
* |
62
|
|
|
* <pre><code class="php">$object = new DomainObject($dependencies); |
63
|
|
|
* |
64
|
|
|
* $object->setId('10f6bace-22f3-4b42-9dda-659c89a3a3c9'); |
65
|
|
|
* </code></pre> |
66
|
|
|
* |
67
|
|
|
* @param string $uuid New object uuid. |
68
|
|
|
* |
69
|
|
|
* @throws UnexpectedValueException If the uuid on the object is already set. |
70
|
|
|
* |
71
|
|
|
* @return string New object uid. |
72
|
|
|
*/ |
73
|
5 |
|
public function setId(string $uuid): string |
74
|
|
|
{ |
75
|
5 |
|
if ($this->uuid !== '') { |
76
|
1 |
|
throw new UnexpectedValueException('ObjectId property is immutable.'); |
77
|
|
|
} |
78
|
|
|
|
79
|
4 |
|
$this->uuid = $uuid; |
80
|
|
|
|
81
|
4 |
|
return $uuid; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Return the value of a private or protected property. |
86
|
|
|
* |
87
|
|
|
* @param string $name |
88
|
|
|
* |
89
|
|
|
* @return mixed |
90
|
|
|
*/ |
91
|
5 |
|
public function __get(string $name) |
92
|
|
|
{ |
93
|
5 |
|
if ($name === 'uuid' || $name === 'id') { |
94
|
4 |
|
return $this->uuid; |
95
|
|
|
} |
96
|
1 |
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Return if a private or protected property exists. |
100
|
|
|
* |
101
|
|
|
* @param string $name |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
1 |
|
public function __isset(string $name) |
106
|
|
|
{ |
107
|
1 |
|
if ($name === 'uuid' || $name === 'id') { |
108
|
1 |
|
return true; |
109
|
|
|
} |
110
|
|
|
|
111
|
1 |
|
return false; |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|