Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
SectionCollection.php
1<?php
2
4
7
17{
19 protected $factory;
21 protected $repository;
22
24 {
25 $this->factory = $factory;
26 $this->repository = $repository;
27 }
28
33 public function setValues(array $values): self
34 {
35 // ToDo recalculate already loaded properties on section modifications?
36 $currentValues = $this->getValues();
37 $filteredValues = $this->filterValues($values);
38
39 $oldValuesToRemove = array_diff($currentValues, $filteredValues);
40
41 if (!empty($oldValuesToRemove))
42 {
43 $oldSections = [];
44
46 foreach ($this->getIterator() as $item)
47 {
48 if (in_array($item->getValue(), $oldValuesToRemove, true))
49 {
50 $oldSections[] = $item;
51 }
52 }
53
54 $this->remove(...$oldSections);
55 }
56
57 $newValuesToAdd = array_diff($filteredValues, $currentValues);
58
59 if (!empty($newValuesToAdd))
60 {
61 $newSections = [];
62
63 foreach ($newValuesToAdd as $value)
64 {
65 $newSections[] = $this
66 ->factory
67 ->createEntity()
68 ->setValue($value)
69 ;
70 }
71
72 $this->add(...$newSections);
73 }
74
75 return $this;
76 }
77
78 public function getValues(): array
79 {
80 $values = [];
81
83 foreach ($this->getIterator() as $item)
84 {
85 $values[] = $item->getValue();
86 }
87
88 return $values;
89 }
90
91 private function filterValues(array $values): array
92 {
93 $filteredValues = [];
94
95 foreach ($values as $value)
96 {
97 if (is_numeric($value))
98 {
99 $filteredValues[] = (int)$value;
100 }
101 }
102
103 return array_unique($filteredValues);
104 }
105
106 public function saveInternal(): Result
107 {
108 $result = new Result();
109
110 if ($this->isChanged())
111 {
112 $res = $this->repository->save(...$this->getIterator());
113
114 if ($res->isSuccess())
115 {
116 $this->clearChanged();
117 }
118 else
119 {
120 $result->addErrors($res->getErrors());
121 }
122 }
123
124 return $result;
125 }
126}
__construct(SectionFactory $factory, SectionRepositoryContract $repository)