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
|
|
|
public function validatePattern($attribute, $value, $parameters = array()) |
|
|
|
|
29
|
|
|
{ |
30
|
|
|
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
|
|
|
public function validateCurrentPassword($attribute, $value, $parameters = array()) |
|
|
|
|
43
|
|
|
{ |
44
|
|
|
$guard = isset($parameters[0]) ? $parameters[0] : null; |
45
|
|
|
$field = isset($parameters[1]) ? $parameters[1] : 'password'; |
46
|
|
|
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
|
|
|
public function validateDocument($attribute, $value, $parameters = array()) |
59
|
|
|
{ |
60
|
|
|
$value = preg_replace('/[^0-9]/', '', $value); |
61
|
|
|
if (strlen($value) == 11) { |
62
|
|
|
return $this->validateCpf($attribute, $value, $parameters); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
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
|
|
|
public function validateCurrency($attribute, $value, $parameters = array()) |
|
|
|
|
78
|
|
|
{ |
79
|
|
|
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
|
|
|
public function validateNotExists($attribute, $value, $parameters) |
91
|
|
|
{ |
92
|
|
|
return !$this->validateExists($attribute, $value, $parameters); |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* {@inheritdoc} |
97
|
|
|
*/ |
98
|
|
|
public function __call($method, $parameters) |
99
|
|
|
{ |
100
|
|
|
if (preg_match('/^validate([A-Z][a-z][a-zA-Z]*)$/', $method, $match) && count($parameters) >= 2) { |
101
|
|
|
$className = 'Respect\\Validation\\Rules\\'.ucfirst($match[1]); |
102
|
|
|
|
103
|
|
|
if (class_exists($className)) { |
104
|
|
|
$reflection = new ReflectionClass($className); |
105
|
|
|
if (!$reflection->isAbstract() && $reflection->isSubclassOf('Respect\\Validation\\Validatable')) { |
106
|
|
|
$arguments = (array) (isset($parameters[2]) ? $parameters[2] : []); |
107
|
|
|
$valided = app($className, $arguments)->validate($parameters[1]); |
|
|
|
|
108
|
|
|
} |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
112
|
|
|
if (isset($valided)) { |
113
|
|
|
return $valided; |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
return parent::__call($method, $parameters); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.