Bitrix-D7 23.9
 
Загрузка...
Поиск...
Не найдено
collection.php
1<?php
3
4use Traversable;
5
6class Collection implements \IteratorAggregate
7{
8 protected $collection = [];
9 static $internalIndex = 0;
10
11 public function addItem(Item $item)
12 {
13 $index = $item->getInternalIndex() == '' ? 'n'.static::$internalIndex++ : $item->getInternalIndex();
14
15 $this->collection[$index] = $item;
16 }
17
18 public function count()
19 {
20 return count($this->collection);
21 }
22
23 public function toArray()
24 {
25 $result = [];
26 if(count($this->collection)>0)
27 {
31 foreach ($this->collection as $index=>$item)
32 {
33 $result[$index] = $item->getEntity()->getFieldsValues();
34 }
35 }
36 return $result;
37 }
38
39 public function getIndexes()
40 {
41 return count($this->collection)>0 ? array_keys($this->collection):[];
42 }
43
48 public function getItemByIndex($index)
49 {
50 $result = null;
51 if(count($this->collection)>0)
52 {
53 foreach ($this->collection as $k=>$item)
54 {
55 if($index == $k)
56 {
57 $result = $item;
58 break;
59 }
60 }
61 }
62 return $result;
63 }
64
72 public function getIterator()
73 {
74 return new \ArrayIterator($this->collection);
75 }
76}