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