Bitrix-D7  20.5.0
collectionbase.php
См. документацию.
1 <?php
2 
3 namespace Bitrix\Sale\Internals;
4 
6 
7 Loc::loadMessages(__FILE__);
8 
9 /**
10  * Class CollectionBase
11  * @package Bitrix\Sale\Internals
12  */
13 abstract class CollectionBase
14  implements \ArrayAccess, \Countable, \IteratorAggregate
15 {
16  /** @var CollectableEntity[] $collection */
17  protected $collection = array();
18 
19  /**
20  * @return \ArrayIterator
21  */
22  public function getIterator()
23  {
24  return new \ArrayIterator($this->collection);
25  }
26 
27 
28  /**
29  * Whether a offset exists
30  */
31  public function offsetExists($offset)
32  {
33  return isset($this->collection[$offset]) || array_key_exists($offset, $this->collection);
34  }
35 
36  /**
37  * Offset to retrieve
38  */
39  public function offsetGet($offset)
40  {
41  if (isset($this->collection[$offset]) || array_key_exists($offset, $this->collection))
42  {
43  return $this->collection[$offset];
44  }
45 
46  return null;
47  }
48 
49  /**
50  * Offset to set
51  */
52  public function offsetSet($offset, $value)
53  {
54  if($offset === null)
55  {
56  $this->collection[] = $value;
57  }
58  else
59  {
60  $this->collection[$offset] = $value;
61  }
62  }
63 
64  /**
65  * Offset to unset
66  */
67  public function offsetUnset($offset)
68  {
69  unset($this->collection[$offset]);
70  }
71 
72  /**
73  * Count elements of an object
74  */
75  public function count()
76  {
77  return count($this->collection);
78  }
79 
80  /**
81  * Return the current element
82  */
83  public function current()
84  {
85  return current($this->collection);
86  }
87 
88  /**
89  * Move forward to next element
90  */
91  public function next()
92  {
93  return next($this->collection);
94  }
95 
96  /**
97  * Return the key of the current element
98  */
99  public function key()
100  {
101  return key($this->collection);
102  }
103 
104  /**
105  * Checks if current position is valid
106  */
107  public function valid()
108  {
109  $key = $this->key();
110  return $key !== null;
111  }
112 
113  /**
114  * Rewind the Iterator to the first element
115  */
116  public function rewind()
117  {
118  return reset($this->collection);
119  }
120 
121  /**
122  * Checks if collection is empty.
123  *
124  * @return bool
125  */
126  public function isEmpty()
127  {
128  return empty($this->collection);
129  }
130 
131  public function toArray()
132  {
133  $result = [];
134 
135  foreach ($this->collection as $entity)
136  {
137  $result[] = $entity->toArray();
138  }
139 
140  return $result;
141  }
142 }
static loadMessages($file)
Loads language messages for specified file in a lazy way.
Definition: loc.php:67
offsetUnset($offset)
Offset to unset.
offsetExists($offset)
Whether a offset exists.
offsetGet($offset)
Offset to retrieve.
key()
Return the key of the current element.
valid()
Checks if current position is valid.
offsetSet($offset, $value)
Offset to set.
count()
Count elements of an object.
next()
Move forward to next element.
rewind()
Rewind the Iterator to the first element.
isEmpty()
Checks if collection is empty.
current()
Return the current element.