|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* Platine Framework |
|
5
|
|
|
* |
|
6
|
|
|
* Platine Framework is a lightweight, high-performance, simple and elegant |
|
7
|
|
|
* PHP Web framework |
|
8
|
|
|
* |
|
9
|
|
|
* This content is released under the MIT License (MIT) |
|
10
|
|
|
* |
|
11
|
|
|
* Copyright (c) 2020 Platine Framework |
|
12
|
|
|
* |
|
13
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining a copy |
|
14
|
|
|
* of this software and associated documentation files (the "Software"), to deal |
|
15
|
|
|
* in the Software without restriction, including without limitation the rights |
|
16
|
|
|
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
|
17
|
|
|
* copies of the Software, and to permit persons to whom the Software is |
|
18
|
|
|
* furnished to do so, subject to the following conditions: |
|
19
|
|
|
* |
|
20
|
|
|
* The above copyright notice and this permission notice shall be included in all |
|
21
|
|
|
* copies or substantial portions of the Software. |
|
22
|
|
|
* |
|
23
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
|
24
|
|
|
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
|
25
|
|
|
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
|
26
|
|
|
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
|
27
|
|
|
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
|
28
|
|
|
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
|
29
|
|
|
* SOFTWARE. |
|
30
|
|
|
*/ |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @file MakeValidatorCommand.php |
|
34
|
|
|
* |
|
35
|
|
|
* The Make validator Command class |
|
36
|
|
|
* |
|
37
|
|
|
* @package Platine\Framework\Console\Command |
|
38
|
|
|
* @author Platine Developers team |
|
39
|
|
|
* @copyright Copyright (c) 2020 |
|
40
|
|
|
* @license http://opensource.org/licenses/MIT MIT License |
|
41
|
|
|
* @link http://www.iacademy.cf |
|
42
|
|
|
* @version 1.0.0 |
|
43
|
|
|
* @filesource |
|
44
|
|
|
*/ |
|
45
|
|
|
|
|
46
|
|
|
declare(strict_types=1); |
|
47
|
|
|
|
|
48
|
|
|
namespace Platine\Framework\Console\Command; |
|
49
|
|
|
|
|
50
|
|
|
use Platine\Console\Input\Reader; |
|
51
|
|
|
use Platine\Console\Output\Writer; |
|
52
|
|
|
use Platine\Filesystem\Filesystem; |
|
53
|
|
|
use Platine\Framework\App\Application; |
|
54
|
|
|
use Platine\Framework\Console\MakeCommand; |
|
55
|
|
|
use Platine\Stdlib\Helper\Str; |
|
56
|
|
|
use ReflectionClass; |
|
57
|
|
|
use ReflectionMethod; |
|
58
|
|
|
|
|
59
|
|
|
/** |
|
60
|
|
|
* @class MakeValidatorCommand |
|
61
|
|
|
* @package Platine\Framework\Console\Command |
|
62
|
|
|
*/ |
|
63
|
|
|
class MakeValidatorCommand extends MakeCommand |
|
64
|
|
|
{ |
|
65
|
|
|
/** |
|
66
|
|
|
* {@inheritdoc} |
|
67
|
|
|
*/ |
|
68
|
|
|
protected string $type = 'validator'; |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* The form parameter class name |
|
72
|
|
|
* @var string |
|
73
|
|
|
*/ |
|
74
|
|
|
protected string $paramClass; |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Create new instance |
|
78
|
|
|
* @param Application $application |
|
79
|
|
|
* @param Filesystem $filesystem |
|
80
|
|
|
*/ |
|
81
|
|
|
public function __construct( |
|
82
|
|
|
Application $application, |
|
83
|
|
|
Filesystem $filesystem |
|
84
|
|
|
) { |
|
85
|
|
|
parent::__construct($application, $filesystem); |
|
86
|
|
|
$this->setName('make:validator') |
|
87
|
|
|
->setDescription('Command to generate new validator class'); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* {@inheritdoc} |
|
92
|
|
|
*/ |
|
93
|
|
|
public function interact(Reader $reader, Writer $writer): void |
|
94
|
|
|
{ |
|
95
|
|
|
parent::interact($reader, $writer); |
|
96
|
|
|
|
|
97
|
|
|
|
|
98
|
|
|
$io = $this->io(); |
|
99
|
|
|
|
|
100
|
|
|
$paramClass = $io->prompt('Enter the form parameter full class name', null); |
|
101
|
|
|
while (!class_exists($paramClass)) { |
|
102
|
|
|
$paramClass = $io->prompt('Class does not exists, please enter the form parameter full class name', null); |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
$this->paramClass = $paramClass; |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
/** |
|
109
|
|
|
* {@inheritdoc} |
|
110
|
|
|
*/ |
|
111
|
|
|
public function getClassTemplate(): string |
|
112
|
|
|
{ |
|
113
|
|
|
return <<<EOF |
|
114
|
|
|
<?php |
|
115
|
|
|
|
|
116
|
|
|
declare(strict_types=1); |
|
117
|
|
|
|
|
118
|
|
|
namespace %namespace%; |
|
119
|
|
|
|
|
120
|
|
|
use Platine\Framework\Form\Validator\AbstractValidator; |
|
121
|
|
|
use Platine\Lang\Lang; |
|
122
|
|
|
%uses% |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @class %classname% |
|
126
|
|
|
* @package %namespace% |
|
127
|
|
|
*/ |
|
128
|
|
|
class %classname% extends AbstractValidator |
|
129
|
|
|
{ |
|
130
|
|
|
/** |
|
131
|
|
|
* The parameter instance |
|
132
|
|
|
* @var %param_class% |
|
133
|
|
|
*/ |
|
134
|
|
|
protected %param_class% \$param; |
|
135
|
|
|
|
|
136
|
|
|
/** |
|
137
|
|
|
* Create new instance |
|
138
|
|
|
* @param %param_class% \$param |
|
139
|
|
|
* @param Lang \$lang |
|
140
|
|
|
*/ |
|
141
|
|
|
public function __construct(%param_class% \$param, Lang \$lang) |
|
142
|
|
|
{ |
|
143
|
|
|
parent::__construct(\$lang); |
|
144
|
|
|
\$this->param = \$param; |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* {@inheritdoc} |
|
149
|
|
|
*/ |
|
150
|
|
|
public function setValidationData(): void |
|
151
|
|
|
{ |
|
152
|
|
|
%validation_data_body% |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* {@inheritdoc} |
|
157
|
|
|
*/ |
|
158
|
|
|
public function setValidationRules(): void |
|
159
|
|
|
{ |
|
160
|
|
|
%validation_rules_body% |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
EOF; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
/** |
|
167
|
|
|
* {@inheritdoc} |
|
168
|
|
|
*/ |
|
169
|
|
|
protected function createClass(): string |
|
170
|
|
|
{ |
|
171
|
|
|
$content = parent::createClass(); |
|
172
|
|
|
|
|
173
|
|
|
$formParameter = $this->replaceFormParameterName($content); |
|
174
|
|
|
$validationDataBody = $this->getValidationDataBody($formParameter); |
|
175
|
|
|
|
|
176
|
|
|
return $this->getValidationRulesBody($validationDataBody); |
|
177
|
|
|
} |
|
178
|
|
|
|
|
179
|
|
|
/** |
|
180
|
|
|
* Return the list of parameters properties |
|
181
|
|
|
* @return array<string, string> |
|
182
|
|
|
*/ |
|
183
|
|
|
protected function getParameterProperties(): array |
|
184
|
|
|
{ |
|
185
|
|
|
$list = []; |
|
186
|
|
|
$reflection = new ReflectionClass($this->paramClass); |
|
187
|
|
|
$methods = $reflection->getMethods(ReflectionMethod::IS_PUBLIC); |
|
188
|
|
|
|
|
189
|
|
|
if (!empty($methods)) { |
|
190
|
|
|
foreach ($methods as /** @var ReflectionMethod $method */ $method) { |
|
191
|
|
|
$returnType = $method->getReturnType(); |
|
192
|
|
|
if ($returnType !== null && $returnType->getName() === 'string') { |
|
|
|
|
|
|
193
|
|
|
$name = $method->name; |
|
194
|
|
|
if (substr($name, 0, 3) === 'get') { |
|
195
|
|
|
$field = str_replace('get', '', $name); |
|
196
|
|
|
$list[Str::snake($field)] = $name; |
|
197
|
|
|
} |
|
198
|
|
|
} |
|
199
|
|
|
} |
|
200
|
|
|
} |
|
201
|
|
|
|
|
202
|
|
|
return $list; |
|
203
|
|
|
} |
|
204
|
|
|
|
|
205
|
|
|
/** |
|
206
|
|
|
* Return the validation rules body |
|
207
|
|
|
* @param string $content |
|
208
|
|
|
* @return string |
|
209
|
|
|
*/ |
|
210
|
|
|
protected function getValidationRulesBody(string $content): string |
|
211
|
|
|
{ |
|
212
|
|
|
$result = ''; |
|
213
|
|
|
foreach ($this->getParameterProperties() as $field => $getter) { |
|
214
|
|
|
$result .= <<<EOF |
|
215
|
|
|
\$this->addRules('$field', [ |
|
216
|
|
|
|
|
217
|
|
|
]); |
|
218
|
|
|
|
|
219
|
|
|
|
|
220
|
|
|
EOF; |
|
221
|
|
|
} |
|
222
|
|
|
return str_replace('%validation_rules_body%', $result, $content); |
|
223
|
|
|
} |
|
224
|
|
|
|
|
225
|
|
|
/** |
|
226
|
|
|
* Return the validation data body |
|
227
|
|
|
* @param string $content |
|
228
|
|
|
* @return string |
|
229
|
|
|
*/ |
|
230
|
|
|
protected function getValidationDataBody(string $content): string |
|
231
|
|
|
{ |
|
232
|
|
|
$result = ''; |
|
233
|
|
|
foreach ($this->getParameterProperties() as $field => $getter) { |
|
234
|
|
|
$result .= <<<EOF |
|
235
|
|
|
\$this->addData('$field', \$this->param->$getter()); |
|
236
|
|
|
|
|
237
|
|
|
EOF; |
|
238
|
|
|
} |
|
239
|
|
|
|
|
240
|
|
|
return str_replace('%validation_data_body%', $result, $content); |
|
241
|
|
|
} |
|
242
|
|
|
|
|
243
|
|
|
/** |
|
244
|
|
|
* {@inheritdoc} |
|
245
|
|
|
*/ |
|
246
|
|
|
protected function getUsesContent(): string |
|
247
|
|
|
{ |
|
248
|
|
|
return $this->getUsesTemplate($this->paramClass); |
|
249
|
|
|
} |
|
250
|
|
|
|
|
251
|
|
|
/** |
|
252
|
|
|
* Replace the form parameter name the entity body |
|
253
|
|
|
* @param string $content |
|
254
|
|
|
* @return string |
|
255
|
|
|
*/ |
|
256
|
|
|
protected function replaceFormParameterName(string $content): string |
|
257
|
|
|
{ |
|
258
|
|
|
$paramName = basename($this->paramClass); |
|
259
|
|
|
|
|
260
|
|
|
return str_replace('%param_class%', $paramName, $content); |
|
261
|
|
|
} |
|
262
|
|
|
} |
|
263
|
|
|
|