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