Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
preselectedcollection.php
1<?
2
4
5class PreselectedCollection implements \IteratorAggregate, \JsonSerializable
6{
7 private $items = [];
8 private $itemsByEntity = [];
9
10 public function __construct()
11 {
12 }
13
14 public function add(PreselectedItem $preselectedItem)
15 {
16 if ($this->has($preselectedItem))
17 {
18 return;
19 }
20
21 if (!isset($this->itemsByEntity[$preselectedItem->getEntityId()]))
22 {
23 $this->itemsByEntity[$preselectedItem->getEntityId()] = [];
24 }
25
26 $this->itemsByEntity[$preselectedItem->getEntityId()][$preselectedItem->getId()] = $preselectedItem;
27 $this->items[] = $preselectedItem;
28 }
29
30 public function load(array $ids)
31 {
32 foreach ($ids as $itemId)
33 {
34 if (!is_array($itemId) || count($itemId) !== 2)
35 {
36 continue;
37 }
38
39 [$entityId, $id] = $itemId;
40 if (is_string($entityId) && (is_string($id) || is_int($id)))
41 {
42 if ($this->get($entityId, $id) === null)
43 {
44 $this->add(new PreselectedItem(['entityId' => $entityId, 'id' => $id]));
45 }
46 }
47 }
48 }
49
50 public function get(string $entityId, $itemId): ?PreselectedItem
51 {
52 return $this->itemsByEntity[$entityId][$itemId] ?? null;
53 }
54
55 public function has(PreselectedItem $preselectedItem): bool
56 {
57 return isset($this->itemsByEntity[$preselectedItem->getEntityId()][$preselectedItem->getId()]);
58 }
59
60 public function getByItem(Item $item): ?PreselectedItem
61 {
62 return $this->itemsByEntity[$item->getEntityId()][$item->getId()] ?? null;
63 }
64
65 public function getAll()
66 {
67 return $this->items;
68 }
69
70 public function getItems()
71 {
72 return $this->itemsByEntity;
73 }
74
75 public function count(): int
76 {
77 return count($this->items);
78 }
79
86 public function getEntityItems(string $entityId): array
87 {
88 return $this->itemsByEntity[$entityId] ?? [];
89 }
90
94 public function getEntities()
95 {
96 return array_keys($this->itemsByEntity);
97 }
98
99 public function jsonSerialize()
100 {
101 return $this->getAll();
102 }
103
104 public function getIterator()
105 {
106 return new \ArrayIterator($this->items);
107 }
108}