Completed
Push — master ( e4d56c...8e58c0 )
by Ivan
02:47
created

Entity   A

Complexity

Total Complexity 29

Size/Duplication

Total Lines 115
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Test Coverage

Coverage 76.12%

Importance

Changes 0
Metric Value
wmc 29
lcom 1
cbo 2
dl 0
loc 115
ccs 51
cts 67
cp 0.7612
rs 10
c 0
b 0
f 0

13 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 6 1
A __lazyProperty() 0 5 1
A __get() 0 17 5
A __set() 0 4 1
A __call() 0 11 4
A definition() 0 4 1
B toArray() 0 21 7
A fromArray() 0 9 3
A id() 0 8 2
A save() 0 5 1
A delete() 0 4 1
A refresh() 0 5 1
A flatten() 0 6 1
1
<?php
2
namespace vakata\database\schema;
3
4
class Entity
5
{
6
    protected $mapper;
7
    protected $empty;
8
    protected $definition;
9
    protected $initial = [];
10
    protected $changed = [];
11
    protected $fetched = [];
12
13 29
    public function __construct(Mapper $mapper, Table $definition, array $data = [])
14
    {
15 29
        $this->mapper = $mapper;
16 29
        $this->definition = $definition;
17 29
        $this->initial = $data;
18 29
    }
19 33
    public function __lazyProperty(string $property, $resolve)
20
    {
21 33
        $this->fetched[$property] = $resolve;
22 33
        return $this;
23
    }
24 51
    public function &__get($property)
25
    {
26 51
        if (array_key_exists($property, $this->fetched)) {
27 24
            if (is_callable($this->fetched[$property])) {
28 9
                $this->fetched[$property] = call_user_func($this->fetched[$property]);
29
            }
30 24
            return $this->fetched[$property];
31
        }
32 51
        if (array_key_exists($property, $this->changed)) {
33 3
            return $this->changed[$property];
34
        }
35 51
        if (array_key_exists($property, $this->initial)) {
36 51
            return $this->initial[$property];
37
        }
38
        $null = null;
39
        return $null;
40
    }
41 9
    public function __set($property, $value)
42
    {
43 9
        $this->changed[$property] = $value;
44 9
    }
45
    public function __call($method, $args)
46
    {
47
        if (isset($this->definition->getRelations()[$method])) {
48
            if (array_key_exists($method, $this->fetched)) {
49
                return is_callable($this->fetched[$method]) ?
50
                    $this->fetched[$method] = call_user_func($this->fetched[$method], $args[0] ?? null) :
51
                    $this->fetched[$method];
52
            }
53
        }
54
        return null;
55
    }
56 36
    public function definition()
57
    {
58 36
        return $this->definition;
59
    }
60 9
    public function toArray(bool $fetch = false)
61
    {
62 9
        $data = [];
63 9
        foreach ($this->definition->getColumns() as $k) {
64 9
            if (array_key_exists($k, $this->fetched)) {
65
                if ($fetch) {
66
                    $this->fetched[$k] = call_user_func($this->fetched[$k]);
67
                }
68
                if (!is_callable($this->fetched[$k])) {
69
                    $data[$k] = $this->fetched[$k];
70
                }
71
            }
72 9
            if (array_key_exists($k, $this->initial)) {
73 6
                $data[$k] = $this->initial[$k];
74
            }
75 9
            if (array_key_exists($k, $this->changed)) {
76 9
                $data[$k] = $this->changed[$k];
77
            }
78
        }
79 9
        return $data;
80
    }
81 3
    public function fromArray(array $data)
82
    {
83 3
        foreach ($this->definition->getColumns() as $k) {
84 3
            if (array_key_exists($k, $data)) {
85 3
                $this->changed[$k] = $data[$k];
86
            }
87
        }
88 3
        return $this;
89
    }
90 36
    public function id()
91
    {
92 36
        $primary = [];
93 36
        foreach ($this->definition->getPrimaryKey() as $k) {
94 36
            $primary[$k] = $this->initial[$k] ?? null;
95
        }
96 36
        return $primary;
97
    }
98 9
    public function save()
99
    {
100 9
        $this->mapper->save($this);
101 9
        return $this->flatten();
102
    }
103 3
    public function delete()
104
    {
105 3
        $this->mapper->delete($this);
106 3
    }
107
    public function refresh()
108
    {
109
        $this->mapper->refresh($this);
110
        return $this->flatten();
111
    }
112 9
    public function flatten()
113
    {
114 9
        $this->initial = $this->toArray();
115 9
        $this->changed = [];
116 9
        return $this;
117
    }
118
}
119