1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
AtLeastOnePropertyNotEmpty.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Main\Validation\Rule;
6
7use Attribute;
8use Bitrix\Main\Localization\LocalizableMessage;
9use Bitrix\Main\Localization\LocalizableMessageInterface;
10use Bitrix\Main\Validation\ValidationError;
11use Bitrix\Main\Validation\ValidationResult;
12use Bitrix\Main\Validation\Validator\AtLeastOneNotEmptyValidator;
13use ReflectionClass;
14use ReflectionProperty;
15
16#[Attribute(Attribute::TARGET_CLASS)]
18{
19 public function __construct(
20 private readonly array $fields,
21 private readonly bool $allowZero = false,
22 private readonly bool $allowEmptyString = false,
23 protected string|LocalizableMessageInterface|null $errorMessage = null,
24 )
25 {
26 }
27
28 public function validateObject(object $object): ValidationResult
29 {
31
32 $properties = $this->getProperties($object);
33
34 if (empty($properties))
35 {
36 $result->addError(new ValidationError(
37 new LocalizableMessage('MAIN_VALIDATION_AT_LEAST_ONE_PROPERTY_NOT_EMPTY_EMPTY')
38 ));
39
40 return $this->replaceWithCustomError($result);
41 }
42
43 $values = $this->getValues($object, ...$properties);
44
45 $result = (new AtLeastOneNotEmptyValidator($this->allowZero, $this->allowEmptyString))
46 ->validate($values);
47
48 return $this->replaceWithCustomError($result);
49 }
50
51 private function getProperties(object $object): array
52 {
53 $reflection = new ReflectionClass($object);
54
55 return array_filter(
56 $reflection->getProperties(),
57 fn (ReflectionProperty $property): bool =>
58 in_array($property->getName(), $this->fields, true)
59 );
60 }
61
62 private function getValues(object $object, ReflectionProperty ...$properties): array
63 {
64 $values = [];
65 foreach ($properties as $property)
66 {
67 if ($property->isInitialized($object))
68 {
69 $values[] = $property->getValue($object);
70 }
71 }
72
73 return $values;
74 }
75}
__construct(private readonly array $fields, private readonly bool $allowZero=false, private readonly bool $allowEmptyString=false, protected string|LocalizableMessageInterface|null $errorMessage=null,)
Определения AtLeastOnePropertyNotEmpty.php:19
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$result
Определения get_property_values.php:14
while($arParentIBlockProperty=$dbParentIBlockProperty->Fetch()) $errorMessage
replaceWithCustomError(ValidationResult $result, ?ValidatorInterface $validator=null)
Определения ValidationErrorTrait.php:21
$fields
Определения yandex_run.php:501