Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
validator.php
1<?php
2
3namespace Bitrix\Bizproc;
4
7
9{
10 const TYPE_ARRAY = 'array';
11 const TYPE_NUMERIC = 'numeric';
12 const TYPE_STRING = 'string';
13
14 protected array $dirtyValues;
15 protected array $pureValues = [];
16
17 public function __construct(array $values)
18 {
19 Loc::loadMessages(__FILE__);
20 $this->dirtyValues = $values;
21 }
22
23 public function getPureValues(): array
24 {
25 return $this->pureValues;
26 }
27
28 protected function validate(string $type, $value): bool
29 {
30 if ($type === self::TYPE_ARRAY)
31 {
32 return is_array($value);
33 }
34 elseif ($type === self::TYPE_NUMERIC)
35 {
36 return is_numeric($value);
37 }
38 else
39 {
40 return is_string($value);
41 }
42 }
43
44 public function validateArray(string $name, string $keyType = self::TYPE_NUMERIC,
45 string $valueType = self::TYPE_STRING): self
46 {
47 if (!isset($this->dirtyValues[$name]))
48 {
49 return $this;
50 }
51
52 foreach ($this->dirtyValues[$name] as $key => $value)
53 {
54 if (!$this->validate($keyType, $key))
55 {
56 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_ARRAY_KEY",
57 ["#name#" => $name, '#val#' => $key]));
58 }
59 if (!$this->validate($valueType, $value))
60 {
61 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_ARRAY_VAL",
62 ["#name#" => $name, '#val#' => $key]));
63 }
64 }
65
66 if (!is_array($this->dirtyValues[$name]))
67 {
68 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_IS_ARRAY", ["#name#" => $name]));
69 }
70
71 $this->setPureValue($name);
72 return $this;
73 }
74
75 public function validateNumeric(string $name, int $min = 0, int $max = 0): self
76 {
77 if (!isset($this->dirtyValues[$name]))
78 {
79 return $this;
80 }
81
82 if (!is_numeric($this->dirtyValues[$name]))
83 {
84 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_IS_NUM", ["#name#" => $name]));
85 }
86
87 $val = (int)$this->dirtyValues[$name];
88
89 if ($min && $val < $min)
90 {
91 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_NUM_MIN",
92 ["#name#" => $name, '#min#' => $min]));
93 }
94
95 if ($max && $val > $max)
96 {
97 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_NUM_MAX",
98 ["#name#" => $name, '#max#' => $max]));
99 }
100
101 $this->setPureValue($name);
102 return $this;
103 }
104
105 public function validateString(string $name, int $minLen = 0, int $maxLen = 0): self
106 {
107 if (!isset($this->dirtyValues[$name]))
108 {
109 return $this;
110 }
111
112 $val = $this->dirtyValues[$name];
113
114 if (!is_string($val))
115 {
116 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_IS_STRING", ["#name#" => $name]));
117 }
118
119 if ($minLen && mb_strlen($val) < $minLen)
120 {
121 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_STRING_MIN",
122 ["#name#" => $name, '#min#' => $minLen]));
123 }
124
125 if ($maxLen && mb_strlen($val) > $maxLen)
126 {
127 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_STRING_MAX",
128 ["#name#" => $name, '#max#' => $maxLen]));
129 }
130
131 $this->setPureValue($name);
132 return $this;
133 }
134
135 public function validateRegExp(string $name, string $pattern): self
136 {
137 if (!isset($this->dirtyValues[$name]))
138 {
139 return $this;
140 }
141
142 if (!preg_match($pattern, $this->dirtyValues[$name]))
143 {
144 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_REGEXP",
145 ["#name#" => $name, "#pattern#" => $pattern]));
146 }
147
148 $this->setPureValue($name);
149 return $this;
150 }
151
152 public function validateEnum(string $name, array $enum): self
153 {
154 if (!isset($this->dirtyValues[$name]))
155 {
156 return $this;
157 }
158
159 if (!in_array($this->dirtyValues[$name], $enum))
160 {
161 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_ENUM",
162 ["#name#" => $name, "#enum#" => implode('", "', $enum)]));
163 }
164
165 $this->setPureValue($name);
166 return $this;
167 }
168
169 public function validateRequire(string $name): self
170 {
171 if (!isset($this->dirtyValues[$name]))
172 {
173 throw new ValidationException(Loc::getMessage("BIZPROC_VALIDATOR_REQUIRE", ["#name#" => $name]));
174 }
175
176 $this->setPureValue($name);
177 return $this;
178 }
179
180 public function setDefault(string $name, $value): self
181 {
182 if (!isset($this->dirtyValues[$name]) && !isset($this->pureValues[$name]))
183 {
184 $this->pureValues[$name] = $value;
185 }
186
187 return $this;
188 }
189
190 public function setPureValue($name): self
191 {
192 if (isset($this->dirtyValues[$name]) && !isset($this->pureValues[$name]))
193 {
194 $this->pureValues[$name] = $this->dirtyValues[$name];
195 }
196
197 return $this;
198 }
199
200 public function convertToInt(string $name): self
201 {
202 if (isset($this->dirtyValues[$name]))
203 {
204 $this->dirtyValues[$name] = (int)$this->dirtyValues[$name];
205 }
206
207 if (isset($this->pureValues[$name]))
208 {
209 $this->pureValues[$name] = (int)$this->pureValues[$name];
210 }
211
212 return $this;
213 }
214}
validateArray(string $name, string $keyType=self::TYPE_NUMERIC, string $valueType=self::TYPE_STRING)
Definition validator.php:44
setDefault(string $name, $value)
validateString(string $name, int $minLen=0, int $maxLen=0)
validateNumeric(string $name, int $min=0, int $max=0)
Definition validator.php:75
validate(string $type, $value)
Definition validator.php:28
__construct(array $values)
Definition validator.php:17
validateRequire(string $name)
validateEnum(string $name, array $enum)
convertToInt(string $name)
validateRegExp(string $name, string $pattern)
static loadMessages($file)
Definition loc.php:64
static getMessage($code, $replace=null, $language=null)
Definition loc.php:29