Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
MeasureRatioRepository.php
1<?php
2
4
10
20{
22 protected $factory;
23
25 {
26 $this->factory = $factory;
27 }
28
29 public function getEntityById(int $id): ?BaseEntity
30 {
31 if ($id <= 0)
32 {
33 throw new \OutOfRangeException($id);
34 }
35
36 $entities = $this->getEntitiesBy([
37 'filter' => [
38 '=ID' => $id,
39 ],
40 ]);
41
42 return reset($entities) ?: null;
43 }
44
45 public function getEntitiesBy($params): array
46 {
47 $entities = [];
48
49 foreach ($this->getList((array)$params) as $item)
50 {
51 $entities[] = $this->createEntity($item);
52 }
53
54 return $entities;
55 }
56
57 public function getProductId(BaseEntity $entity): ?int
58 {
59 $id = null;
60
61 $parent = $entity->getParent();
62
63 if ($parent && !$parent->isNew())
64 {
65 $id = $parent->getId();
66 }
67
68 return $id;
69 }
70
71 public function save(BaseEntity ...$entities): Result
72 {
73 $result = new Result();
74
76 foreach ($entities as $entity)
77 {
78 if (!$entity->getProductId())
79 {
80 $productId = $this->getProductId($entity);
81
82 if ($productId)
83 {
84 $entity->setProductId($productId);
85 }
86 else
87 {
88 $result->addError(new Error('Wrong product id'));
89 continue;
90 }
91 }
92
93 if ($entityId = $entity->getId())
94 {
95 $res = $this->updateInternal($entityId, $entity->getChangedFields());
96
97 if (!$res->isSuccess())
98 {
99 $result->addErrors($res->getErrors());
100 }
101 }
102 else
103 {
104 $res = $this->addInternal($entity->getFields());
105
106 if ($res->isSuccess())
107 {
108 $entity->setId($res->getData()['ID']);
109 }
110 else
111 {
112 $result->addErrors($res->getErrors());
113 }
114 }
115 }
116
117 return $result;
118 }
119
120 public function delete(BaseEntity ...$entities): Result
121 {
122 $result = new Result();
123
125 foreach ($entities as $entity)
126 {
127 if ($entityId = $entity->getId())
128 {
129 $res = $this->deleteInternal($entityId);
130
131 if (!$res->isSuccess())
132 {
133 $result->addErrors($res->getErrors());
134 }
135 }
136 }
137
138 return $result;
139 }
140
142 {
143 if ($sku->isNew())
144 {
145 return $this->createCollection();
146 }
147
148 $result = $this->getByProductId($sku->getId());
149
150 return $this->createCollection($result);
151 }
152
153 protected function getByProductId(int $skuId): array
154 {
155 return $this->getList([
156 'filter' => [
157 '=PRODUCT_ID' => $skuId,
158 ],
159 ]);
160 }
161
162 protected function getList(array $params): array
163 {
164 return MeasureRatioTable::getList($params)
165 ->fetchAll()
166 ;
167 }
168
169 protected function createEntity(array $fields = []): BaseMeasureRatio
170 {
171 $entity = $this->factory->createEntity();
172
173 $entity->initFields($fields);
174
175 return $entity;
176 }
177
178 protected function createCollection(array $entityFields = []): MeasureRatioCollection
179 {
180 $collection = $this->factory->createCollection();
181
182 foreach ($entityFields as $fields)
183 {
184 $measureRatio = $this->createEntity($fields);
185 $collection->add($measureRatio);
186 }
187
188 return $collection;
189 }
190
191 protected function addInternal(array $fields): Result
192 {
193 $result = new Result();
194
195 $res = MeasureRatioTable::add($fields);
196
197 if ($res->isSuccess())
198 {
199 $result->setData(['ID' => $res->getId()]);
200 }
201 else
202 {
203 $result->addErrors($res->getErrors());
204 }
205
206 return $result;
207 }
208
209 protected function updateInternal(int $id, array $fields): Result
210 {
211 $result = new Result();
212
213 $res = MeasureRatioTable::update($id, $fields);
214
215 if (!$res->isSuccess())
216 {
217 $result->addErrors($res->getErrors());
218 }
219
220 return $result;
221 }
222
223 protected function deleteInternal(int $id): Result
224 {
225 $result = new Result();
226
227 $res = MeasureRatioTable::delete($id);
228
229 if (!$res->isSuccess())
230 {
231 $result->addErrors($res->getErrors());
232 }
233
234 return $result;
235 }
236}
save(BaseEntity ... $entities)