commercetools-sdk-php-v2
The commercetools platform, import-api and PHP sdks generated from our api reference.
BaseJsonObject.php
1 <?php
2 
3 declare(strict_types=1);
10 namespace Commercetools\Base;
11 
12 use stdClass;
13 
17 abstract class BaseJsonObject implements JsonObject
18 {
20  private $rawData;
21 
26  final public static function of($data = null)
27  {
28  if (is_array($data)) {
29  return self::fromArray($data);
30  }
31  return self::fromStdClass($data);
32  }
33 
38  final public static function fromStdClass(stdClass $data = null)
39  {
40  $t = new static();
41  $t->rawData = $data;
42  return $t;
43  }
44 
49  final public static function fromArray(array $data = [])
50  {
51  return self::fromStdClass((object)$data);
52  }
53 
57  final protected function raw(string $field)
58  {
59  if (isset($this->rawData->$field)) {
64  return $this->rawData->$field;
65  }
66  return null;
67  }
68 
69  #[\ReturnTypeWillChange]
70  public function jsonSerialize()
71  {
72  return (object)$this->toArray();
73  }
74 
78  final protected function getRawDataArray(): array
79  {
80  if (is_null($this->rawData)) {
81  return [];
82  }
83  return get_object_vars($this->rawData);
84  }
85 
89  abstract protected function toArray(): array;
90 }
static fromStdClass(stdClass $data=null)
static fromArray(array $data=[])