1 | <?php |
||
12 | abstract class MoipResource implements JsonSerializable |
||
13 | { |
||
14 | /** |
||
15 | * Version of API. |
||
16 | * |
||
17 | * @const string |
||
18 | */ |
||
19 | const VERSION = 'v2'; |
||
20 | |||
21 | /** |
||
22 | * @var \Moip\Moip |
||
23 | */ |
||
24 | protected $moip; |
||
25 | |||
26 | /** |
||
27 | * @var \stdClass |
||
28 | */ |
||
29 | protected $data; |
||
30 | |||
31 | /** |
||
32 | * Initialize a new instance. |
||
33 | */ |
||
34 | abstract protected function initialize(); |
||
35 | |||
36 | /** |
||
37 | * Mount information of a determined object. |
||
38 | * |
||
39 | * @param \stdClass $response |
||
40 | * |
||
41 | * @return mixed |
||
42 | */ |
||
43 | abstract protected function populate(stdClass $response); |
||
44 | |||
45 | /** |
||
46 | * Create a new instance. |
||
47 | * |
||
48 | * @param \Moip\Moip $moip |
||
49 | */ |
||
50 | public function __construct(Moip $moip) |
||
56 | |||
57 | /** |
||
58 | * Get a key of an object if it exists. |
||
59 | * |
||
60 | * @param string $key |
||
61 | * @param \stdClass|null $data |
||
62 | * |
||
63 | * @return mixed |
||
64 | */ |
||
65 | protected function getIfSet($key, stdClass $data = null) |
||
75 | |||
76 | protected function getIfSetDateFmt($key, $fmt, stdClass $data = null) |
||
85 | |||
86 | /** |
||
87 | * Get a key, representing a date (Y-m-d), of an object if it exists. |
||
88 | * |
||
89 | * @param string $key |
||
90 | * @param stdClass|null $data |
||
91 | * |
||
92 | * @return \DateTime|null |
||
93 | */ |
||
94 | protected function getIfSetDate($key, stdClass $data = null) |
||
98 | |||
99 | /** |
||
100 | * Get a key representing a datetime (\Datetime::ATOM), of an object if it exists. |
||
101 | * |
||
102 | * @param string $key |
||
103 | * @param stdClass|null $data |
||
104 | * |
||
105 | * @return \DateTime|null |
||
106 | */ |
||
107 | protected function getIfSetDateTime($key, stdClass $data = null) |
||
111 | |||
112 | /** |
||
113 | * Specify data which should be serialized to JSON. |
||
114 | * |
||
115 | * @return \stdClass |
||
116 | */ |
||
117 | public function jsonSerialize() |
||
121 | |||
122 | /** |
||
123 | * Execute a http request. If payload == null no body will be sent. Empty body ('{}') is supported by sending a |
||
124 | * empty stdClass. |
||
125 | * |
||
126 | * @param string $path |
||
127 | * @param string $method |
||
128 | * @param mixed|null $payload |
||
129 | * |
||
130 | * @throws Exceptions\ValidationException if the API returns a 4xx http status code. Usually means invalid data was sent. |
||
131 | * @throws Exceptions\UnautorizedException if the API returns a 401 http status code. Check API token and key. |
||
132 | * @throws Exceptions\UnexpectedException if the API returns a 500 http status code or something unexpected happens (ie.: Network error). |
||
133 | * |
||
134 | * @return stdClass |
||
135 | */ |
||
136 | protected function httpRequest($path, $method, $payload = null) |
||
168 | |||
169 | /** |
||
170 | * Find by path. |
||
171 | * |
||
172 | * @param string $path |
||
173 | * |
||
174 | * @return stdClass |
||
175 | */ |
||
176 | public function getByPath($path) |
||
182 | |||
183 | /** |
||
184 | * Create a new item in Moip. |
||
185 | * |
||
186 | * @param string $path |
||
187 | * |
||
188 | * @return stdClass |
||
189 | */ |
||
190 | public function createResource($path) |
||
196 | } |
||
197 |
If a method or function can return multiple different values and unless you are sure that you only can receive a single value in this context, we recommend to add an additional type check:
If this a common case that PHP Analyzer should handle natively, please let us know by opening an issue.