commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
MapperSequence.php
1<?php
2
3declare(strict_types=1);
10namespace Commercetools\Base;
11
12use stdClass;
13
18abstract class MapperSequence implements CSequence
19{
21 private $data;
23 private $indexes = [];
25 private $iterator;
26
31 final public function __construct(array $data = null)
32 {
33 if (!is_null($data)) {
34 $this->index($data);
35 }
36 $this->data = $data;
37 $this->iterator = $this->getIterator();
38 }
39
40 public function toArray(): ?array
41 {
42 return $this->data;
43 }
44
45 #[\ReturnTypeWillChange]
46 public function jsonSerialize(): ?array
47 {
48 return $this->data;
49 }
50
56 final public static function fromArray(array $data)
57 {
58 return new static($data);
59 }
60
64 protected function index($data): void
65 {
66 }
67
71 final protected function get(?int $index)
72 {
73 if (isset($this->data[$index])) {
74 return $this->data[$index];
75 }
76 return null;
77 }
78
82 final protected function set($data, ?int $index): void
83 {
84 if (is_null($index)) {
85 $this->data[] = $data;
86 } else {
87 $this->data[$index] = $data;
88 }
89 }
90
96 public function add($value)
97 {
98 return $this->store($value);
99 }
100
106 final protected function store($value)
107 {
108 $this->set($value, null);
109 $this->iterator = $this->getIterator();
110
111 return $this;
112 }
113
117 public function at(int $index)
118 {
119 return $this->mapper()($index);
120 }
121
125 abstract protected function mapper();
126
130 final protected function addToIndex(string $field, string $key, int $index): void
131 {
132 $this->indexes[$field][$key] = $index;
133 }
134
138 final protected function valueByKey(string $field, string $key)
139 {
140 return isset($this->indexes[$field][$key]) ? $this->at($this->indexes[$field][$key]) : null;
141 }
142
143 public function getIterator(): MapperIterator
144 {
145 $keys = !is_null($this->data) ? array_keys($this->data) : [];
146 $keyIterator = new \ArrayIterator(array_combine($keys, $keys));
147 $iterator = new MapperIterator(
148 $keyIterator,
149 $this->mapper()
150 );
151 $iterator->rewind();
152
153 return $iterator;
154 }
155
159 public function current()
160 {
162 return $this->iterator->current();
163 }
164
168 public function end()
169 {
170 if ($this->data == null) {
171 return null;
172 }
173 $arrayKeys = array_keys($this->data);
174 $lastKey = array_pop($arrayKeys);
175
176 return $this->at($lastKey);
177 }
178
182 public function next()
183 {
184 $this->iterator->next();
185 }
186
190 public function key()
191 {
193 return $this->iterator->key();
194 }
195
199 public function valid()
200 {
201 return $this->iterator->valid();
202 }
203
207 public function rewind()
208 {
209 $this->iterator->rewind();
210 }
211
216 public function offsetExists($offset)
217 {
218 return !is_null($this->data) && array_key_exists($offset, $this->data);
219 }
220
225 public function offsetGet($offset)
226 {
227 return $this->at($offset);
228 }
229
236 public function offsetSet($offset, $value)
237 {
238 $this->set($value, $offset);
239 $this->iterator = $this->getIterator();
240 }
241
246 public function offsetUnset($offset)
247 {
248 if ($this->offsetExists($offset)) {
250 unset($this->data[$offset]);
251 $this->iterator = $this->getIterator();
252 }
253 }
254
258 final public static function of()
259 {
260 return new static();
261 }
262}
addToIndex(string $field, string $key, int $index)
valueByKey(string $field, string $key)