Completed
Branch master (0bf65c)
by Dmitry
02:31
created

Entity::update()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
crap 1
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 4
    public function __construct(array $data = null)
13
    {
14 4
        $this->update($data);
15 4
    }
16
17 4
    public function update($data)
18
    {
19 4
        $this->original = $data;
20 4
        $this->data = $data;
21 4
    }
22
23 4
    public function __set($key, $value)
24
    {
25 4
        if ($key == 'id' && $this->getId()) {
26
            throw new LogicException("Id property is readonly");
27
        }
28
29 4
        $this->data[$key] = $value;
30 4
    }
31
32 4
    public function __get($key)
33
    {
34 4
        if (array_key_exists($key, $this->data)) {
35 4
            return $this->data[$key];
36
        }
37 4
    }
38
39
    /**
40
     * @return int
41
     */
42 4
    public function getId()
43
    {
44 4
        return $this->__get('id');
45
    }
46
47
    /**
48
     * @return Entity
49
     */
50 4
    public function setId($id)
51
    {
52 4
        $this->__set('id', $id);
53 4
        return $this;
54
    }
55
56
    /**
57
     * @return array
58
     */
59 4
    public function toArray()
60
    {
61 4
        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