|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace NwLaravel\Validation; |
|
4
|
|
|
|
|
5
|
|
|
use ReflectionClass; |
|
6
|
|
|
use Illuminate\Validation\Validator; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class ValidatorResolver |
|
10
|
|
|
* |
|
11
|
|
|
* @method bool validateCpf(string $attribute, mixed $value, array $parameters) |
|
12
|
|
|
* @method bool validateCnpj(string $attribute, mixed $value, array $parameters) |
|
13
|
|
|
*/ |
|
14
|
|
|
class ValidatorResolver extends Validator |
|
15
|
|
|
{ |
|
16
|
|
|
|
|
17
|
|
|
protected $currentRule; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Validate Pattern Valid |
|
21
|
|
|
* |
|
22
|
|
|
* @param string $attribute String Attribute |
|
23
|
|
|
* @param string $value String Value |
|
24
|
|
|
* @param array $parameters Array Parameters |
|
25
|
|
|
* |
|
26
|
|
|
* @return boolean |
|
27
|
|
|
*/ |
|
28
|
1 |
|
public function validatePattern($attribute, $value, $parameters = array()) |
|
|
|
|
|
|
29
|
|
|
{ |
|
30
|
1 |
|
return (bool) (@preg_match($value, "subject") !== false); |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* Validate Current Password |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $attribute String Attribute |
|
37
|
|
|
* @param mixed $value Mixed Value |
|
38
|
|
|
* @param array $parameters Array Parameters |
|
39
|
|
|
* |
|
40
|
|
|
* @return bool |
|
41
|
|
|
*/ |
|
42
|
2 |
|
public function validateCurrentPassword($attribute, $value, $parameters = array()) |
|
|
|
|
|
|
43
|
|
|
{ |
|
44
|
2 |
|
$guard = isset($parameters[0]) ? $parameters[0] : null; |
|
45
|
2 |
|
$field = isset($parameters[1]) ? $parameters[1] : 'password'; |
|
46
|
2 |
|
return password_verify($value, auth($guard)->user()->{$field}); |
|
|
|
|
|
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Validate that an attribute is cpf valid |
|
51
|
|
|
* |
|
52
|
|
|
* @param string $attribute String Attribute |
|
53
|
|
|
* @param mixed $value Mixed Value |
|
54
|
|
|
* @param array $parameters Array Parameters |
|
55
|
|
|
* |
|
56
|
|
|
* @return bool |
|
57
|
|
|
*/ |
|
58
|
1 |
|
public function validateDocument($attribute, $value, $parameters = array()) |
|
59
|
|
|
{ |
|
60
|
1 |
|
$value = preg_replace('/[^0-9]/', '', $value); |
|
61
|
1 |
|
if (strlen($value) == 11) { |
|
62
|
1 |
|
return $this->validateCpf($attribute, $value, $parameters); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
|
return $this->validateCnpj($attribute, $value, $parameters); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Validate currency |
|
70
|
|
|
* |
|
71
|
|
|
* @param string $attribute String Attribute |
|
72
|
|
|
* @param mixed $value Mixed Value |
|
73
|
|
|
* @param array $parameters Array Parameters |
|
74
|
|
|
* |
|
75
|
|
|
* @return bool |
|
76
|
|
|
*/ |
|
77
|
40 |
|
public function validateCurrency($attribute, $value, $parameters = array()) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
40 |
|
return !is_null(asCurrency($value)); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* Validate the not existence of an attribute value in a database table. |
|
84
|
|
|
* |
|
85
|
|
|
* @param string $attribute |
|
86
|
|
|
* @param mixed $value |
|
87
|
|
|
* @param array $parameters |
|
88
|
|
|
* @return bool |
|
89
|
|
|
*/ |
|
90
|
2 |
|
public function validateNotExists($attribute, $value, $parameters) |
|
91
|
|
|
{ |
|
92
|
2 |
|
return !$this->validateExists($attribute, $value, $parameters); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Validate that an attribute exists when another attribute has a given value. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $attribute |
|
99
|
|
|
* @param mixed $value |
|
100
|
|
|
* @param mixed $parameters |
|
101
|
|
|
* @return bool |
|
102
|
|
|
*/ |
|
103
|
1 |
|
public function validateRequiredIfAll($attribute, $value, $parameters) |
|
104
|
|
|
{ |
|
105
|
1 |
|
$this->requireParameterCount(2, $parameters, 'required_if_all'); |
|
106
|
|
|
|
|
107
|
1 |
|
$valid = true; |
|
108
|
1 |
|
$count = count($parameters); |
|
109
|
|
|
|
|
110
|
1 |
|
for ($i = 0; $i < $count; $i += 2) { |
|
111
|
1 |
|
$field = $parameters[$i]; |
|
112
|
1 |
|
$fieldValue = $parameters[$i + 1]; |
|
113
|
|
|
|
|
114
|
1 |
|
if ($fieldValue != $this->getValue($field)) { |
|
115
|
1 |
|
$valid = false; |
|
116
|
1 |
|
break; |
|
117
|
|
|
} |
|
118
|
1 |
|
} |
|
119
|
|
|
|
|
120
|
1 |
|
if ($valid) { |
|
121
|
1 |
|
return $this->validateRequired($attribute, $value); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
1 |
|
return true; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Validate that an attribute exists when another attribute does not have a given value. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $attribute |
|
131
|
|
|
* @param mixed $value |
|
132
|
|
|
* @param mixed $parameters |
|
133
|
|
|
* @return bool |
|
134
|
|
|
*/ |
|
135
|
1 |
|
public function validateRequiredUnlessAll($attribute, $value, $parameters) |
|
136
|
|
|
{ |
|
137
|
1 |
|
$this->requireParameterCount(2, $parameters, 'required_unless_all'); |
|
138
|
|
|
|
|
139
|
1 |
|
$valid = true; |
|
140
|
1 |
|
$count = count($parameters); |
|
141
|
|
|
|
|
142
|
1 |
|
for ($i = 0; $i < $count; $i += 2) { |
|
143
|
1 |
|
$field = $parameters[$i]; |
|
144
|
1 |
|
$fieldValue = $parameters[$i + 1]; |
|
145
|
|
|
|
|
146
|
1 |
|
if ($fieldValue == $this->getValue($field)) { |
|
147
|
1 |
|
$valid = false; |
|
148
|
1 |
|
break; |
|
149
|
|
|
} |
|
150
|
1 |
|
} |
|
151
|
|
|
|
|
152
|
1 |
|
if ($valid) { |
|
153
|
1 |
|
return $this->validateRequired($attribute, $value); |
|
154
|
|
|
} |
|
155
|
|
|
|
|
156
|
1 |
|
return true; |
|
157
|
|
|
} |
|
158
|
|
|
|
|
159
|
|
|
/** |
|
160
|
|
|
* {@inheritdoc} |
|
161
|
|
|
*/ |
|
162
|
9 |
|
public function __call($method, $parameters) |
|
163
|
|
|
{ |
|
164
|
9 |
|
if (preg_match('/^validate([A-Z][a-z][a-zA-Z]*)$/', $method, $match) && count($parameters) >= 2) { |
|
165
|
8 |
|
$className = 'Respect\\Validation\\Rules\\'.ucfirst($match[1]); |
|
166
|
|
|
|
|
167
|
8 |
|
if (class_exists($className)) { |
|
168
|
8 |
|
$reflection = new ReflectionClass($className); |
|
169
|
8 |
|
if (!$reflection->isAbstract() && $reflection->isSubclassOf('Respect\\Validation\\Validatable')) { |
|
170
|
8 |
|
$arguments = (array) (isset($parameters[2]) ? $parameters[2] : []); |
|
171
|
8 |
|
$instance = $reflection->newInstanceArgs($arguments); |
|
172
|
8 |
|
return $instance->validate($parameters[1]); |
|
173
|
|
|
} |
|
174
|
|
|
} |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
1 |
|
return parent::__call($method, $parameters); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.