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
|
|
|
* Validate Pattern Valid |
18
|
|
|
* |
19
|
|
|
* @param string $attribute String Attribute |
20
|
|
|
* @param string $value String Value |
21
|
|
|
* @param array $parameters Array Parameters |
22
|
|
|
* |
23
|
|
|
* @return boolean |
24
|
|
|
*/ |
25
|
1 |
|
public function validatePattern($attribute, $value, $parameters = array()) |
|
|
|
|
26
|
|
|
{ |
27
|
1 |
|
return (bool) (@preg_match($value, "subject") !== false); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Validate Current Password |
32
|
|
|
* |
33
|
|
|
* @param string $attribute String Attribute |
34
|
|
|
* @param mixed $value Mixed Value |
35
|
|
|
* @param array $parameters Array Parameters |
36
|
|
|
* |
37
|
|
|
* @return bool |
38
|
|
|
*/ |
39
|
2 |
|
public function validateCurrentPassword($attribute, $value, $parameters = array()) |
|
|
|
|
40
|
|
|
{ |
41
|
2 |
|
$guard = isset($parameters[0]) ? $parameters[0] : null; |
42
|
2 |
|
$field = isset($parameters[1]) ? $parameters[1] : 'password'; |
43
|
2 |
|
return password_verify($value, auth($guard)->user()->{$field}); |
|
|
|
|
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* Validate that an attribute is cpf valid |
48
|
|
|
* |
49
|
|
|
* @param string $attribute String Attribute |
50
|
|
|
* @param mixed $value Mixed Value |
51
|
|
|
* @param array $parameters Array Parameters |
52
|
|
|
* |
53
|
|
|
* @return bool |
54
|
|
|
*/ |
55
|
1 |
|
public function validateDocument($attribute, $value, $parameters = array()) |
56
|
|
|
{ |
57
|
1 |
|
$value = preg_replace('/[^0-9]/', '', $value); |
58
|
1 |
|
if (strlen($value) == 11) { |
59
|
1 |
|
return $this->validateCpf($attribute, $value, $parameters); |
60
|
|
|
} |
61
|
|
|
|
62
|
1 |
|
return $this->validateCnpj($attribute, $value, $parameters); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Validate currency |
67
|
|
|
* |
68
|
|
|
* @param string $attribute String Attribute |
69
|
|
|
* @param mixed $value Mixed Value |
70
|
|
|
* @param array $parameters Array Parameters |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
40 |
|
public function validateCurrency($attribute, $value, $parameters = array()) |
|
|
|
|
75
|
|
|
{ |
76
|
40 |
|
return !is_null(asCurrency($value)); |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Validate the not existence of an attribute value in a database table. |
81
|
|
|
* |
82
|
|
|
* @param string $attribute |
83
|
|
|
* @param mixed $value |
84
|
|
|
* @param array $parameters |
85
|
|
|
* @return bool |
86
|
|
|
*/ |
87
|
2 |
|
public function validateNotExists($attribute, $value, $parameters) |
88
|
|
|
{ |
89
|
2 |
|
return !$this->validateExists($attribute, $value, $parameters); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
/** |
93
|
|
|
* {@inheritdoc} |
94
|
|
|
*/ |
95
|
9 |
|
public function __call($method, $parameters) |
96
|
|
|
{ |
97
|
9 |
|
if (preg_match('/^validate([A-Z][a-z][a-zA-Z]*)$/', $method, $match) && count($parameters) >= 2) { |
98
|
8 |
|
$className = 'Respect\\Validation\\Rules\\'.ucfirst($match[1]); |
99
|
|
|
|
100
|
8 |
|
if (class_exists($className)) { |
101
|
8 |
|
$reflection = new ReflectionClass($className); |
102
|
8 |
|
if (!$reflection->isAbstract() && $reflection->isSubclassOf('Respect\\Validation\\Validatable')) { |
103
|
8 |
|
$arguments = (array) (isset($parameters[2]) ? $parameters[2] : []); |
104
|
8 |
|
$valided = app($className, $arguments)->validate($parameters[1]); |
105
|
8 |
|
} |
106
|
8 |
|
} |
107
|
8 |
|
} |
108
|
|
|
|
109
|
9 |
|
if (isset($valided)) { |
110
|
8 |
|
return $valided; |
111
|
|
|
} |
112
|
|
|
|
113
|
1 |
|
return parent::__call($method, $parameters); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.