1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Modules\Entitizer\Utils { |
4
|
|
|
|
5
|
|
|
use DB; |
6
|
|
|
|
7
|
|
|
abstract class Definition { |
8
|
|
|
|
9
|
|
|
private $id = null, $params = [], $orderers = [], $implementors = []; |
10
|
|
|
|
11
|
|
|
# Add param |
12
|
|
|
|
13
|
|
|
private function addParam(Param $param) { |
14
|
|
|
|
15
|
|
View Code Duplication |
if (('' === ($name = $param->name())) || isset($this->params[$name]) || isset($this->implementors[$name])) return; |
|
|
|
|
16
|
|
|
|
17
|
|
|
$this->params[$name] = $param; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
# Get list of params statements |
22
|
|
|
|
23
|
|
|
private function getStatements($method) { |
24
|
|
|
|
25
|
|
|
$statements = [$this->id->$method()]; |
26
|
|
|
|
27
|
|
|
foreach ($this->params as $param) { |
28
|
|
|
|
29
|
|
|
if (false !== ($statement = $param->$method())) $statements[] = $statement; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
# ------------------------ |
33
|
|
|
|
34
|
|
|
return $statements; |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
# Add boolean param |
38
|
|
|
|
39
|
|
|
protected function addBoolean(string $name, bool $default = false, bool $index = false) { |
40
|
|
|
|
41
|
|
|
$this->addParam(new Param\Type\Boolean($name, $default, $index)); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
# Add integer param |
45
|
|
|
|
46
|
|
|
protected function addInteger(string $name, bool $short = false, int $maxlength = 0, |
47
|
|
|
|
48
|
|
|
int $default = 0, bool $index = false, bool $unique = false) { |
49
|
|
|
|
50
|
|
|
$this->addParam(new Param\Type\Integer($name, $short, $maxlength, $default, $index, $unique)); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
# Add textual param |
54
|
|
|
|
55
|
|
|
protected function addTextual(string $name, bool $text = false, int $maxlength = 0, |
56
|
|
|
|
57
|
|
|
bool $binary = false, bool $index = false, bool $unique = false) { |
58
|
|
|
|
59
|
|
|
$this->addParam(new Param\Type\Textual($name, $text, $maxlength, $binary, $index, $unique)); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
# Add orderer |
63
|
|
|
|
64
|
|
|
protected function addOrderer(string $name, bool $descending = false) { |
65
|
|
|
|
66
|
|
|
if (!isset($this->params[$name]) || isset($this->orderers[$name])) return; |
67
|
|
|
|
68
|
|
|
$this->orderers[$name] = $descending; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
# Add implementor |
72
|
|
|
|
73
|
|
|
protected function addImplementor(string $name, callable $callback) { |
74
|
|
|
|
75
|
|
View Code Duplication |
if (('' === $name) || isset($this->params[$name]) || isset($this->implementors[$name])) return; |
|
|
|
|
76
|
|
|
|
77
|
|
|
$this->implementors[$name] = $callback; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
# Constructor |
81
|
|
|
|
82
|
|
|
public function __construct() { |
83
|
|
|
|
84
|
|
|
$this->id = new Param\Id('id', static::$auto_increment); |
85
|
|
|
|
86
|
|
|
# Define specific entity params |
87
|
|
|
|
88
|
|
|
$this->define(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
# Create table |
92
|
|
|
|
93
|
|
|
public function createTable() { |
94
|
|
|
|
95
|
|
|
$set = array_merge($this->getStatements('fieldStatement'), $this->getStatements('keyStatement')); |
96
|
|
|
|
97
|
|
|
$query = ("CREATE TABLE IF NOT EXISTS `" . static::$table . "`") . |
98
|
|
|
|
99
|
|
|
("(" . implode(", ", $set) . ") ENGINE=InnoDB DEFAULT CHARSET=utf8"); |
100
|
|
|
|
101
|
|
|
# ------------------------ |
102
|
|
|
|
103
|
|
|
return (DB::send($query) && DB::last()->status); |
104
|
|
|
} |
105
|
|
|
|
106
|
|
|
# Return id param |
107
|
|
|
|
108
|
|
|
public function id() { |
109
|
|
|
|
110
|
|
|
return $this->id; |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
# Return params |
114
|
|
|
|
115
|
|
|
public function params() { |
116
|
|
|
|
117
|
|
|
return $this->params; |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
# Return orderers |
121
|
|
|
|
122
|
|
|
public function orderers() { |
123
|
|
|
|
124
|
|
|
return $this->orderers; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
# Return param by name |
128
|
|
|
|
129
|
|
|
public function get(string $name) { |
130
|
|
|
|
131
|
|
|
return (isset($this->params[$name]) ? $this->params[$name] : false); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
# Cast data to be suitable with definition |
135
|
|
|
|
136
|
|
|
public function cast(array $data, bool $process_id = false) { |
137
|
|
|
|
138
|
|
|
$result = []; |
139
|
|
|
|
140
|
|
|
# Cast id |
141
|
|
|
|
142
|
|
|
if ($process_id) { |
143
|
|
|
|
144
|
|
|
if (isset($data['id'])) $result['id'] = $this->id->cast($data['id']); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
# Cast params |
148
|
|
|
|
149
|
|
|
foreach ($this->params as $name => $param) { |
150
|
|
|
|
151
|
|
|
if (isset($data[$name])) $result[$name] = $param->cast($data[$name]); |
152
|
|
|
} |
153
|
|
|
|
154
|
|
|
# ------------------------ |
155
|
|
|
|
156
|
|
|
return $result; |
157
|
|
|
} |
158
|
|
|
|
159
|
|
|
# Implement data accordingly to definition |
160
|
|
|
|
161
|
|
|
public function implement(array $data = [], bool $process_id = false) { |
162
|
|
|
|
163
|
|
|
$result = []; |
164
|
|
|
|
165
|
|
|
# Cast id |
166
|
|
|
|
167
|
|
|
if ($process_id) { |
168
|
|
|
|
169
|
|
|
$result['id'] = $this->id->cast($data['id'] ?? null); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
# Cast params |
173
|
|
|
|
174
|
|
|
foreach ($this->params as $name => $param) { |
175
|
|
|
|
176
|
|
|
$result[$name] = $param->cast($data[$name] ?? null); |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
# Cast extras |
180
|
|
|
|
181
|
|
|
foreach ($this->implementors as $name => $callback) { |
182
|
|
|
|
183
|
|
|
$result[$name] = $callback($result); |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
# ------------------------ |
187
|
|
|
|
188
|
|
|
return $result; |
189
|
|
|
} |
190
|
|
|
} |
191
|
|
|
} |
192
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.