commercetools-sdk-php-v2  master
The platform, import-api and ml-api PHP sdks generated from our api reference.
MapperArraySequence.php
1 <?php
2 
3 declare(strict_types=1);
11 
12 use stdClass;
13 
19 abstract class MapperArraySequence implements CSequence
20 {
22  private $data;
24  private $indexes = [];
26  private $iterator;
27 
32  final public function __construct(array $data = null)
33  {
34  if (!is_null($data)) {
35  $this->index($data);
36  }
37  $this->data = $data;
38  $this->iterator = $this->getIterator();
39  }
40 
41  public function toArray(): ?array
42  {
43  return $this->data;
44  }
45 
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 }
addToIndex(string $field, string $key, int $index)