Completed
Push — master ( 2f89bf...8773a6 )
by Dmitry
05:59 queued 02:54
created

Entity::setId()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

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