|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace Cerbero\CommandValidator; |
|
6
|
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Validation\Validator; |
|
8
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
|
9
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* The trait to validate the input of console commands. |
|
13
|
|
|
*/ |
|
14
|
|
|
trait ValidatesInput |
|
15
|
|
|
{ |
|
16
|
|
|
/** |
|
17
|
|
|
* The validator. |
|
18
|
|
|
*/ |
|
19
|
|
|
protected Validator $validator; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* Retrieve the validation rules. |
|
23
|
|
|
* |
|
24
|
|
|
* @return array<string, mixed> |
|
25
|
|
|
*/ |
|
26
|
|
|
abstract protected function rules(): array; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* Execute the console command. |
|
30
|
|
|
*/ |
|
31
|
3 |
|
protected function execute(InputInterface $input, OutputInterface $output): int |
|
32
|
|
|
{ |
|
33
|
3 |
|
if ($this->validator()->fails()) { |
|
34
|
2 |
|
$this->printErrors($this->formatErrors()); |
|
35
|
|
|
|
|
36
|
2 |
|
return static::FAILURE; |
|
|
|
|
|
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
1 |
|
return parent::execute($input, $output); |
|
40
|
|
|
} |
|
41
|
|
|
|
|
42
|
|
|
/** |
|
43
|
|
|
* Retrieve the validator. |
|
44
|
|
|
*/ |
|
45
|
3 |
|
protected function validator(): Validator |
|
46
|
|
|
{ |
|
47
|
|
|
/** @phpstan-ignore-next-line */ |
|
48
|
3 |
|
return $this->validator ??= $this->laravel['validator']->make( |
|
49
|
3 |
|
$this->getDataToValidate(), |
|
50
|
3 |
|
$this->rules(), |
|
51
|
3 |
|
$this->messages(), |
|
52
|
3 |
|
$this->attributes(), |
|
53
|
3 |
|
); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Retrieve the data to validate. |
|
58
|
|
|
* |
|
59
|
|
|
* @return array<string, mixed> |
|
60
|
|
|
*/ |
|
61
|
3 |
|
protected function getDataToValidate(): array |
|
62
|
|
|
{ |
|
63
|
3 |
|
return array_filter([...$this->argument(), ...$this->option()], fn(mixed $value) => $value !== null); |
|
|
|
|
|
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Print the given errors to the console. |
|
68
|
|
|
*/ |
|
69
|
2 |
|
protected function printErrors(string $errors): void |
|
70
|
|
|
{ |
|
71
|
2 |
|
$this->output->block($errors, style: 'fg=white;bg=red', prefix: ' ', padding: true); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Format the validation errors. |
|
76
|
|
|
*/ |
|
77
|
2 |
|
protected function formatErrors(): string |
|
78
|
|
|
{ |
|
79
|
2 |
|
return implode(PHP_EOL, $this->validator()->errors()->all()); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Retrieve the custom error messages. |
|
84
|
|
|
* |
|
85
|
|
|
* @return array<string, string> |
|
86
|
|
|
*/ |
|
87
|
1 |
|
protected function messages(): array |
|
88
|
|
|
{ |
|
89
|
1 |
|
return []; |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Retrieve the custom error attributes. |
|
94
|
|
|
* |
|
95
|
|
|
* @return array<string, string> |
|
96
|
|
|
*/ |
|
97
|
1 |
|
protected function attributes(): array |
|
98
|
|
|
{ |
|
99
|
1 |
|
return []; |
|
100
|
|
|
} |
|
101
|
|
|
} |
|
102
|
|
|
|