1C-Bitrix 25.700.0
Загрузка...
Поиск...
Не найдено
BaseCollection.php
См. документацию.
1<?php
2
3declare(strict_types=1);
4
5namespace Bitrix\Rest\Entity\Collection;
6
7use Bitrix\Main\ArgumentException;
8use Closure;
9use Countable;
10use IteratorAggregate;
11use Traversable;
12
17abstract class BaseCollection implements Countable, IteratorAggregate
18{
22 private array $items = [];
23
28 public function __construct(mixed ...$items)
29 {
30 foreach ($items as $item)
31 {
32 $this->add($item);
33 }
34 }
35
39 abstract protected static function getItemClassName(): string;
40
45 public function add(mixed $item): void
46 {
47 if (!$this->isValidType($item))
48 {
49 $type = static::getItemClassName();
50 throw new ArgumentException("Item must be of type {$type}");
51 }
52
53 $this->items[] = $item;
54 }
55
59 public function get(int $index): mixed
60 {
61 return $this->items[$index] ?? null;
62 }
63
67 public function first(): mixed
68 {
69 return $this->get(0);
70 }
71
75 public function all(): array
76 {
77 return $this->items;
78 }
79
80 public function remove(int $index): void
81 {
82 if (isset($this->items[$index]))
83 {
84 unset($this->items[$index]);
85 $this->items = array_values($this->items);
86 }
87 }
88
89 public function count(): int
90 {
91 return count($this->items);
92 }
93
94 public function isEmpty(): bool
95 {
96 return $this->count() === 0;
97 }
98
102 public function getIterator(): Traversable
103 {
104 return new \ArrayIterator($this->items);
105 }
106
111 public function contains(mixed $item): bool
112 {
113 return in_array($item, $this->items, true);
114 }
115
116 public function clear(): void
117 {
118 $this->items = [];
119 }
120
126 public function filter(Closure $callback): self
127 {
128 $filtered = array_filter($this->items, $callback);
129 $collection = new static();
130
131 foreach ($filtered as $item)
132 {
133 $collection->add($item);
134 }
135
136 return $collection;
137 }
138
144 public function map(Closure $callback): array
145 {
146 return array_map($callback, $this->items);
147 }
148
149 public function __clone(): void
150 {
151 $this->items = $this->map(static fn($item) => clone $item);
152 }
153
158 private function isValidType(mixed $item): bool
159 {
160 return $item instanceof (static::getItemClassName());
161 }
162}
$type
Определения options.php:106
__construct(mixed ... $items)
Определения BaseCollection.php:28
</td ></tr ></table ></td ></tr >< tr >< td class="bx-popup-label bx-width30"><?=GetMessage("PAGE_NEW_TAGS")?> array( $site)
Определения file_new.php:804
$items
Определения template.php:224