1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace yentu\database; |
4
|
|
|
|
5
|
|
|
use yentu\exceptions\SyntaxErrorException; |
6
|
|
|
use yentu\factories\DatabaseItemFactory; |
7
|
|
|
use yentu\ChangeLogger; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
class DatabaseItem |
11
|
|
|
{ |
12
|
|
|
private ?DatabaseItem $encapsulated = null; |
13
|
|
|
private ChangeLogger $changeLogger; |
14
|
|
|
protected bool $new = false; |
15
|
|
|
private array $changes = []; |
16
|
|
|
protected string $home; |
17
|
|
|
protected DatabaseItemFactory $factory; |
18
|
|
|
private EncapsulatedStack $stack; |
19
|
|
|
|
20
|
|
|
public function setFactory(DatabaseItemFactory $factory): void |
21
|
|
|
{ |
22
|
|
|
$this->factory = $factory; |
23
|
|
|
} |
24
|
|
|
|
25
|
|
|
protected function addChange($property, $attribute, $value): DatabaseItem |
26
|
|
|
{ |
27
|
|
|
if (!$this->isNew()) { |
28
|
|
|
$currentDescription = $this->buildDescription(); |
|
|
|
|
29
|
|
|
$newDescription = $currentDescription; |
30
|
|
|
$newDescription[$attribute] = $value; |
31
|
|
|
$class = new \ReflectionClass($this); |
32
|
|
|
$name = $class->getShortName(); |
33
|
|
|
|
34
|
|
|
$this->changes[] = \yentu\Parameters::wrap(array( |
35
|
|
|
'method' => "change{$name}" . str_replace('_', '', $attribute), |
36
|
|
|
'args' => array( |
37
|
|
|
'from' => $currentDescription, |
38
|
|
|
'to' => $newDescription |
39
|
|
|
) |
40
|
|
|
)); |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
(new \ReflectionProperty($this, $property))->setValue($this, $value); |
44
|
|
|
|
45
|
|
|
return $this; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
public function isNew(): bool |
49
|
|
|
{ |
50
|
|
|
return $this->new; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
protected function getChangeLogger(): ChangeLogger |
54
|
|
|
{ |
55
|
|
|
return $this->changeLogger; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function setChangeLogger($changeLogger): void |
59
|
|
|
{ |
60
|
|
|
$this->changeLogger = $changeLogger; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
public function setStack(EncapsulatedStack $stack): void |
64
|
|
|
{ |
65
|
|
|
$this->stack = $stack; |
66
|
|
|
if ($this->stack->hasItems()) { |
67
|
|
|
$this->encapsulated = $this->stack->top(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
public function getStack(): EncapsulatedStack |
72
|
|
|
{ |
73
|
|
|
return $this->stack; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
public function __call($method, $arguments) |
77
|
|
|
{ |
78
|
|
|
if (!is_object($this->encapsulated)) { |
79
|
|
|
throw new SyntaxErrorException("Failed to call method '{$method}'", $this->home ?? ''); |
80
|
|
|
} else if (method_exists($this->encapsulated, $method)) { |
81
|
|
|
$method = new \ReflectionMethod($this->encapsulated, $method); |
82
|
|
|
$this->commit(); |
83
|
|
|
$this->stack->pop(); |
84
|
|
|
return $method->invokeArgs($this->encapsulated, $arguments); |
85
|
|
|
} else { |
86
|
|
|
$this->commit(); |
87
|
|
|
$this->stack->pop(); |
88
|
|
|
return $this->encapsulated->__call($method, $arguments); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
public function commit(): DatabaseItem |
93
|
|
|
{ |
94
|
|
|
if ($this instanceof Commitable && $this->isNew()) { |
95
|
|
|
$this->commitNew(); |
|
|
|
|
96
|
|
|
$this->new = false; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
foreach ($this->changes as $change) { |
100
|
|
|
$this->changeLogger->{$change['method']}($change['args']); |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
return $this; |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
public function rename($newName): DatabaseItem |
107
|
|
|
{ |
108
|
|
|
return $this->addChange('name', 'name', $newName); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
public function setHome(string $home): void |
112
|
|
|
{ |
113
|
|
|
$this->home = $home; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
public function init(): void |
117
|
|
|
{ |
118
|
|
|
if ($this instanceof Initializable) { |
119
|
|
|
$this->initialize(); |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|