Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
baseoperator.php
1<?php
2
4
6
8{
9 protected $toCheck;
10 protected $value;
12
13 public static function getCode(): string
14 {
15 return 'BASE';
16 }
17
18 public static function getTitle(): string
19 {
20 return '';
21 }
22
24 {
25 $this->fieldType = $fieldType;
26 $this->toCheck = $toCheck;
27 $this->value = $value;
28
29 $this->toBaseType();
30 }
31
32 protected function toBaseType(): void
33 {
34 $baseType = $this->fieldType->getBaseType();
35 $documentId = $this->fieldType->getDocumentId();
36 if ($baseType === 'user')
37 {
38 $this->toCheck = \CBPHelper::extractUsers($this->toCheck, $documentId);
39 $this->value = \CBPHelper::extractUsers($this->value, $documentId);
40 }
41 elseif ($baseType === 'select')
42 {
43 if (is_array($this->toCheck) && \CBPHelper::isAssociativeArray($this->toCheck))
44 {
45 $this->toCheck = array_keys($this->toCheck);
46 }
47 }
48 }
49
50 public function check(): bool
51 {
52 $toCheck = $this->valueToArray($this->toCheck);
53 $value = $this->valueToArray($this->value);
54
55 $result = false;
56
57 $fieldCount = count($toCheck);
58 $valueCount = count($value);
59 for ($i = 0; $i < max($fieldCount, $valueCount); $i++)
60 {
61 $fieldI = ($fieldCount > $i) ? $toCheck[$i] : $toCheck[$fieldCount -1];
62 $valueI = ($valueCount > $i) ? $value[$i] : $value[$valueCount - 1];
63
64 [$valueI, $fieldI] = $this->normalizeZeroComparing($valueI, $fieldI);
65
66 $result = $this->compare($fieldI, $valueI);
67
68 if (!$result)
69 {
70 break;
71 }
72 }
73
74 return $result;
75 }
76
77 protected function compare($toCheck, $value): bool
78 {
79 return $toCheck === $value;
80 }
81
82 protected function valueToArray($value): array
83 {
84 $value = is_array($value) ? $value : [$value];
85 $value = \CBPHelper::isAssociativeArray($value) ? array_keys($value) : $value;
86 if (empty($value))
87 {
88 $value = [null];
89 }
90
91 return $value;
92 }
93
94 protected function normalizeZeroComparing($value, $field): array
95 {
96 if ($value === '' && ($field === '0' || $field === 0.0))
97 {
98 return [null, null];
99 }
100
101 if (($value === '0' || $value === 0.0) && $field === '')
102 {
103 return [null, null];
104 }
105
106 return [$value, $field];
107 }
108}
__construct($toCheck, $value, FieldType $fieldType)