1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace IndraGunawan\RestService\Validator; |
4
|
|
|
|
5
|
|
|
use IndraGunawan\RestService\Exception\ValidatorException; |
6
|
|
|
use Sirius\Validation\Validator as SiriusValidator; |
7
|
|
|
|
8
|
|
|
class Validator |
9
|
|
|
{ |
10
|
|
|
/** |
11
|
|
|
* @var \Sirius\Validation\Validator |
12
|
|
|
*/ |
13
|
|
|
private $validator; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var array |
17
|
|
|
*/ |
18
|
|
|
private $datas; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var array |
22
|
|
|
*/ |
23
|
|
|
private $rules; |
24
|
|
|
|
25
|
3 |
|
public function __construct() |
26
|
|
|
{ |
27
|
3 |
|
$this->validator = new SiriusValidator(); |
28
|
3 |
|
$this->rules = []; |
29
|
3 |
|
$this->datas = []; |
30
|
3 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* Add validator rules. |
34
|
|
|
* |
35
|
|
|
* @param string $name |
36
|
|
|
* @param array $detail |
37
|
|
|
* @param string $value |
38
|
|
|
*/ |
39
|
2 |
|
public function add($name, array $detail, $value = '') |
40
|
|
|
{ |
41
|
2 |
|
if (!$value && !is_numeric($value)) { |
42
|
2 |
|
$value = $detail['defaultValue']; |
43
|
2 |
|
} |
44
|
2 |
|
$this->datas[$name] = $value; |
45
|
|
|
|
46
|
2 |
|
if (isset($detail['rule']) && $detail['rule']) { |
47
|
2 |
|
$this->rules[$name] = $detail['rule']; |
48
|
2 |
|
} |
49
|
2 |
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Check is data valid. |
53
|
|
|
* |
54
|
|
|
* @return bool |
55
|
|
|
*/ |
56
|
1 |
|
public function isValid() |
57
|
|
|
{ |
58
|
1 |
|
return $this->validate($this->datas); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Check is input datas valid. |
63
|
|
|
* |
64
|
|
|
* @return bool |
65
|
|
|
*/ |
66
|
2 |
|
public function validate(array $datas) |
67
|
|
|
{ |
68
|
2 |
|
foreach ($this->rules as $field => $rule) { |
69
|
2 |
|
$this->validator->add($field, $rule); |
70
|
2 |
|
} |
71
|
|
|
|
72
|
2 |
|
$this->datas = $datas; |
73
|
|
|
|
74
|
2 |
|
return $this->validator->validate($datas); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* Get datas. |
79
|
|
|
* |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
2 |
|
public function getDatas() |
83
|
|
|
{ |
84
|
2 |
|
return $this->datas; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Get rules. |
89
|
|
|
* |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
2 |
|
public function getRules() |
93
|
|
|
{ |
94
|
2 |
|
return $this->rules; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Get all error messages. |
99
|
|
|
* |
100
|
|
|
* @return array |
101
|
|
|
*/ |
102
|
1 |
|
public function getMessages() |
103
|
|
|
{ |
104
|
1 |
|
return $this->validator->getMessages(); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get first error message. |
109
|
|
|
* |
110
|
|
|
* @return array |
111
|
|
|
*/ |
112
|
1 |
|
public function getFirstMessage() |
113
|
|
|
{ |
114
|
1 |
|
$messages = $this->getMessages(); |
115
|
1 |
|
reset($messages); |
116
|
1 |
|
$field = key($messages); |
117
|
|
|
|
118
|
|
|
return [ |
119
|
1 |
|
'field' => $field, |
120
|
1 |
|
'message' => (string) $messages[$field][0], |
121
|
1 |
|
]; |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Create ValidatorException. |
126
|
|
|
* |
127
|
|
|
* @return IndraGunawan\RestService\Exception\ValidatorException |
128
|
|
|
*/ |
129
|
1 |
|
public function createValidatorException() |
130
|
|
|
{ |
131
|
1 |
|
$message = $this->getFirstMessage(); |
132
|
|
|
|
133
|
1 |
|
return new ValidatorException($message['field'], $message['message']); |
134
|
|
|
} |
135
|
|
|
} |
136
|
|
|
|