Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
Collection.php
1<?php
2
3namespace Bitrix\Im\V2;
4
9
13abstract class Collection extends Registry implements ActiveRecordCollection
14{
16
17 // temporary entity id in registry
18 protected int $newEntityTmpId = 0;
19
20 public function __construct($source = null)
21 {
22 parent::__construct();
23
24 if (!empty($source))
25 {
26 $this->load($source);
27 }
28 }
29
34 final public static function getDataClass(): string
35 {
36 return static::getCollectionElementClass()::getDataClass();
37 }
38
43 abstract public static function getCollectionElementClass(): string;
44
52 abstract public static function find(array $filter, array $order, ?int $limit = null, ?Context $context = null): self;
53
60 public function add(ActiveRecord $entry): self
61 {
62 $collectionElementClass = static::getCollectionElementClass();
63
64 if (!($entry instanceof $collectionElementClass))
65 {
66 $entryClass = \get_class($entry);
67 throw new ArgumentTypeException("Entry is instance of {$entryClass}, but collection support {$collectionElementClass}");
68 }
69
70 if ($entry instanceof RegistryEntry)
71 {
72 $entry->setRegistry($this);
73 }
74
75 if ($entry->getPrimaryId())
76 {
77 parent::offsetSet($entry->getPrimaryId(), $entry);
78 }
79 else
80 {
81 $this->newEntityTmpId --;
82 parent::offsetSet($this->newEntityTmpId, $entry);
83 }
84
85 return $this;
86 }
87
94 public function offsetSet($offset, $entry): void
95 {
96 $collectionElementClass = static::getCollectionElementClass();
97
98 if (!($entry instanceof $collectionElementClass))
99 {
100 $entryClass = \get_class($entry);
101 throw new ArgumentTypeException("Entry is instance of {$entryClass}, but collection support {$collectionElementClass}");
102 }
103
104 if ($offset === null)
105 {
106 if ($entry->getPrimaryId())
107 {
108 $offset = $entry->getPrimaryId();
109 }
110 else
111 {
112 $this->newEntityTmpId --;
113 $offset = $this->newEntityTmpId;
114 }
115 }
116
117 parent::offsetSet($offset, $entry);
118 }
119
123 protected function prepareFields(): Result
124 {
125 $result = new Result;
126
127 $this->dataEntityCollection = null; //Resetting the collection of entities before filling it
128 $dataEntity = $this->getDataEntityCollection();
129
131 foreach ($this as $entity)
132 {
133 if ($entity->isDeleted())
134 {
135 continue;
136 }
137
138 $resultFill = $entity->prepareFields();
139
140 if (!$resultFill->isSuccess())
141 {
142 $result->addErrors($resultFill->getErrors());
143 continue;
144 }
145
146 $dataEntity->add($entity->getDataEntity());
147 }
148
149 return $result;
150 }
151
155 public function getPrimaryIds(): array
156 {
157 $ids = [];
158
160 foreach ($this as $item)
161 {
162 if ($item->getPrimaryId())
163 {
164 $ids[] = $item->getPrimaryId();
165 }
166 }
167
168 return $ids;
169 }
170
175 public function load($source): Result
176 {
177 if (is_array($source))
178 {
179 if ($this->isArrayOfIds($source))
180 {
181 return $this->initByArrayOfPrimary($source);
182 }
183
184 return $this->initByArray($source);
185 }
186
187 if ($source instanceof ORM\Objectify\Collection)
188 {
189 return $this->initByEntitiesCollection($source);
190 }
191
192 return (new Result())->addError(new Error(Error::NOT_FOUND));
193 }
194
195
200 protected function setDataEntityCollection(ORM\Objectify\Collection $entityCollection): self
201 {
202 $this->dataEntityCollection = $entityCollection;
203 return $this;
204 }
205
210 public function getDataEntityCollection(): ORM\Objectify\Collection
211 {
212 if ($this->dataEntityCollection === null)
213 {
215 $dataClass = static::getDataClass();
217 $entityCollectionClass = $dataClass::getCollectionClass();
218 $this->dataEntityCollection = new $entityCollectionClass;
219 }
220
222 }
223
224 public function save(bool $isGroupSave = false): Result
225 {
226 $result = $this->prepareFields();
227
228 if ($result->isSuccess())
229 {
230 if ($isGroupSave)
231 {
232 $resultSave = $this->getDataEntityCollection()->save(true);
233 if (!$resultSave->isSuccess())
234 {
235 $result->addErrors($resultSave->getErrors());
236 }
237 }
238 else
239 {
240 $index = [];
241 foreach ($this as $inx => $entity)
242 {
243 $index[] = $inx;
244 }
245
247 foreach ($index as $inx)
248 {
249 $entity = $this[$inx];
250 if ($entity instanceof RegistryEntry)
251 {
252 $entity->setRegistry($this);
253 }
254 $resultSave = $entity->save();
255 if (!$resultSave->isSuccess())
256 {
257 $result->addErrors($resultSave->getErrors());
258 }
259 elseif ($inx < 0 && isset($this[$entity->getPrimaryId()]))
260 {
261 unset($this[$inx]);// remove temporary object link from registry
262 }
263 }
264 }
265 }
266
267 return $result;
268 }
269
274 public function delete(): Result
275 {
276 $result = new Result();
277
278 $idsToDelete = $this->getPrimaryIds();
279 if (method_exists(static::getDataClass(), 'deleteByFilter'))
280 {
281 if (empty($idsToDelete))
282 {
283 return $result;
284 }
285
286 $primaryField = static::getPrimaryFieldName();
287 static::getDataClass()::deleteByFilter(
288 [
289 "={$primaryField}" => $idsToDelete
290 ]
291 );
292
293 return $result;
294 }
295
296 foreach ($idsToDelete as $idToDelete)
297 {
298 $deleteResult = static::getDataClass()::delete($idToDelete);
299 if (!$deleteResult->isSuccess())
300 {
301 $result->addErrors($deleteResult->getErrors());
302 }
303 }
304
305 return $result;
306 }
307
311 public function hasUnsaved(): bool
312 {
314 foreach ($this as $entity)
315 {
316 if ($entity->getPrimaryId() === null)
317 {
318 return true;
319 }
320 if ($entity->isChanged())
321 {
322 return true;
323 }
324 if ($entity->isDeleted())
325 {
326 return true;
327 }
328 }
329
330 return false;
331 }
332
338 protected function initByEntitiesCollection(ORM\Objectify\Collection $entitiesCollection): Result
339 {
340 $collectionClass = static::getDataClass()::getCollectionClass();
341
342 if (!($entitiesCollection instanceof $collectionClass))
343 {
344 $entryClass = \get_class($entitiesCollection);
345 throw new ArgumentTypeException("Entry is instance of {$entryClass}, but collection support {$collectionClass}");
346 }
347
348 $this->setDataEntityCollection($entitiesCollection);
349
350 return $this->initForEach($entitiesCollection);
351 }
352
357 protected function initByArray(array $items): Result
358 {
359 return $this->initForEach($items);
360 }
361
366 protected function initByArrayOfPrimary(array $ids): Result
367 {
368 $primaryField = static::getPrimaryFieldName();
369
370 if (empty($ids))
371 {
372 return new Result();
373 }
374
376 $entitiesCollection = static::getDataClass()::query()
377 ->setSelect(['*'])
378 ->whereIn($primaryField, $ids)
379 ->fetchCollection()
380 ;
381
382 return $this->initByEntitiesCollection($entitiesCollection);
383 }
384
389 private function initForEach($entitiesCollection): Result
390 {
391 $result = new Result();
392 $itemClass = static::getCollectionElementClass();
393
394 foreach ($entitiesCollection as $entity)
395 {
396 $item = new $itemClass;
397 $loadResult = $item->load($entity);
398
399 if (!$loadResult->isSuccess())
400 {
401 $result->addErrors($loadResult->getErrors());
402 }
403 elseif ($item instanceof RegistryEntry)
404 {
405 $item->setRegistry($this);
406 }
407 }
408
409 return $result;
410 }
411
412 private function isArrayOfIds(array $array): bool
413 {
414 foreach ($array as $key => $value)
415 {
416 if (!is_int($key) || !is_int($value))
417 {
418 return false;
419 }
420 }
421
422 return true;
423 }
424
429 private static function getPrimaryFieldName(): string
430 {
431 $primaryField = static::getDataClass()::getEntity()->getPrimary();
432
433 if (!is_scalar($primaryField))
434 {
435 throw new \Bitrix\Main\SystemException('Do not support composite primary keys');
436 }
437
438 return $primaryField;
439 }
440}
setDataEntityCollection(ORM\Objectify\Collection $entityCollection)
offsetSet($offset, $entry)
static find(array $filter, array $order, ?int $limit=null, ?Context $context=null)
static getCollectionElementClass()
__construct($source=null)
ORM Objectify Collection $dataEntityCollection
add(ActiveRecord $entry)
initByEntitiesCollection(ORM\Objectify\Collection $entitiesCollection)
initByArray(array $items)
const NOT_FOUND
Definition Error.php:9