Completed
Push — master ( f74f27...ebdf9f )
by Dmitry
05:43 queued 03:08
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 3
Bugs 0 Features 0
Metric Value
c 3
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
    protected $original = [];
10
    protected $data = [];
11
12 50
    public function __construct(array $data = null)
13
    {
14 50
        $this->update($data);
15 50
    }
16
17 50
    public function update($data)
18
    {
19 50
        $this->original = $data;
20 50
        $this->data = $data;
21 50
    }
22
23 50
    public function __set($key, $value)
24
    {
25 50
        if ($key == 'id' && $this->getId()) {
26 1
            throw new LogicException('Id property is readonly');
27
        }
28
29 50
        $this->data[$key] = $value;
30 50
    }
31
32 50
    public function __get($key)
33
    {
34 50
        if (array_key_exists($key, $this->data)) {
35 50
            return $this->data[$key];
36
        }
37 50
    }
38
39
    /**
40
     * @return int
41
     */
42 50
    public function getId()
43
    {
44 50
        return $this->__get('id');
45
    }
46
47
    /**
48
     * @return Entity
49
     */
50 50
    public function setId($id)
51
    {
52 50
        $this->__set('id', $id);
53
54 50
        return $this;
55
    }
56
57
    /**
58
     * @return array
59
     */
60 49
    public function toArray($recursive = false)
61
    {
62 49
        $array = [];
63 49 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...
64 49
            $array[$key] = $this->__get($key);
65 49
            if ($array[$key] instanceof Contracts\Entity) {
66
                if ($recursive) {
67
                    $array[$key] = $array[$key]->toArray(true);
68
                }
69
            }
70 49
        }
71
72 49
        return $array;
73
    }
74
75 1
    public function getData()
76
    {
77 1
        $data = [];
78 1 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...
79 1
            $data[$key] = $this->__get($key);
80 1
            if ($data[$key] instanceof Contracts\Entity) {
81
                $data[$key] = $data[$key]->id;
82
            }
83 1
        }
84
85 1
        return $data;
86
    }
87
88
    /**
89
     * @return array
90
     */
91 11
    public function pullChanges()
92
    {
93 11
        $changes = [];
94
95 11
        foreach ($this->data as $key => $value) {
96 11
            if (!array_key_exists($key, $this->original) || $this->original[$key] != $value) {
97 11
                $changes[$key] = $value;
98 11
            }
99 11
        }
100
101 11
        $this->original = $this->data;
102
103 11
        return $changes;
104
    }
105
}
106