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