Completed
Push — master ( fc7aa2...a1b4af )
by Sebastian
03:22
created

DomainObjectAbstract::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 2
c 0
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 2
nc 2
nop 1
crap 2
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.
18
 */
19
abstract class DomainObjectAbstract implements DomainObjectInterface
20
{
21
    /**
22
     * @var int Read only object id on persistent storage.
23
     */
24
    protected int $id = 0;
25
26
    /**
27
     * @var string Read only insertion date on persistent storage.
28
     */
29
    public string $created = '';
30
31
    /**
32
     * @var string Read only last update date on persistento storage.
33
     */
34
    public string $lastUpdate = '';
35
36
    /**
37
     * Get the id of the object (unique to the object type).
38
     *
39
     * <pre><code class="php">$object = new DomainObject($dependencies);
40
     *
41
     * $object->getId();
42
     * </code></pre>
43
     *
44
     * @return int Current object id.
45
     */
46 111
    public function getId(): int
47
    {
48 111
        return $this->id;
49
    }
50
51
    /**
52
     * Set the id for the object.
53
     *
54
     * <pre><code class="php">$object = new DomainObject($dependencies);
55
     *
56
     * $object->setId(5);
57
     * </code></pre>
58
     *
59
     * @param int $id New object id.
60
     *
61
     * @throws UnexpectedValueException If the id on the object is already set.
62
     *
63
     * @return int New object id.
64
     */
65 34
    public function setId(int $id): int
66
    {
67 34
        if ($this->id !== 0) {
68 1
            throw new UnexpectedValueException('ObjectId property is immutable.');
69
        }
70
71 33
        $this->id = $id;
72
73 33
        return $id;
74
    }
75
76
    /**
77
     * Return the value of a private or protected property.
78
     *
79
     * @param string $name
80
     *
81
     * @return mixed
82
     */
83 220
    public function __get(string $name)
84
    {
85 220
        if ($name === 'id') {
86 220
            return $this->id;
87
        }
88
    }
89
90
    /**
91
     * Return if a private or protected property exists.
92
     *
93
     * @param string $name
94
     *
95
     * @return bool
96
     */
97 219
    public function __isset(string $name)
98
    {
99 219
        if ($name === 'id') {
100 219
            return true;
101
        }
102
103
        return false;
104
    }
105
}
106