commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
MapperMap.php
1<?php
2
3declare(strict_types=1);
10namespace Commercetools\Base;
11
12use stdClass;
13
18abstract class MapperMap implements CMap
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
45 final public static function of($data = null)
46 {
47 if (is_array($data)) {
48 return self::fromArray($data);
49 }
51 return self::fromStdClass($data);
52 }
53
57 public function toArray(): ?array
58 {
59 return $this->data;
60 }
61
62 #[\ReturnTypeWillChange]
66 public function jsonSerialize(): ?array
67 {
68 return $this->data;
69 }
70
75 final public static function fromStdClass(stdClass $data = null)
76 {
78 $t = (array)$data;
79 return new static($t);
80 }
81
87 final public static function fromArray(array $data)
88 {
89 return new static($data);
90 }
91
95 protected function index($data): void
96 {
97 }
98
102 final protected function get(string $key)
103 {
104 if (isset($this->data[$key])) {
105 return $this->data[$key];
106 }
107 return null;
108 }
109
113 final protected function set($data, string $key): void
114 {
115 $this->data[$key] = $data;
116 }
117
123 public function put(string $key, $value)
124 {
125 return $this->store($key, $value);
126 }
127
134 final protected function store(string $key, $value)
135 {
136 $this->set($value, $key);
137 $this->iterator = $this->getIterator();
138
139 return $this;
140 }
141
145 public function at(string $key)
146 {
147 return $this->mapper()($key);
148 }
149
153 public function with(string $key, callable $callable = null)
154 {
155 $data = $this->at($key);
156 if (is_null($callable)) {
157 return $data;
158 }
159
160 return $callable($data);
161 }
162
166 abstract protected function mapper();
167
171 final protected function addToIndex(string $field, string $key, string $indexKey): void
172 {
173 $this->indexes[$field][$key] = $indexKey;
174 }
175
179 final protected function valueByKey(string $field, string $key)
180 {
181 return isset($this->indexes[$field][$key]) ? $this->at($this->indexes[$field][$key]) : null;
182 }
183
184 public function getIterator(): MapperIterator
185 {
186 $keys = !is_null($this->data) ? array_keys($this->data) : [];
187 $keyIterator = new \ArrayIterator(array_combine($keys, $keys));
188 $iterator = new MapperIterator(
189 $keyIterator,
190 $this->mapper()
191 );
192 $iterator->rewind();
193
194 return $iterator;
195 }
196
200 public function current()
201 {
203 return $this->iterator->current();
204 }
205
209 public function next()
210 {
211 $this->iterator->next();
212 }
213
217 public function key()
218 {
220 return $this->iterator->key();
221 }
222
226 public function valid()
227 {
228 return $this->iterator->valid();
229 }
230
234 public function rewind()
235 {
236 $this->iterator->rewind();
237 }
238
243 public function offsetExists($offset)
244 {
245 return !is_null($this->data) && array_key_exists($offset, $this->data);
246 }
247
252 public function offsetGet($offset)
253 {
254 return $this->at($offset);
255 }
256
263 public function offsetSet($offset, $value)
264 {
265 $this->store($offset, $value);
266 }
267
272 public function offsetUnset($offset)
273 {
274 if ($this->offsetExists($offset)) {
276 unset($this->data[$offset]);
277 $this->iterator = $this->getIterator();
278 }
279 }
280}
static of($data=null)
Definition MapperMap.php:45
__construct(array $data=null)
Definition MapperMap.php:31
put(string $key, $value)
addToIndex(string $field, string $key, string $indexKey)
static fromArray(array $data)
Definition MapperMap.php:87
with(string $key, callable $callable=null)
valueByKey(string $field, string $key)
static fromStdClass(stdClass $data=null)
Definition MapperMap.php:75
store(string $key, $value)
offsetSet($offset, $value)