Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
CompositionItemCollection.php
1<?php
2
4
5use ArrayIterator;
7use IteratorAggregate;
8
9class CompositionItemCollection implements IteratorAggregate, Arrayable
10{
12 private array $items = [];
13
14 public static function createFromModuleIds(array $moduleIds): static
15 {
16 $collection = new static();
17 array_map(static function (string $moduleId) use ($collection): void {
18 $item = AbstractCompositionItem::createFromModuleId($moduleId);
19 !is_null($item) && $collection->addItem($item);
20 }, $moduleIds);
21
22 return $collection;
23 }
24
25 public function getIterator(): ArrayIterator
26 {
27 return new ArrayIterator($this->items);
28 }
29
30 public function addItem(AbstractCompositionItem $item): static
31 {
32 if (!$this->has($item))
33 {
34 $this->items[] = $item;
35 }
36
37 return $this;
38 }
39
40 public function remove(string $moduleId): static
41 {
42 foreach ($this->items as $key => $item)
43 {
44 if ($item->getModuleId() === $moduleId)
45 {
46 unset($this->items[$key]);
47 }
48 }
49
50 return $this;
51 }
52
53 public function has(AbstractCompositionItem $item): bool
54 {
55 return in_array(
56 $item->getModuleId(),
57 array_map(fn (AbstractCompositionItem $compositionItem): string => $compositionItem->getModuleId(),
58 $this->items),
59 true
60 );
61 }
62
63 public function fillBoundItems(): static
64 {
65 foreach ($this->items as $item)
66 {
67 $item->hasBoundItem() && $this->addItem($item->getBoundItem());
68 }
69
70 return $this;
71 }
72
73 public function hideItems(): static
74 {
75 foreach ($this->items as $item)
76 {
77 $item->isHidden() && $this->remove($item->getModuleId());
78 }
79
80 return $this;
81 }
82
83 public function toArray(): array
84 {
85 $moduleIds = [];
86 foreach ($this->items as $item)
87 {
88 $moduleIds[] = $item->getModuleId();
89 }
90
91 return $moduleIds;
92 }
93}