1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace ddlzz\AmoAPI\Validator; |
4
|
|
|
|
5
|
|
|
use ddlzz\AmoAPI\Exception\EntityFieldsException; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class FieldsValidator. |
9
|
|
|
* |
10
|
|
|
* @author ddlzz |
11
|
|
|
*/ |
12
|
|
|
class FieldsValidator |
13
|
|
|
{ |
14
|
|
|
/** @var array */ |
15
|
|
|
private $fieldsParams; |
16
|
|
|
|
17
|
|
|
/** @var string */ |
18
|
|
|
private $action = ''; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* FieldsValidator constructor. |
22
|
|
|
* |
23
|
|
|
* @param array $fieldsParams |
24
|
|
|
*/ |
25
|
33 |
|
public function __construct(array $fieldsParams) |
26
|
|
|
{ |
27
|
33 |
|
$this->fieldsParams = $fieldsParams; |
28
|
33 |
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @param string $action |
32
|
|
|
*/ |
33
|
5 |
|
public function setAction($action) |
34
|
|
|
{ |
35
|
5 |
|
$this->action = $action; |
36
|
5 |
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @param string $key |
40
|
|
|
* @param mixed $value |
41
|
|
|
* |
42
|
|
|
* @return bool |
43
|
|
|
*/ |
44
|
33 |
|
public function isValid($key, $value) |
45
|
|
|
{ |
46
|
33 |
|
$this->validateRequired($key, $value); |
47
|
|
|
|
48
|
32 |
|
if (null !== $value) { |
49
|
31 |
|
$validate = $this->prepareValidateType($this->fieldsParams[$key]['type']); |
50
|
30 |
|
self::$validate($key, $value); |
51
|
|
|
} |
52
|
|
|
|
53
|
13 |
|
return true; |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
/** |
57
|
|
|
* @param string $key |
58
|
|
|
* |
59
|
|
|
* @throws EntityFieldsException |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
31 |
|
private function prepareValidateType($key) |
64
|
|
|
{ |
65
|
31 |
|
$key = str_replace('|', '', $key); |
66
|
31 |
|
$method = 'validate'.ucfirst($key); |
67
|
31 |
|
if (!method_exists(self::class, $method)) { |
68
|
1 |
|
throw new EntityFieldsException( |
69
|
1 |
|
sprintf('Internal error: the field "%s" doesn\'t match any of the entity predefined fields', $key) |
70
|
|
|
); |
71
|
|
|
} |
72
|
|
|
|
73
|
30 |
|
return $method; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $key |
78
|
|
|
* @param $value |
79
|
|
|
* |
80
|
|
|
* @throws EntityFieldsException |
81
|
|
|
* |
82
|
|
|
* @return bool |
83
|
|
|
*/ |
84
|
33 |
|
private function validateRequired($key, $value) |
85
|
|
|
{ |
86
|
33 |
|
if (('add' === $this->action) || ('update' === $this->action)) { |
87
|
5 |
|
if (null === $value && (true === $this->fieldsParams[$key]['required_'.$this->action])) { |
88
|
2 |
|
throw new EntityFieldsException(sprintf('%s error: the required field "%s" is missing or empty', ucfirst($this->action), $key)); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
92
|
32 |
|
return true; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** @noinspection PhpUnusedPrivateMethodInspection */ |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* @param string $key |
99
|
|
|
* @param int $value |
100
|
|
|
* |
101
|
|
|
* @throws EntityFieldsException |
102
|
|
|
* |
103
|
|
|
* @return bool |
104
|
|
|
*/ |
105
|
9 |
|
private static function validateInt($key, $value) |
106
|
|
|
{ |
107
|
9 |
|
if (!is_int($value) || !preg_match('/^\d+$/', (string) $value)) { |
|
|
|
|
108
|
6 |
|
throw new EntityFieldsException(sprintf('The field "%s" must contain digits only', $key)); |
109
|
|
|
} |
110
|
|
|
|
111
|
3 |
|
return true; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** @noinspection PhpUnusedPrivateMethodInspection */ |
115
|
|
|
|
116
|
|
|
/** |
117
|
|
|
* @param string $key |
118
|
|
|
* @param string $value |
119
|
|
|
* |
120
|
|
|
* @throws EntityFieldsException |
121
|
|
|
* |
122
|
|
|
* @return bool |
123
|
|
|
*/ |
124
|
5 |
|
private static function validateString($key, $value) |
125
|
|
|
{ |
126
|
5 |
|
if (!is_string($value) && !is_numeric($value)) { |
|
|
|
|
127
|
3 |
|
throw new EntityFieldsException(sprintf('The field "%s" must be string', $key)); |
128
|
|
|
} |
129
|
|
|
|
130
|
2 |
|
return true; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** @noinspection PhpUnusedPrivateMethodInspection */ |
134
|
|
|
|
135
|
|
|
/** |
136
|
|
|
* @param string $key |
137
|
|
|
* @param bool $value |
138
|
|
|
* |
139
|
|
|
* @throws EntityFieldsException |
140
|
|
|
* |
141
|
|
|
* @return bool |
142
|
|
|
*/ |
143
|
6 |
|
private static function validateBool($key, $value) |
144
|
|
|
{ |
145
|
6 |
|
if (null === filter_var($value, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE)) { |
146
|
4 |
|
throw new EntityFieldsException(sprintf('The field "%s" must contain boolean values only', $key)); |
147
|
|
|
} |
148
|
|
|
|
149
|
2 |
|
return true; |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** @noinspection PhpUnusedPrivateMethodInspection */ |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* @param string $key |
156
|
|
|
* @param array $value |
157
|
|
|
* |
158
|
|
|
* @throws EntityFieldsException |
159
|
|
|
* |
160
|
|
|
* @return bool |
161
|
|
|
*/ |
162
|
5 |
|
private static function validateArray($key, $value) |
163
|
|
|
{ |
164
|
5 |
|
if (!is_array($value)) { |
|
|
|
|
165
|
3 |
|
throw new EntityFieldsException("The field \"$key\" must be an array"); |
166
|
|
|
} |
167
|
|
|
|
168
|
2 |
|
return true; |
169
|
|
|
} |
170
|
|
|
|
171
|
|
|
/** @noinspection PhpUnusedPrivateMethodInspection */ |
172
|
|
|
|
173
|
|
|
/** |
174
|
|
|
* Because some fields must be either strings during entity creation or arrays during it's obtaining from server, |
175
|
|
|
* we create this check. |
176
|
|
|
* |
177
|
|
|
* @param string $key |
178
|
|
|
* @param mixed $value |
179
|
|
|
* |
180
|
|
|
* @throws EntityFieldsException |
181
|
|
|
* |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
5 |
|
private static function validateArraystring($key, $value) |
185
|
|
|
{ |
186
|
5 |
|
if ((!is_array($value)) && (!is_string($value) && !is_numeric($value))) { |
187
|
2 |
|
throw new EntityFieldsException(sprintf('The field "%s" must be an array or string', $key)); |
188
|
|
|
} |
189
|
|
|
|
190
|
3 |
|
return true; |
191
|
|
|
} |
192
|
|
|
} |
193
|
|
|
|