Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
componentscollection.php
1<?php
2
3
5
6
7use ArrayIterator;
8use IteratorAggregate;
9use LogicException;
10
11class ComponentsCollection implements IteratorAggregate
12{
16 private $collection;
17
22 public static function createInstance(array $collection = null): ComponentsCollection
23 {
24 return new self($collection);
25 }
26
31 public function __construct(array $collection = null)
32 {
33 if (!$this->checkCollection($collection))
34 {
35 throw new LogicException('The collection contains elements of the wrong class. You need to pass a collection of objects Bitrix\\Calendar\\ICal\\Builder\\Attendee');
36 }
37
38 $this->collection = $collection ?? [];
39 }
40
44 public function getIterator(): ArrayIterator
45 {
46 return new ArrayIterator($this->collection);
47 }
48
53 public function add(?ParserComponent $component): ComponentsCollection
54 {
55 if ($component === null)
56 {
57 return $this;
58 }
59
60 $this->collection[] = $component;
61
62 return $this;
63 }
64
68 public function count(): int
69 {
70 return count($this->collection);
71 }
72
76 public function hasOneComponent(): bool
77 {
78 return $this->count() === 1;
79 }
80
84 public function hasOneCalendarComponent(): bool
85 {
86 return $this->hasOneComponent() && ($this->collection[0] instanceof Calendar);
87 }
88
93 private function checkCollection(?array $collection): bool
94 {
95 if ($collection === null)
96 {
97 return true;
98 }
99
100 $attendee = array_filter($collection, function ($attendee) {
101 return !($attendee instanceof ParserComponent);
102 });
103
104 return empty($attendee);
105 }
106}