1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
LengthValidator.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Main\Validation\Validator;
6
7use Bitrix\Main\ArgumentException;
8use Bitrix\Main\Localization\Loc;
9use Bitrix\Main\Localization\LocalizableMessage;
10use Bitrix\Main\Localization\LocalizableMessagePlural;
11use Bitrix\Main\Validation\ValidationError;
12use Bitrix\Main\Validation\ValidationResult;
13
15{
19 public function __construct(
20 private readonly ?int $min = null,
21 private readonly ?int $max = null
22 )
23 {
24 if (null === $this->min && null === $this->max)
25 {
26 throw new ArgumentException(Loc::getMessage('MAIN_VALIDATION_LENGTH_INVALID_ARGUMENT'));
27 }
28 }
29
30 public function validate(mixed $value): ValidationResult
31 {
33
34 if (!is_string($value))
35 {
36 $result->addError(new ValidationError(
37 new LocalizableMessage('MAIN_VALIDATION_LENGTH_INVALID_ARGUMENT'),
38 failedValidator: $this
39 ));
40
41 return $result;
42 }
43
44 $length = mb_strlen($value);
45
46 if (null !== $this->min)
47 {
48 $result = $this->checkMin($length);
49 }
50
51 if (null !== $this->max && $result->isSuccess())
52 {
53 $result = $this->checkMax($length);
54 }
55
56 return $result;
57 }
58
59 private function checkMin(int $length): ValidationResult
60 {
62
63 if ($length < $this->min)
64 {
65 $result->addError(new ValidationError(
66 new LocalizableMessagePlural('MAIN_VALIDATION_LENGTH_INVALID_MIN', $this->min, ['#MIN#' => $this->min]),
67 failedValidator: $this
68 ));
69 }
70
71 return $result;
72 }
73
74 private function checkMax(int $length): ValidationResult
75 {
76 $result = new ValidationResult();
77
78 if ($length > $this->max)
79 {
80 $result->addError(new ValidationError(
81 new LocalizableMessagePlural('MAIN_VALIDATION_LENGTH_INVALID_MAX', $this->max, ['#MAX#' => $this->max])
82 , failedValidator: $this
83 ));
84 }
85
86 return $result;
87 }
88}
__construct(private readonly ?int $min=null, private readonly ?int $max=null)
Определения LengthValidator.php:19
$result
Определения get_property_values.php:14
$max
Определения template_copy.php:262