Completed
Push — master ( 8ab73f...1793b5 )
by Dmitry
03:38
created

Entity   A

Complexity

Total Complexity 14

Size/Duplication

Total Lines 68
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 8
Bugs 0 Features 3
Metric Value
c 8
b 0
f 3
dl 0
loc 68
wmc 14
lcom 1
cbo 1
ccs 29
cts 29
cp 1
rs 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 2
A update() 0 6 2
A __set() 0 8 3
A __get() 0 6 2
A getId() 0 4 1
A setId() 0 6 1
A toArray() 0 12 3
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
        }
16 61
    }
17
18 61
    public function update($data)
19
    {
20 61
        foreach ($data as $k => $v) {
21 61
            $this->$k = $v;
22
        }
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 60
                $array[$k] = $v->toArray($recursive);
69
            }
70
        }
71
72 60
        return $array;
73
    }
74
}
75