commercetools-sdk-php-v2  master
The platform, import-api and ml-api PHP sdks generated from our api reference.
JsonObjectModel.php
1 <?php
2 
3 declare(strict_types=1);
10 namespace Commercetools\Base;
11 
12 use stdClass;
13 
14 class JsonObjectModel extends BaseJsonObject implements JsonObject
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 }
with(callable $callable=null)