Completed
Push — master ( 13dce9...b41dd5 )
by Dmitry
03:01
created

Entity::__set()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 3.072

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 8
ccs 4
cts 5
cp 0.8
rs 9.4285
cc 3
eloc 4
nc 2
nop 2
crap 3.072
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use LogicException;
6
7
class Entity implements Contracts\Entity
8
{
9
    protected $original = [];
10
    protected $data = [];
11
12 2
    public function __construct(array $data = null)
13
    {
14 2
        $this->update($data);
15 2
    }
16
17 2
    public function update($data)
18
    {
19 2
        $this->original = $data;
20 2
        $this->data = $data;
21 2
    }
22
23 2
    public function __set($key, $value)
24
    {
25 2
        if ($key == 'id' && $this->getId()) {
26
            throw new LogicException("Id property is readonly");
27
        }
28
29 2
        $this->data[$key] = $value;
30 2
    }
31
32 2
    public function __get($key)
33
    {
34 2
        if (array_key_exists($key, $this->data)) {
35 2
            return $this->data[$key];
36
        }
37 2
    }
38
39
    /**
40
     * @return int
41
     */
42 2
    public function getId()
43
    {
44 2
        return $this->__get('id');
45
    }
46
47
    /**
48
     * @return Entity
49
     */
50 2
    public function setId($id)
51
    {
52 2
        $this->__set('id', $id);
53 2
        return $this;
54
    }
55
56
    /**
57
     * @return array
58
     */
59 2
    public function toArray()
60
    {
61 2
        return $this->data;
62
    }
63
64
    /**
65
     * @return array
66
     */
67 1
    public function pullChanges()
68
    {
69 1
        $changes = [];
70
71 1
        foreach ($this->data as $key => $value) {
72 1
            if (!array_key_exists($key, $this->original) || $this->original[$key] != $value) {
73 1
                $changes[$key] = $value;
74 1
            }
75 1
        }
76
77 1
        $this->original = $this->data;
78 1
        return $changes;
79
    }
80
}
81