|
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
|
|
|
|