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