Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
itemcollection.php
1<?
2
4
5class ItemCollection implements \IteratorAggregate, \JsonSerializable
6{
7 private $items = [];
8 private $itemsByEntity = [];
9
10 public function __construct()
11 {
12 }
13
14 public function add(Item $item): bool
15 {
16 if ($this->has($item))
17 {
18 return false;
19 }
20
21 if (!isset($this->itemsByEntity[$item->getEntityId()]))
22 {
23 $this->itemsByEntity[$item->getEntityId()] = [];
24 }
25
26 $this->itemsByEntity[$item->getEntityId()][$item->getId()] = $item;
27 $this->items[] = $item;
28
29 return true;
30 }
31
32 public function get(string $entityId, $itemId): ?Item
33 {
34 return $this->itemsByEntity[$entityId][$itemId] ?? null;
35 }
36
37 public function has(Item $item): bool
38 {
39 return isset($this->itemsByEntity[$item->getEntityId()][$item->getId()]);
40 }
41
42 public function getAll(): array
43 {
44 return $this->items;
45 }
46
47 public function count(): int
48 {
49 return count($this->items);
50 }
51
52 public function getEntityItems(string $entityId): array
53 {
54 return $this->itemsByEntity[$entityId] ?? [];
55 }
56
57 public function toJsObject(): string
58 {
59 $items = $this->toArray();
60
61 return \CUtil::phpToJSObject($items, false, false, true);
62 }
63
64 public function toArray(): array
65 {
66 return array_map(function(Item $item) {
67 return $item->toArray();
68 }, $this->getAll());
69 }
70
71 public function jsonSerialize()
72 {
73 return $this->items;
74 }
75
76 public function getIterator(): \ArrayIterator
77 {
78 return new \ArrayIterator($this->items);
79 }
80}