|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Telkins\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Contracts\Validation\Rule; |
|
6
|
|
|
use Illuminate\Support\Facades\Validator; |
|
7
|
|
|
use Telkins\Validation\Contracts\FieldRuleSetContract; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Source/inspiration: https://medium.com/@juampi92/laravel-5-5-validation-ruleception-rule-inside-rule-2762d2cf4471 |
|
11
|
|
|
*/ |
|
12
|
|
|
abstract class AbstractFieldRuleSet implements Rule, FieldRuleSetContract |
|
13
|
|
|
{ |
|
14
|
|
|
protected $data = []; |
|
15
|
|
|
|
|
16
|
|
|
protected $except = []; |
|
17
|
|
|
|
|
18
|
|
|
protected $validator; |
|
19
|
|
|
|
|
20
|
|
|
public function __construct(array $data = []) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->data = $data; |
|
23
|
|
|
} |
|
24
|
|
|
|
|
25
|
|
|
/** |
|
26
|
|
|
* Use the rule set without the specified rules. |
|
27
|
|
|
* |
|
28
|
|
|
* @param mixed $rules |
|
29
|
|
|
* @return self |
|
30
|
|
|
*/ |
|
31
|
|
|
public function except($rules): self |
|
32
|
|
|
{ |
|
33
|
|
|
$this->except = ! is_array($rules) ? func_get_args() : $rules; |
|
34
|
|
|
|
|
35
|
|
|
return $this; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* Determine if the validation rule passes. |
|
40
|
|
|
* |
|
41
|
|
|
* @param string $attribute |
|
42
|
|
|
* @param mixed $value |
|
43
|
|
|
* @return bool |
|
44
|
|
|
*/ |
|
45
|
|
|
public function passes($attribute, $value) |
|
46
|
|
|
{ |
|
47
|
|
|
return $this->validate($value, $this->rules(), $attribute); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
abstract public function rules() : array; |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @param mixed $value |
|
54
|
|
|
* @param array|string|Rule $rules |
|
55
|
|
|
* @param string $name Name of the property (optional) |
|
56
|
|
|
* |
|
57
|
|
|
* @return boolean |
|
58
|
|
|
*/ |
|
59
|
|
|
protected function validate($value, $rules, $name = 'variable') |
|
60
|
|
|
{ |
|
61
|
|
|
$rules = $this->prepareRules($rules); |
|
62
|
|
|
|
|
63
|
|
|
$data = empty($this->data) ? [$name => $value] : $this->data; |
|
64
|
|
|
|
|
65
|
|
|
$this->validator = Validator::make($data, [$name => $rules]); |
|
66
|
|
|
|
|
67
|
|
|
return $this->validator->passes(); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
/** |
|
71
|
|
|
* Prepare the rules. |
|
72
|
|
|
* |
|
73
|
|
|
* @param mixed $rules |
|
74
|
|
|
* @return array |
|
75
|
|
|
*/ |
|
76
|
|
|
protected function prepareRules($rules): array |
|
77
|
|
|
{ |
|
78
|
|
|
if (!is_string($rules) && !is_array($rules)) { |
|
79
|
|
|
$rules = [$rules]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return collect($rules) |
|
83
|
|
|
->reject(function ($rule) { |
|
84
|
|
|
return in_array($rule, $this->except); |
|
85
|
|
|
})->toArray(); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Get the validation error message. |
|
90
|
|
|
* |
|
91
|
|
|
* @return string |
|
92
|
|
|
*/ |
|
93
|
|
|
public function message() |
|
94
|
|
|
{ |
|
95
|
|
|
$errors = $this->validator->errors(); |
|
96
|
|
|
|
|
97
|
|
|
if ($errors->any()) { |
|
98
|
|
|
return $errors->first(); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
return 'The :attribute is not valid.'; |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|