commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
Loading...
Searching...
No Matches
JsonObjectModel.php
1<?php
2
3declare(strict_types=1);
10namespace Commercetools\Base;
11
12use stdClass;
13
15{
19 final public function get(string $field)
20 {
21 $data = $this->raw($field);
22 if ($data instanceof stdClass) {
23 return JsonObjectModel::of($data);
24 }
25 if (is_array($data) && isset($data[0]) && $data[0] instanceof stdClass) {
27 return new JsonObjectCollection($data);
28 }
29 return $data;
30 }
31
32 final protected function toArray(): array
33 {
34 $data = array_filter(
35 get_object_vars($this),
40 function ($value) {
41 return !is_null($value);
42 }
43 );
44 $data = array_replace($this->getRawDataArray(), $data);
45 return $data;
46 }
47
51 public function with(callable $callable = null)
52 {
53 if (is_null($callable)) {
54 return $this;
55 }
56
57 return $callable($this);
58 }
59}