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