1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of dispositif/wikibot application (@github) |
4
|
|
|
* 2019/2020 © Philippe M. <[email protected]> |
5
|
|
|
* For the full copyright and MIT license information, please view the license file. |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
declare(strict_types=1); |
9
|
|
|
|
10
|
|
|
namespace App\Domain\Models\Wiki; |
11
|
|
|
|
12
|
|
|
use App\Domain\Utils\ArrayProcessTrait; |
13
|
|
|
use Exception; |
14
|
|
|
use Throwable; |
15
|
|
|
|
16
|
|
|
abstract class AbstractStrictWikiTemplate extends AbstractParametersObject implements WikiTemplateInterface |
17
|
|
|
{ |
18
|
|
|
use ArrayProcessTrait, InfoTrait; |
19
|
|
|
/** |
20
|
|
|
* Name of the wiki-template |
21
|
|
|
*/ |
22
|
|
|
const WIKITEMPLATE_NAME = 'NO NAME'; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Error in wiki parsing without those required params. |
26
|
|
|
*/ |
27
|
|
|
const REQUIRED_PARAMETERS = []; |
28
|
|
|
/** |
29
|
|
|
* The minimum parameters for pretty wiki-template. |
30
|
|
|
*/ |
31
|
|
|
const MINIMUM_PARAMETERS = []; |
32
|
|
|
|
33
|
|
|
/* commented to allow inherit from Interface in OuvrageTemplate |
34
|
|
|
const PARAM_ALIAS = []; */ |
35
|
|
|
|
36
|
|
|
const COMMENT_STRIPPED = '<!-- Paramètre obligatoire -->'; |
37
|
|
|
|
38
|
|
|
public $log = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* AbstractWikiTemplate constructor. |
42
|
|
|
* |
43
|
|
|
* @throws Exception |
44
|
|
|
*/ |
45
|
|
|
public function __construct() |
46
|
|
|
{ |
47
|
|
|
if (empty(static::MINIMUM_PARAMETERS)) { |
|
|
|
|
48
|
|
|
throw new Exception(sprintf('DEFAULT_PARAMETERS not configured in "%s"', get_called_class())); |
49
|
|
|
} |
50
|
|
|
$this->parametersValues = static::MINIMUM_PARAMETERS; |
51
|
|
|
|
52
|
|
|
if (empty($this->parametersByOrder)) { |
53
|
|
|
$this->parametersByOrder = static::MINIMUM_PARAMETERS; |
54
|
|
|
} |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
public function getParamsAndAlias(): array |
58
|
|
|
{ |
59
|
|
|
return array_merge($this->parametersByOrder, array_keys(static::PARAM_ALIAS)); |
|
|
|
|
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param array $data |
64
|
|
|
* |
65
|
|
|
* @return AbstractStrictWikiTemplate |
66
|
|
|
* @throws Exception |
67
|
|
|
*/ |
68
|
|
|
public function hydrate(array $data): AbstractStrictWikiTemplate |
69
|
|
|
{ |
70
|
|
|
foreach ($data as $name => $value) { |
71
|
|
|
if (is_string($value)) { |
72
|
|
|
$this->hydrateTemplateParameter($name, $value); |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $this; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @param $name |
81
|
|
|
* @param string $value |
82
|
|
|
* |
83
|
|
|
* @throws Exception |
84
|
|
|
*/ |
85
|
|
|
protected function hydrateTemplateParameter($name, string $value): void |
86
|
|
|
{ |
87
|
|
|
if ($this->isValidParamName($name)) { |
88
|
|
|
$this->setParam($name, $value); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* Delete key if empty value and the key not required. |
94
|
|
|
* |
95
|
|
|
* @param array $params |
96
|
|
|
* |
97
|
|
|
* @return array |
98
|
|
|
*/ |
99
|
|
|
protected function keepMinimumOrNotEmpty(array $params): array |
100
|
|
|
{ |
101
|
|
|
$render = []; |
102
|
|
|
foreach ($params as $name => $value) { |
103
|
|
|
if (empty($value) && !isset(static::MINIMUM_PARAMETERS[$name])) { |
104
|
|
|
continue; |
105
|
|
|
} |
106
|
|
|
$render[$name] = $params[$name]; |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
return $render; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
protected function paramsByRenderOrder(): array |
113
|
|
|
{ |
114
|
|
|
$renderParams = []; |
115
|
|
|
|
116
|
|
|
// default order |
117
|
|
|
foreach ($this->parametersByOrder as $order => $paramName) { |
118
|
|
|
if (isset($this->parametersValues[$paramName])) { |
119
|
|
|
$renderParams[$paramName] = $this->parametersValues[$paramName]; |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
return $renderParams; |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
/** |
127
|
|
|
* @param string $name |
128
|
|
|
* |
129
|
|
|
* @return string|null |
130
|
|
|
* @throws Exception |
131
|
|
|
*/ |
132
|
|
|
public function getParam(string $name): ?string |
133
|
|
|
{ |
134
|
|
|
if (!$this->isValidParamName($name)) { |
135
|
|
|
return null; |
136
|
|
|
} |
137
|
|
|
$name = $this->getAliasParam($name); |
138
|
|
|
// keyNum parameter ? |
139
|
|
|
// if (!in_array($name, ['1', '2', '3', '4'])) { |
140
|
|
|
|
141
|
|
|
return ($this->parametersValues[$name]) ?? null; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Todo replacement for old validOrExceptionOnParamName() |
146
|
|
|
* |
147
|
|
|
* @param $name |
148
|
|
|
* |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
|
|
protected function isValidParamName($name): bool |
152
|
|
|
{ |
153
|
|
|
if (is_int($name)) { |
154
|
|
|
$name = (string)$name; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
// that parameter exists in template ? |
158
|
|
|
if (in_array($name, $this->parametersByOrder) |
159
|
|
|
|| array_key_exists($name, static::PARAM_ALIAS) |
|
|
|
|
160
|
|
|
) { |
161
|
|
|
return true; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
return false; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* @param string $name |
169
|
|
|
* |
170
|
|
|
* @return $this |
171
|
|
|
* @throws Exception |
172
|
|
|
*/ |
173
|
|
|
public function unsetParam(string $name) |
174
|
|
|
{ |
175
|
|
|
if (!$this->isValidParamName($name)) { |
176
|
|
|
throw new Exception(sprintf('no parameter "%s" in template "%s"', $name, get_called_class())); |
177
|
|
|
} |
178
|
|
|
$name = $this->getAliasParam($name); |
179
|
|
|
unset($this->parametersValues[$name]); |
180
|
|
|
|
181
|
|
|
return $this; |
182
|
|
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* TODO ? check if method set{ParamName} exists ? |
186
|
|
|
* |
187
|
|
|
* @param string $name |
188
|
|
|
* @param string $value |
189
|
|
|
* |
190
|
|
|
* @return AbstractParametersObject |
191
|
|
|
* @throws Exception |
192
|
|
|
*/ |
193
|
|
|
public function setParam(string $name, string $value): AbstractParametersObject |
194
|
|
|
{ |
195
|
|
|
if (!$this->isValidParamName($name)) { |
196
|
|
|
$this->log[] = sprintf('no parameter "%s" in AbstractParametersObject "%s"', $name, get_called_class()); |
197
|
|
|
|
198
|
|
|
return $this; |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
$name = $this->getAliasParam($name); |
202
|
|
|
$value = trim($value); |
203
|
|
|
if (!empty($value) || $this->parametersValues[$name]) { |
204
|
|
|
$this->parametersValues[$name] = $value; |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* todo peu utile en public |
212
|
|
|
* |
213
|
|
|
* @param string $name |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
public function getAliasParam(string $name): string |
218
|
|
|
{ |
219
|
|
|
if (array_key_exists($name, static::PARAM_ALIAS)) { |
|
|
|
|
220
|
|
|
$name = static::PARAM_ALIAS[$name]; |
221
|
|
|
} |
222
|
|
|
|
223
|
|
|
return $name; |
224
|
|
|
} |
225
|
|
|
|
226
|
|
|
/** |
227
|
|
|
* For a parameter, check is the value exists (not empty). |
228
|
|
|
* |
229
|
|
|
* @param string $name |
230
|
|
|
* |
231
|
|
|
* @return bool |
232
|
|
|
* @throws Exception |
233
|
|
|
*/ |
234
|
|
|
public function hasParamValue(string $name): bool |
235
|
|
|
{ |
236
|
|
|
try { |
237
|
|
|
if (!empty(trim($this->getParam($name)))) { |
238
|
|
|
return true; |
239
|
|
|
} |
240
|
|
|
} catch (Throwable $e) { |
241
|
|
|
unset($e); |
242
|
|
|
} |
243
|
|
|
|
244
|
|
|
return false; |
245
|
|
|
} |
246
|
|
|
|
247
|
|
|
/** |
248
|
|
|
* Verify the required template parameters for an edit by the bot. |
249
|
|
|
* |
250
|
|
|
* @return bool |
251
|
|
|
* @throws Exception |
252
|
|
|
* @throws Exception |
253
|
|
|
* @throws Exception |
254
|
|
|
* @throws Exception |
255
|
|
|
*/ |
256
|
|
|
public function isValidForEdit(): bool |
257
|
|
|
{ |
258
|
|
|
$validParams = array_keys(static::MINIMUM_PARAMETERS); |
259
|
|
|
if (!empty(static::REQUIRED_PARAMETERS)) { |
260
|
|
|
$validParams = static::REQUIRED_PARAMETERS; |
261
|
|
|
} |
262
|
|
|
|
263
|
|
|
foreach ($validParams as $param) { |
264
|
|
|
if (in_array($param, ['date', 'année']) |
265
|
|
|
&& ($this->hasParamValue('date') || $this->hasParamValue('année')) |
266
|
|
|
) { |
267
|
|
|
// équivalence date-année |
268
|
|
|
continue; |
269
|
|
|
} |
270
|
|
|
if (!$this->hasParamValue($param)) { |
271
|
|
|
return false; |
272
|
|
|
} |
273
|
|
|
} |
274
|
|
|
|
275
|
|
|
return true; |
276
|
|
|
} |
277
|
|
|
|
278
|
|
|
public abstract function serialize(?bool $cleanOrder = false): string; |
279
|
|
|
|
280
|
|
|
} |
281
|
|
|
|