32 $values = $this->prepareValues($values);
33 $values = $this->prepareValuesIds($values);
35 if ($this->isPropertyMultiple())
37 $this->removeUnchangedItems($values);
40 foreach ($values as $fields)
42 $this->setValue($fields);
48 private function prepareValuesIds($values): array
55 foreach ($values as $key => $value)
57 $values[$key][
'ID'] = 0;
63 $values[$valueIndex][
'ID'] = $entity->getId();
65 if ($valueIndex >
count($values) - 1)
84 foreach ($this->prepareValues($values) as $index => $fields)
86 $entity = $this->propertyValueFactory->createEntity();
88 $fieldsToInitialize = [
89 'VALUE' => $fields[
'VALUE'] ??
null,
90 'DESCRIPTION' => $fields[
'DESCRIPTION'] ??
null,
93 $id = (int)($fields[
'ID'] ?? 0);
96 $fieldsToInitialize[
'ID'] = $id;
99 $entity->initFields($fieldsToInitialize);
100 $entities[] = $entity;
104 $this->
add(...$entities);
109 public function getValues()
116 $values[] = $item->getValue();
119 if (!$this->isPropertyMultiple())
121 $values = !empty($values) ? reset($values) : null;
127 public function findByValue($value): ?BaseEntity
132 if ($item->getValue() === $value)
141 private function isPropertyMultiple(): bool
146 return $property && $property->isMultiple();
149 private function prepareValues($values): array
151 if (!is_array($values))
154 [
'VALUE' => $values],
157 elseif (isset($values[
'VALUE']) || isset($values[
'DESCRIPTION']) || isset($values[
'CUR_PATH']))
162 foreach ($values as &$value)
164 if (!isset($value[
'VALUE']) && !isset($value[
'DESCRIPTION']))
166 $value = [
'VALUE' => $value];
173 private function removeUnchangedItems($values): void
177 foreach ($values as $value)
179 if ($value[
'ID'] > 0)
181 $idsToSave[] = $value[
'ID'];
187 if ($entity->isNew() || !in_array($entity->getId(), $idsToSave,
true))
194 private function setValue($fields): void
198 if ($this->isPropertyMultiple())
200 if (isset($fields[
'ID']) && $fields[
'ID'] > 0)
202 $entity = $this->
findById($fields[
'ID']);
207 $entity = !empty($this->items) ? reset($this->items) : null;
210 if ($entity ===
null)
212 $entity = $this->propertyValueFactory->createEntity();
216 $entity->setValue($fields[
'VALUE'] ??
null);
218 if (isset($fields[
'DESCRIPTION']))
220 $entity->setDescription($fields[
'DESCRIPTION']);