Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
SwitcherCollection.php
1<?php
2
4
5use ArrayIterator;
6use IteratorAggregate;
7
8class SwitcherCollection implements IteratorAggregate
9{
10 private array $items = [];
11
12 public function getIterator(): ArrayIterator
13 {
14 return new ArrayIterator($this->items);
15 }
16
17 public function has(AbstractSwitcher $switcher): bool
18 {
19 return !empty(array_filter(
20 $this->items,
21 fn (AbstractSwitcher $item): bool =>
22 $this->isEquals($item, $switcher)
23 ));
24 }
25
26 public function add(AbstractSwitcher $switcher): static
27 {
28 if ($this->has($switcher))
29 {
30 return $this;
31 }
32
33 $this->items[] = $switcher;
34 return $this;
35 }
36
37 public function remove(AbstractSwitcher $switcher): static
38 {
39 if (!$this->has($switcher))
40 {
41 return $this;
42 }
43
44 foreach ($this->items as $key => $item)
45 {
46 if ($this->isEquals($item, $switcher))
47 {
48 unset($this->items[$key]);
49 }
50 }
51
52 return $this;
53 }
54
55 private function isEquals(AbstractSwitcher $a, AbstractSwitcher $b): bool
56 {
57 return $a::class === $b::class
58 && $a->getUserId() === $b->getUserId()
59 && $a->getSpaceId() === $b->getSpaceId()
60 && $a->getCode() === $b->getCode();
61 }
62}