commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
MapperScalarSequence.php
1<?php
2
3declare(strict_types=1);
10namespace Commercetools\Base;
11
12use stdClass;
13
18abstract class MapperScalarSequence 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
127 final protected function addToIndex(string $field, string $key, int $index): void
128 {
129 $this->indexes[$field][$key] = $index;
130 }
131
135 final protected function valueByKey(string $field, string $key)
136 {
137 return isset($this->indexes[$field][$key]) ? $this->at($this->indexes[$field][$key]) : null;
138 }
139
140 public function getIterator(): MapperIterator
141 {
142 $keys = !is_null($this->data) ? array_keys($this->data) : [];
143 $keyIterator = new \ArrayIterator(array_combine($keys, $keys));
144 $iterator = new MapperIterator(
145 $keyIterator,
146 $this->mapper()
147 );
148 $iterator->rewind();
149
150 return $iterator;
151 }
152
156 public function current()
157 {
159 return $this->iterator->current();
160 }
161
165 public function end()
166 {
167 if ($this->data == null) {
168 return null;
169 }
170 $arrayKeys = array_keys($this->data);
171 $lastKey = array_pop($arrayKeys);
172
173 return $this->at($lastKey);
174 }
175
179 public function next()
180 {
181 $this->iterator->next();
182 }
183
187 public function key()
188 {
190 return $this->iterator->key();
191 }
192
196 public function valid()
197 {
198 return $this->iterator->valid();
199 }
200
204 public function rewind()
205 {
206 $this->iterator->rewind();
207 }
208
213 public function offsetExists($offset)
214 {
215 return !is_null($this->data) && array_key_exists($offset, $this->data);
216 }
217
222 public function offsetGet($offset)
223 {
224 return $this->at($offset);
225 }
226
233 public function offsetSet($offset, $value)
234 {
235 $this->set($value, $offset);
236 $this->iterator = $this->getIterator();
237 }
238
243 public function offsetUnset($offset)
244 {
245 if ($this->offsetExists($offset)) {
247 unset($this->data[$offset]);
248 $this->iterator = $this->getIterator();
249 }
250 }
251
255 final public static function of()
256 {
257 return new static();
258 }
259}
addToIndex(string $field, string $key, int $index)