Completed
Push — master ( 705b66...c30b18 )
by Dmitry
04:21
created

Entity::toArray()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 7
CRAP Score 3.0987

Importance

Changes 5
Bugs 0 Features 2
Metric Value
c 5
b 0
f 2
dl 0
loc 12
ccs 7
cts 9
cp 0.7778
rs 9.4285
cc 3
eloc 7
nc 3
nop 1
crap 3.0987
1
<?php
2
3
namespace Tarantool\Mapper;
4
5
use LogicException;
6
7
class Entity implements Contracts\Entity
8
{
9
    private $id;
0 ignored issues
show
Unused Code introduced by
The property $id is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
10
11 60
    public function __construct(array $data = null)
12
    {
13 60
        if ($data) {
14 60
            $this->update($data);
15 60
        }
16 60
    }
17
18 60
    public function update($data)
19
    {
20 60
        foreach ($data as $k => $v) {
21 60
            $this->$k = $v;
22 60
        }
23 60
    }
24
25 60
    public function __set($key, $value)
26
    {
27 60
        if ($key == 'id' && $this->getId()) {
28 1
            throw new LogicException('Id property is readonly');
29
        }
30
31 60
        $this->$key = $value;
32 60
    }
33
34 60
    public function __get($key)
35
    {
36 60
        if (property_exists($this, $key)) {
37 60
            return $this->$key;
38
        }
39 2
    }
40
41
    /**
42
     * @return int
43
     */
44 60
    public function getId()
45
    {
46 60
        return $this->__get('id');
47
    }
48
49
    /**
50
     * @return Entity
51
     */
52 60
    public function setId($id)
53
    {
54 60
        $this->__set('id', $id);
55
56 60
        return $this;
57
    }
58
59
    /**
60
     * @return array
61
     */
62 59
    public function toArray($recursive = false)
63
    {
64 59
        $array = [];
65 59
        foreach (get_object_vars($this) as $k => $v) {
66 59
            $array[$k] = $v;
67 59
            if ($v instanceof Contracts\Entity) {
68
                $array[$k] = $v->toArray($recursive);
69
            }
70 59
        }
71
72 59
        return $array;
73
    }
74
}
75