Completed
Push — master ( 51c809...50c783 )
by Dmitry
03:19
created

Entity::pullChanges()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 4

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 14
ccs 7
cts 7
cp 1
rs 9.2
cc 4
eloc 7
nc 3
nop 0
crap 4
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 53
    public function __construct(array $data = null)
12
    {
13 53
        if ($data) {
14 53
            $this->update($data);
15
        }
16 53
    }
17
18 53
    public function update($data)
19
    {
20 53
        foreach ($data as $k => $v) {
21 53
            $this->$k = $v;
22
        }
23 53
    }
24
25 53
    public function __set($key, $value)
26
    {
27 53
        if ($key == 'id' && $this->getId()) {
28 1
            throw new LogicException('Id property is readonly');
29
        }
30
31 53
        $this->$key = $value;
32 53
    }
33
34 53
    public function __get($key)
35
    {
36 53
        if (property_exists($this, $key)) {
37 53
            return $this->$key;
38
        }
39 3
    }
40
41
    /**
42
     * @return int
43
     */
44 53
    public function getId()
45
    {
46 53
        return $this->__get('id');
47
    }
48
49
    /**
50
     * @return Entity
51
     */
52 53
    public function setId($id)
53
    {
54 53
        $this->__set('id', $id);
55
56 53
        return $this;
57
    }
58
59
    /**
60
     * @return array
61
     */
62 52
    public function toArray($recursive = false)
63
    {
64 52
        $array = [];
65 52
        foreach (get_object_vars($this) as $k => $v) {
66 52
            $array[$k] = $v;
67 52
            if ($v instanceof Contracts\Entity) {
68 52
                $array[$k] = $v->toArray($recursive);
69
            }
70
        }
71
72 52
        return $array;
73
    }
74
}
75