1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Win\Validation; |
4
|
|
|
|
5
|
|
|
class Validator |
6
|
|
|
{ |
7
|
|
|
private $data = []; |
8
|
|
|
private $errors = []; |
9
|
|
|
|
10
|
|
|
const INDEX_NAME = 0; |
11
|
|
|
const INDEX_RULES = 1; |
12
|
|
|
const INDEX_MESSAGES = 'messages'; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Cria um validador |
16
|
|
|
* @param mixed[] $data |
17
|
|
|
* @return static |
18
|
|
|
*/ |
19
|
|
|
public static function create($data) |
20
|
|
|
{ |
21
|
|
|
return new static($data); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/* Cria um validador |
25
|
|
|
* @param mixed[] $data |
26
|
|
|
* @return static |
27
|
|
|
*/ |
28
|
|
|
public function __construct($data) |
29
|
|
|
{ |
30
|
|
|
$this->data = $data; |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Retorna nome da validação |
35
|
|
|
* @param string[] $validation |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
protected function getName($validation) |
39
|
|
|
{ |
40
|
|
|
return $validation[self::INDEX_NAME]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Retorna as regras da validação |
45
|
|
|
* @param string[] $validation |
46
|
|
|
* @return string[] |
47
|
|
|
*/ |
48
|
|
|
protected function getRules($validation) |
49
|
|
|
{ |
50
|
|
|
if (key_exists(self::INDEX_RULES, $validation)) { |
51
|
|
|
return explode('|', $validation[self::INDEX_RULES]); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
return []; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* Retorna a mensagem personalizada desta validação |
59
|
|
|
* @param string[] $validation |
60
|
|
|
* @param string $rule |
61
|
|
|
* @return string|null |
62
|
|
|
*/ |
63
|
|
|
protected function getMessage($rule, $validation) |
64
|
|
|
{ |
65
|
|
|
if (key_exists(self::INDEX_MESSAGES, $validation)) { |
66
|
|
|
if (key_exists($rule, $validation[self::INDEX_MESSAGES])) { |
|
|
|
|
67
|
|
|
return $validation[self::INDEX_MESSAGES][$rule]; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return null; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Valida e retorna os dados válidos |
76
|
|
|
* @param string[] $validations |
77
|
|
|
* @return mixed[] |
78
|
|
|
*/ |
79
|
|
|
public function validate($validations) |
80
|
|
|
{ |
81
|
|
|
foreach ($validations as $index => $validation) { |
82
|
|
|
$name = $this->getName($validation); |
|
|
|
|
83
|
|
|
$rules = $this->getRules($validation); |
|
|
|
|
84
|
|
|
|
85
|
|
|
foreach ($rules as $rule) { |
86
|
|
|
$message = $this->getMessage($rule, $validation); |
|
|
|
|
87
|
|
|
if (!$this->isFieldValid($index, $rule, $name, $message)) { |
88
|
|
|
break; |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $this->data; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Retorna TRUE se o campo é valido |
98
|
|
|
* @param string $index |
99
|
|
|
* @param string $rule |
100
|
|
|
* @param string $name |
101
|
|
|
* @param string|null $message |
102
|
|
|
* @return bool |
103
|
|
|
*/ |
104
|
|
|
private function isFieldValid($index, $rule, $name, &$message = null) |
105
|
|
|
{ |
106
|
|
|
if (key_exists($index, $this->data)) { |
107
|
|
|
if (!Rules::isValid($this->data[$index], $rule)) { |
108
|
|
|
if (!$message) { |
109
|
|
|
$message = Rules::getError(); |
110
|
|
|
} |
111
|
|
|
$this->errors[] = str_replace(':name', $name, $message); |
112
|
|
|
} |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return !$this->hasError(); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Retorna TRUE se tem algum erro |
120
|
|
|
* @return bool |
121
|
|
|
*/ |
122
|
|
|
public function hasError() |
123
|
|
|
{ |
124
|
|
|
return count($this->errors) > 0; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Retorna a mensagem de erro |
129
|
|
|
* @return string|null |
130
|
|
|
*/ |
131
|
|
|
public function getError() |
132
|
|
|
{ |
133
|
|
|
if (count($this->errors) > 0) { |
134
|
|
|
return $this->errors[0]; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
return null; |
138
|
|
|
} |
139
|
|
|
} |
140
|
|
|
|