1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
ElementsType.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Main\Validation\Rule;
6
7use ArrayAccess;
8use Attribute;
9use Bitrix\Main\ArgumentException;
10use Bitrix\Main\Localization\Loc;
11use Bitrix\Main\Localization\LocalizableMessage;
12use Bitrix\Main\Localization\LocalizableMessageInterface;
13use Bitrix\Main\Validation\Rule\Enum\Type;
14use Bitrix\Main\Validation\ValidationError;
15use Bitrix\Main\Validation\ValidationResult;
16
17#[Attribute(Attribute::TARGET_PROPERTY | Attribute::TARGET_PARAMETER)]
19{
21
25 public function __construct(
26 private readonly ?Type $typeEnum = null,
27 private readonly ?string $className = null,
29 )
30 {
31 if (null === $this->typeEnum && null === $this->className)
32 {
33 throw new ArgumentException(Loc::getMessage('MAIN_VALIDATION_ELEMENTS_TYPE_UNSUPPORTED_TYPE'));
34 }
35
36 $this->errorMessage = $errorMessage;
37 }
38
40 {
42
43 if (!is_array($propertyValue) && !is_iterable($propertyValue) && !($propertyValue instanceof ArrayAccess))
44 {
45 $result->addError(new ValidationError(
46 new LocalizableMessage('MAIN_VALIDATION_ELEMENTS_TYPE_UNSUPPORTED_TYPE')
47 ));
48
49 return $this->replaceWithCustomError($result);
50 }
51
52 if (null === $this->typeEnum)
53 {
54 $result = $this->checkClassType($propertyValue);
55 }
56 else
57 {
58 $result = $this->checkScalar($propertyValue);
59 }
60
61 return $this->replaceWithCustomError($result);
62 }
63
64 private function checkScalar(mixed $propertyValue): ValidationResult
65 {
67
68 $checkType = $this->typeEnum->value;
69
70 foreach ($propertyValue as $item)
71 {
72 if (!$checkType($item))
73 {
74 $result->addError(new ValidationError(
75 new LocalizableMessage('MAIN_VALIDATION_ELEMENTS_TYPE_UNSUPPORTED_TYPE')
76 ));
77
78 return $result;
79 }
80 }
81
82 return $result;
83 }
84
85 private function checkClassType(mixed $propertyValue): ValidationResult
86 {
87 $result = new ValidationResult();
88
89 if (!class_exists($this->className))
90 {
91 $result->addError(new ValidationError(
92 new LocalizableMessage('MAIN_VALIDATION_ELEMENTS_TYPE_UNSUPPORTED_TYPE')
93 ));
94
95 return $result;
96 }
97
98 foreach ($propertyValue as $item)
99 {
100 if (!$item instanceof $this->className)
101 {
102 $result->addError(new ValidationError(
103 new LocalizableMessage('MAIN_VALIDATION_ELEMENTS_TYPE_UNSUPPORTED_TYPE')
104 ));
105
106 return $result;
107 }
108 }
109
110 return $result;
111 }
112}
__construct(private readonly ?Type $typeEnum=null, private readonly ?string $className=null, string|LocalizableMessageInterface|null $errorMessage=null)
Определения ElementsType.php:25
validateProperty(mixed $propertyValue)
Определения ElementsType.php:39
</td ></tr ></table ></td ></tr ><?endif?><? $propertyIndex=0;foreach( $arGlobalProperties as $propertyCode=> $propertyValue
Определения file_new.php:729
$result
Определения get_property_values.php:14
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
replaceWithCustomError(ValidationResult $result, ?ValidatorInterface $validator=null)
Определения ValidationErrorTrait.php:21