Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
immutable.php
1<?php
2
4use Bitrix\Mail\Item\Base as Item;
5
10abstract class Immutable implements \Iterator, \Countable
11{
12 protected $position = 0;
13
15 protected $items = array();
16
17 protected function appendItem(Item $item, $throwException = true)
18 {
19 if ($this->ensureItem($item, $throwException)) {
20 $this->items[] = $item;
21 return true;
22 }
23 return false;
24 }
25
26 protected function appendCollection(Immutable $collection, $throwException = true)
27 {
28 foreach ($collection as $item) {
29 $this->appendItem($item, $throwException);
30 }
31 }
32
33 public function ensureItem(Item $item, $throwException = true)
34 {
35 if ($throwException) {
36 // TODO: unsupported item special exception
37 throw new \Bitrix\Main\SystemException('unsupported item');
38 }
39 return false;
40 }
41
42 abstract public function createItem(array $array);
43
44 public function __construct(array $data = [])
45 {
46 foreach ($data as $item) {
47 $this->appendItem($item);
48 }
49 }
50
56 public static function fromArray(array $array)
57 {
58 $collection = new static();
59 foreach ($array as $row) {
60 if ($item = $collection->createItem($row)) {
61 $collection->appendItem($item);
62 }
63 }
64 return $collection;
65 }
66
67 public function getFirst(&$position)
68 {
69 $position = 0;
70 return $this->getNext($position);
71 }
72
73 public function getNext(&$position)
74 {
75 return isset($this->items[$position]) ? $this->items[$position++] : null;
76 }
77
82 public function current()
83 {
84 return $this->items[$this->position];
85 }
86
91 public function next()
92 {
94 }
95
100 public function key()
101 {
102 return $this->position;
103 }
104
109 public function valid()
110 {
111 return isset($this->items[$this->position]);
112 }
113
117 public function rewind()
118 {
119 $this->position = 0;
120 }
121
126 public function count()
127 {
128 return count($this->items);
129 }
130}
ensureItem(Item $item, $throwException=true)
Definition immutable.php:33
appendCollection(Immutable $collection, $throwException=true)
Definition immutable.php:26
appendItem(Item $item, $throwException=true)
Definition immutable.php:17
static fromArray(array $array)
Definition immutable.php:56