1 | <?php |
||
14 | class JsonObject extends AbstractJsonDeserializeObject implements \JsonSerializable |
||
15 | { |
||
16 | use ContextTrait; |
||
17 | |||
18 | const TYPE = 'type'; |
||
19 | const OPTIONAL = 'optional'; |
||
20 | const INITIALIZED = 'initialized'; |
||
21 | const DECORATOR = 'decorator'; |
||
22 | const ELEMENT_TYPE = 'elementType'; |
||
23 | |||
24 | /** |
||
25 | * @param array $data |
||
26 | * @param Context|callable $context |
||
27 | */ |
||
28 | 960 | public function __construct(array $data = [], $context = null) |
|
32 | |||
33 | /** |
||
34 | * @return array |
||
35 | * @internal |
||
36 | */ |
||
37 | 1 | public function fieldDefinitions() |
|
41 | |||
42 | /** |
||
43 | * @param $method |
||
44 | * @param $arguments |
||
45 | * @return $this|bool|mixed |
||
46 | * @internal |
||
47 | */ |
||
48 | 665 | public function __call($method, $arguments) |
|
49 | { |
||
50 | 665 | $action = substr($method, 0, 3); |
|
51 | 665 | $field = lcfirst(substr($method, 3)); |
|
52 | |||
53 | 665 | if (!$this->hasField($field)) { |
|
54 | 3 | if ($action == 'get' || $action == 'set') { |
|
55 | 3 | throw new \BadMethodCallException( |
|
56 | 3 | sprintf(Message::UNKNOWN_FIELD, $field, $method, implode(', ', $arguments)) |
|
57 | ); |
||
58 | } else { |
||
59 | throw new \BadMethodCallException(sprintf(Message::UNKNOWN_METHOD, $method, $field)); |
||
60 | } |
||
61 | } |
||
62 | switch ($action) { |
||
63 | 663 | case 'get': |
|
64 | 382 | return $this->get($field); |
|
65 | 1 | case 'set': |
|
66 | 620 | $this->set($field, isset($arguments[0]) ? $arguments[0] : null); |
|
67 | 618 | return $this; |
|
68 | default: |
||
69 | 1 | throw new \BadMethodCallException(sprintf(Message::UNKNOWN_METHOD, $method, $field)); |
|
70 | } |
||
71 | } |
||
72 | |||
73 | public function __get($field) |
||
74 | { |
||
75 | if (!$this->hasField($field)) { |
||
76 | throw new \BadMethodCallException( |
||
77 | sprintf(Message::UNKNOWN_FIELD, $field, 'get', $field) |
||
78 | ); |
||
79 | } |
||
80 | return $this->get($field); |
||
81 | } |
||
82 | |||
83 | /** |
||
84 | * @param string $field |
||
85 | * @return bool |
||
86 | * @internal |
||
87 | */ |
||
88 | 665 | protected function hasField($field) |
|
89 | { |
||
90 | 665 | if (isset($this->fieldDefinitions()[$field])) { |
|
91 | 663 | return true; |
|
92 | } |
||
93 | 3 | return false; |
|
94 | } |
||
95 | |||
96 | /** |
||
97 | * @param string $field |
||
98 | * @return array |
||
99 | * @internal |
||
100 | */ |
||
101 | 663 | protected function fieldDefinition($field) |
|
105 | |||
106 | /** |
||
107 | * @param string $field |
||
108 | * @param string $key |
||
109 | * @param $default |
||
110 | * @return bool|string |
||
111 | * @internal |
||
112 | */ |
||
113 | 663 | protected function fieldDefinitionValue($field, $key, $default = false) |
|
114 | { |
||
115 | 663 | $field = $this->fieldDefinition($field); |
|
116 | |||
117 | 663 | if (isset($field[$key])) { |
|
118 | 661 | return $field[$key]; |
|
119 | } |
||
120 | |||
121 | 655 | return $default; |
|
122 | } |
||
123 | |||
124 | /** |
||
125 | * @param string $field |
||
126 | * @return mixed |
||
127 | * @internal |
||
128 | */ |
||
129 | 383 | public function get($field) |
|
133 | |||
134 | 662 | protected function fieldDefinitionType($field) |
|
138 | /** |
||
139 | * @param string $field |
||
140 | * @internal |
||
141 | */ |
||
142 | 335 | protected function initialize($field) |
|
173 | |||
174 | 2 | public function isOptional($field) |
|
175 | { |
||
176 | 2 | return $this->fieldDefinitionValue($field, static::OPTIONAL, false); |
|
177 | } |
||
178 | |||
179 | 656 | protected function decorateField($field, $value) |
|
187 | |||
188 | /** |
||
189 | * @param string $field |
||
190 | * @param mixed $value |
||
191 | * @return $this |
||
192 | * @internal |
||
193 | */ |
||
194 | 620 | public function set($field, $value) |
|
216 | } |
||
217 |