Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
PropertyValueCollection.php
1<?php
2
4
7
17{
20
22 {
23 $this->propertyValueFactory = $propertyValueFactory;
24 }
25
30 public function setValues($values): self
31 {
32 $values = $this->prepareValues($values);
33 $values = $this->prepareValuesIds($values);
34
35 if ($this->isPropertyMultiple())
36 {
37 $this->removeUnchangedItems($values);
38 }
39
40 foreach ($values as $fields)
41 {
42 $this->setValue($fields);
43 }
44
45 return $this;
46 }
47
48 private function prepareValuesIds($values): array
49 {
50 if (empty($values))
51 {
52 return $values;
53 }
54
55 foreach ($values as $key => $value)
56 {
57 $values[$key]['ID'] = 0;
58 }
59
60 $valueIndex = 0;
61 foreach ($this->getIterator() as $entity)
62 {
63 $values[$valueIndex]['ID'] = $entity->getId();
64 $valueIndex++;
65 if ($valueIndex > count($values) - 1)
66 {
67 break;
68 }
69 }
70
71 return $values;
72 }
73
80 public function initValues($values): self
81 {
82 $entities = [];
83
84 foreach ($this->prepareValues($values) as $index => $fields)
85 {
86 $entity = $this->propertyValueFactory->createEntity();
87
88 $fieldsToInitialize = [
89 'VALUE' => $fields['VALUE'] ?? null,
90 'DESCRIPTION' => $fields['DESCRIPTION'] ?? null,
91 ];
92
93 $id = (int)($fields['ID'] ?? 0);
94 if ($id > 0)
95 {
96 $fieldsToInitialize['ID'] = $id;
97 }
98
99 $entity->initFields($fieldsToInitialize);
100 $entities[] = $entity;
101 }
102
103 $this->items = [];
104 $this->add(...$entities);
105
106 return $this;
107 }
108
109 public function getValues()
110 {
111 $values = [];
112
114 foreach ($this->getIterator() as $item)
115 {
116 $values[] = $item->getValue();
117 }
118
119 if (!$this->isPropertyMultiple())
120 {
121 $values = !empty($values) ? reset($values) : null;
122 }
123
124 return $values;
125 }
126
127 public function findByValue($value): ?BaseEntity
128 {
130 foreach ($this->getIterator() as $item)
131 {
132 if ($item->getValue() === $value)
133 {
134 return $item;
135 }
136 }
137
138 return null;
139 }
140
141 private function isPropertyMultiple(): bool
142 {
144 $property = $this->getParent();
145
146 return $property && $property->isMultiple();
147 }
148
149 private function prepareValues($values): array
150 {
151 if (!is_array($values))
152 {
153 $values = [
154 ['VALUE' => $values],
155 ];
156 }
157 elseif (isset($values['VALUE']) || isset($values['DESCRIPTION']) || isset($values['CUR_PATH']))
158 {
159 $values = [$values];
160 }
161
162 foreach ($values as &$value)
163 {
164 if (!isset($value['VALUE']) && !isset($value['DESCRIPTION']))
165 {
166 $value = ['VALUE' => $value];
167 }
168 }
169
170 return $values;
171 }
172
173 private function removeUnchangedItems($values): void
174 {
175 $idsToSave = [];
176
177 foreach ($values as $value)
178 {
179 if ($value['ID'] > 0)
180 {
181 $idsToSave[] = $value['ID'];
182 }
183 }
184
185 foreach ($this->getIterator() as $entity)
186 {
187 if ($entity->isNew() || !in_array($entity->getId(), $idsToSave, true))
188 {
189 $entity->remove();
190 }
191 }
192 }
193
194 private function setValue($fields): void
195 {
196 $entity = null;
197
198 if ($this->isPropertyMultiple())
199 {
200 if (isset($fields['ID']) && $fields['ID'] > 0)
201 {
202 $entity = $this->findById($fields['ID']);
203 }
204 }
205 else
206 {
207 $entity = !empty($this->items) ? reset($this->items) : null;
208 }
209
210 if ($entity === null)
211 {
212 $entity = $this->propertyValueFactory->createEntity();
213 $this->add($entity);
214 }
215
216 $entity->setValue($fields['VALUE'] ?? null);
217
218 if (isset($fields['DESCRIPTION']))
219 {
220 $entity->setDescription($fields['DESCRIPTION']);
221 }
222 }
223}
__construct(PropertyValueFactory $propertyValueFactory)