Completed
Branch master (eb6d5f)
by Dmitry
02:18
created

Entity::__get()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3.2098

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 10
ccs 5
cts 7
cp 0.7143
rs 9.4285
cc 3
eloc 5
nc 3
nop 1
crap 3.2098
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use Closure;
6
use LogicException;
7
8
class Entity implements Contracts\Entity
9
{
10
    protected $original = [];
11
    protected $data = [];
12
13 23
    public function __construct(array $data = null)
14
    {
15 23
        $this->update($data);
16 23
    }
17
18 23
    public function update($data)
19
    {
20 23
        $this->original = $data;
21 23
        $this->data = $data;
22 23
    }
23
24 23
    public function __set($key, $value)
25
    {
26 23
        if ($key == 'id' && $this->getId()) {
27 1
            throw new LogicException('Id property is readonly');
28
        }
29
30 23
        $this->data[$key] = $value;
31 23
    }
32
33 23
    public function __get($key)
34
    {
35 23
        if (array_key_exists($key, $this->data)) {
36 23
            if ($this->data[$key] instanceof Closure) {
37
                $this->data[$key] = $this->data[$key]();
38
            }
39
40 23
            return $this->data[$key];
41
        }
42 23
    }
43
44
    /**
45
     * @return int
46
     */
47 23
    public function getId()
48
    {
49 23
        return $this->__get('id');
50
    }
51
52
    /**
53
     * @return Entity
54
     */
55 23
    public function setId($id)
56
    {
57 23
        $this->__set('id', $id);
58
59 23
        return $this;
60
    }
61
62
    /**
63
     * @return array
64
     */
65 22
    public function toArray($recursive = false)
66
    {
67 22
        $array = [];
68 22 View Code Duplication
        foreach (array_keys($this->data) as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
69 22
            $array[$key] = $this->__get($key);
70 22
            if ($array[$key] instanceof Contracts\Entity) {
71 3
                if ($recursive) {
72 1
                    $array[$key] = $array[$key]->toArray(true);
73 1
                }
74 3
            }
75 22
        }
76
77 22
        return $array;
78
    }
79
80
    public function getData()
81
    {
82
        $data = [];
83 View Code Duplication
        foreach (array_keys($this->data) as $key) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
84
            $data[$key] = $this->__get($key);
85
            if ($data[$key] instanceof Contracts\Entity) {
86
                $data[$key] = $data[$key]->id;
87
            }
88
        }
89
90
        return $data;
91
    }
92
93
    /**
94
     * @return array
95
     */
96 4
    public function pullChanges()
97
    {
98 4
        $changes = [];
99
100 4
        foreach ($this->data as $key => $value) {
101 4
            if (!array_key_exists($key, $this->original) || $this->original[$key] != $value) {
102 4
                $changes[$key] = $value;
103 4
            }
104 4
        }
105
106 4
        $this->original = $this->data;
107
108 4
        return $changes;
109
    }
110
}
111