1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Cerbero\CommandValidator; |
4
|
|
|
|
5
|
|
|
use Symfony\Component\Console\Input\InputInterface; |
6
|
|
|
use Symfony\Component\Console\Output\OutputInterface; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Validate input submitted to console commands. |
10
|
|
|
* |
11
|
|
|
* @author Andrea Marco Sartori |
12
|
|
|
*/ |
13
|
|
|
trait ValidatesInput |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* @author Andrea Marco Sartori |
17
|
|
|
* @var Illuminate\Validation\Validator $validator Input validator. |
18
|
|
|
*/ |
19
|
|
|
protected $validator; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* Execute the console command. |
23
|
|
|
* |
24
|
|
|
* @param \Symfony\Component\Console\Input\InputInterface $input |
25
|
|
|
* @param \Symfony\Component\Console\Output\OutputInterface $output |
26
|
|
|
* @return mixed |
27
|
|
|
*/ |
28
|
|
|
protected function execute(InputInterface $input, OutputInterface $output) |
29
|
|
|
{ |
30
|
|
|
if ($this->validator()->passes()) { |
31
|
|
|
return parent::execute($input, $output); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
return $this->error($this->getFormattedErrors()); |
|
|
|
|
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* Retrieve the input validator. |
39
|
|
|
* |
40
|
|
|
* @author Andrea Marco Sartori |
41
|
|
|
* @return Illuminate\Validation\Validator |
42
|
|
|
*/ |
43
|
|
|
protected function validator() |
44
|
|
|
{ |
45
|
|
|
return $this->validator = $this->validator ?: $this->laravel['validator']->make( |
|
|
|
|
46
|
|
|
$this->filteredInput(), $this->rules(), $this->messages(), $this->attributes() |
47
|
|
|
); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Filter the input to avoid the validation of optional values. |
52
|
|
|
* |
53
|
|
|
* @author Andrea Marco Sartori |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
|
|
private function filteredInput() |
57
|
|
|
{ |
58
|
|
|
$input = array_merge($this->argument(), $this->option()); |
|
|
|
|
59
|
|
|
|
60
|
|
|
return array_filter($input, function ($value) { |
61
|
|
|
return $value !== null; |
62
|
|
|
}); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Retrieve and format the validation errors. |
67
|
|
|
* |
68
|
|
|
* @author Andrea Marco Sartori |
69
|
|
|
* @return string |
70
|
|
|
*/ |
71
|
|
|
protected function getFormattedErrors() |
72
|
|
|
{ |
73
|
|
|
$errors = implode("\n", $this->validator()->errors()->all()); |
74
|
|
|
|
75
|
|
|
return "\n\n{$errors}\n"; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Retrieve the validation rules. |
80
|
|
|
* |
81
|
|
|
* @author Andrea Marco Sartori |
82
|
|
|
* @return array |
83
|
|
|
*/ |
84
|
|
|
abstract protected function rules(); |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Retrieve the custom error messages. |
88
|
|
|
* |
89
|
|
|
* @author Andrea Marco Sartori |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
|
|
protected function messages() |
93
|
|
|
{ |
94
|
|
|
return []; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Retrieve the custom attributes for error messages. |
99
|
|
|
* |
100
|
|
|
* @author Andrea Marco Sartori |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
|
|
protected function attributes() |
104
|
|
|
{ |
105
|
|
|
return []; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.