DomainObjectAbstract::__isset()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

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