Completed
Push — master ( 8ab73f...1793b5 )
by Dmitry
03:38
created

Entity::__get()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 4
Bugs 0 Features 0
Metric Value
c 4
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 1
crap 2
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use LogicException;
6
7
class Entity implements Contracts\Entity
8
{
9
    private $id;
0 ignored issues
show
Unused Code introduced by
The property $id is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11 61
    public function __construct(array $data = null)
12
    {
13 61
        if ($data) {
14 61
            $this->update($data);
15
        }
16 61
    }
17
18 61
    public function update($data)
19
    {
20 61
        foreach ($data as $k => $v) {
21 61
            $this->$k = $v;
22
        }
23 61
    }
24
25 61
    public function __set($key, $value)
26
    {
27 61
        if ($key == 'id' && $this->getId()) {
28 1
            throw new LogicException('Id property is readonly');
29
        }
30
31 61
        $this->$key = $value;
32 61
    }
33
34 61
    public function __get($key)
35
    {
36 61
        if (property_exists($this, $key)) {
37 61
            return $this->$key;
38
        }
39 2
    }
40
41
    /**
42
     * @return int
43
     */
44 61
    public function getId()
45
    {
46 61
        return $this->__get('id');
47
    }
48
49
    /**
50
     * @return Entity
51
     */
52 61
    public function setId($id)
53
    {
54 61
        $this->__set('id', $id);
55
56 61
        return $this;
57
    }
58
59
    /**
60
     * @return array
61
     */
62 60
    public function toArray($recursive = false)
63
    {
64 60
        $array = [];
65 60
        foreach (get_object_vars($this) as $k => $v) {
66 60
            $array[$k] = $v;
67 60
            if ($v instanceof Contracts\Entity) {
68 60
                $array[$k] = $v->toArray($recursive);
69
            }
70
        }
71
72 60
        return $array;
73
    }
74
}
75