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
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* The trait to validate console commands input. |
11
|
|
|
* |
12
|
|
|
*/ |
13
|
|
|
trait ValidatesInput |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* The command input validator. |
17
|
|
|
* |
18
|
|
|
* @var \Illuminate\Contracts\Validation\Validator |
19
|
|
|
*/ |
20
|
|
|
protected $validator; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Retrieve the rules to validate data against |
24
|
|
|
* |
25
|
|
|
* @return array |
26
|
|
|
*/ |
27
|
|
|
abstract protected function rules() : array; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Execute the console command. |
31
|
|
|
* |
32
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
33
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
34
|
|
|
* @return mixed |
35
|
|
|
*/ |
36
|
6 |
|
protected function execute(InputInterface $input, OutputInterface $output) |
37
|
|
|
{ |
38
|
6 |
|
if ($this->validator()->fails()) { |
39
|
3 |
|
$this->error($this->formatErrors()); |
|
|
|
|
40
|
|
|
// Exit with status code 1 |
41
|
3 |
|
return 1; |
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
|
|
|
* Retrieve the custom error messages |
82
|
|
|
* |
83
|
|
|
* @return array |
84
|
|
|
*/ |
85
|
|
|
protected function messages() : array |
86
|
|
|
{ |
87
|
|
|
return []; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Retrieve the custom attribute names for error messages |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
protected function attributes() : array |
96
|
|
|
{ |
97
|
|
|
return []; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Format the validation errors |
102
|
|
|
* |
103
|
|
|
* @return string |
104
|
|
|
*/ |
105
|
3 |
|
protected function formatErrors() : string |
106
|
|
|
{ |
107
|
3 |
|
$errors = implode(PHP_EOL, $this->getErrorMessages()); |
108
|
|
|
|
109
|
3 |
|
return PHP_EOL . PHP_EOL . $errors . PHP_EOL; |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
/** |
113
|
|
|
* Retrieve the error messages |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
3 |
|
protected function getErrorMessages() : array |
118
|
|
|
{ |
119
|
3 |
|
return $this->validator()->errors()->all(); |
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|