Completed
Push — master ( ebdf9f...a89b72 )
by Dmitry
03:41
created

Entity::toArray()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 14
Code Lines 8

Duplication

Lines 8
Ratio 57.14 %

Code Coverage

Tests 7
CRAP Score 4.0312

Importance

Changes 4
Bugs 0 Features 2
Metric Value
c 4
b 0
f 2
dl 8
loc 14
ccs 7
cts 8
cp 0.875
rs 9.2
cc 4
eloc 8
nc 4
nop 1
crap 4.0312
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 49
                    $array[$key] = $array[$key]->toArray(true);
68
                }
69
            }
70
        }
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 1
                $data[$key] = $data[$key]->id;
82
            }
83
        }
84
85 1
        return $data;
86
    }
87
88
    /**
89
     * @return array
90
     */
91 12
    public function pullChanges()
92
    {
93 12
        $changes = [];
94
95 12
        foreach ($this->data as $key => $value) {
96 12
            if (!array_key_exists($key, $this->original) || $this->original[$key] != $value) {
97 12
                $changes[$key] = $value;
98
            }
99
        }
100
101 12
        $this->original = $this->data;
102
103 12
        return $changes;
104
    }
105
}
106