Completed
Push — master ( 0118f2...56d754 )
by Sebastian
03:50
created

UuidDomainObjectAbstract::__get()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 3

Importance

Changes 0
Metric Value
eloc 2
dl 0
loc 4
c 0
b 0
f 0
ccs 3
cts 3
cp 1
rs 10
cc 3
nc 2
nop 1
crap 3
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
    /**
22
     * @var string Read only object id on persistent storage.
23
     */
24
    protected string $uuid = '';
25
26
    /**
27
     * @var string Insertion date on persistent storage.
28
     */
29
    public string $created = '';
30
31
    /**
32
     * @var string Last update date on persistento storage.
33
     */
34
    public string $lastUpdate = '';
35
36
    /**
37
     * Get the UUID of the object (unique to the object type).
38
     * Alias of $this->getId();
39
     *
40
     * <pre><code class="php">$object = new DomainObject($dependencies);
41
     *
42
     * $object->getUuid();
43
     * </code></pre>
44
     *
45
     * @return string Current object uuid.
46
     */
47 1
    public function getUuid(): string
48
    {
49 1
        return $this->uuid;
50
    }
51
52
    /**
53
     * Get the id of the object (unique to the object type).
54
     *
55
     * <pre><code class="php">$object = new DomainObject($dependencies);
56
     *
57
     * $object->getId();
58
     * </code></pre>
59
     *
60
     * @return string Current object id.
61
     */
62 2
    public function getId(): string
63
    {
64 2
        return $this->uuid;
65
    }
66
67
    /**
68
     * Set the uuid for the object.
69
     *
70
     * <pre><code class="php">$object = new DomainObject($dependencies);
71
     *
72
     * $object->setId('10f6bace-22f3-4b42-9dda-659c89a3a3c9');
73
     * </code></pre>
74
     *
75
     * @param string $uuid New object uuid.
76
     *
77
     * @throws UnexpectedValueException If the uuid on the object is already set.
78
     *
79
     * @return string New object uid.
80
     */
81 2
    public function setId(string $uuid): string
82
    {
83 2
        if ($this->uuid !== '') {
84 1
            throw new UnexpectedValueException('ObjectId property is immutable.');
85
        }
86
87 1
        $this->uuid = $uuid;
88
89 1
        return $uuid;
90
    }
91
92
    /**
93
     * Set the creation date for the object.
94
     *
95
     * @return void
96
     *
97
     * @throws UnexpectedValueException If the creation date on the object is already set.
98
     */
99
    public function setCreated(): void
100
    {
101
        $date = \date(DATE_ATOM);
102
103
        if ($this->created !== '') {
104
            throw new UnexpectedValueException('Creation date property is immutable.');
105
        }
106
107
        $this->created = $date;
108
    }
109
110
    /**
111
     * Set the time for the last object changes.
112
     *
113
     * @return void
114
     */
115
    public function setLastUpdate(): void
116
    {
117
        $date = \date(DATE_ATOM);
118
119
        $this->lastUpdate = $date;
120
    }
121
122
    /**
123
     * Return the value of a private or protected property.
124
     *
125
     * @param string $name
126
     *
127
     * @return mixed
128
     */
129 2
    public function __get(string $name)
130
    {
131 2
        if ($name === 'uuid' || $name === 'id') {
132 1
            return $this->uuid;
133
        }
134 1
    }
135
136
    /**
137
     * Return if a private or protected property exists.
138
     *
139
     * @param string $name
140
     *
141
     * @return bool
142
     */
143 1
    public function __isset(string $name)
144
    {
145 1
        if ($name === 'uuid' || $name === 'id') {
146 1
            return true;
147
        }
148
149 1
        return false;
150
    }
151
}
152