1 | <?php |
||
18 | abstract class MoipResource implements JsonSerializable |
||
19 | { |
||
20 | /** |
||
21 | * Version of API. |
||
22 | * |
||
23 | * @const string |
||
24 | */ |
||
25 | const VERSION = 'v2'; |
||
26 | |||
27 | /** |
||
28 | * @var \Moip\Moip |
||
29 | */ |
||
30 | protected $moip; |
||
31 | |||
32 | /** |
||
33 | * @var \stdClass |
||
34 | */ |
||
35 | protected $data; |
||
36 | |||
37 | /** |
||
38 | * Initialize a new instance. |
||
39 | */ |
||
40 | abstract protected function initialize(); |
||
41 | |||
42 | /** |
||
43 | * Mount information of a determined object. |
||
44 | * |
||
45 | * @param \stdClass $response |
||
46 | * |
||
47 | * @return mixed |
||
48 | */ |
||
49 | abstract protected function populate(stdClass $response); |
||
50 | |||
51 | /** |
||
52 | * Create a new instance. |
||
53 | * |
||
54 | * @param \Moip\Moip $moip |
||
55 | */ |
||
56 | public function __construct(Moip $moip) |
||
62 | |||
63 | /** |
||
64 | * Get a key of an object if it exists. |
||
65 | * |
||
66 | * @param string $key |
||
67 | * @param \stdClass|null $data |
||
68 | * |
||
69 | * @return mixed |
||
70 | */ |
||
71 | protected function getIfSet($key, stdClass $data = null) |
||
81 | |||
82 | /** |
||
83 | * @return \Moip\Helper\Links |
||
84 | */ |
||
85 | public function getLinks() |
||
93 | |||
94 | /** |
||
95 | * @param $key |
||
96 | * @param $fmt |
||
97 | * @param stdClass|null $data |
||
98 | * |
||
99 | * @return bool|\DateTime|null |
||
100 | */ |
||
101 | protected function getIfSetDateFmt($key, $fmt, stdClass $data = null) |
||
110 | |||
111 | /** |
||
112 | * Get a key, representing a date (Y-m-d), of an object if it exists. |
||
113 | * |
||
114 | * @param string $key |
||
115 | * @param stdClass|null $data |
||
116 | * |
||
117 | * @return \DateTime|null |
||
118 | */ |
||
119 | protected function getIfSetDate($key, stdClass $data = null) |
||
123 | |||
124 | /** |
||
125 | * Get a key representing a datetime (\Datetime::ATOM), of an object if it exists. |
||
126 | * |
||
127 | * @param string $key |
||
128 | * @param stdClass|null $data |
||
129 | * |
||
130 | * @return \DateTime|null |
||
131 | */ |
||
132 | protected function getIfSetDateTime($key, stdClass $data = null) |
||
136 | |||
137 | /** |
||
138 | * Specify data which should be serialized to JSON. |
||
139 | * |
||
140 | * @return \stdClass |
||
141 | */ |
||
142 | public function jsonSerialize() |
||
146 | |||
147 | /** |
||
148 | * Generate URL to request. |
||
149 | * |
||
150 | * @param $action |
||
151 | * @param $id |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | public function generatePath($action, $id = null) |
||
163 | |||
164 | /** |
||
165 | * Generate URL to request a get list. |
||
166 | * |
||
167 | * @param Pagination $pagination |
||
168 | * @param Filters $filters |
||
169 | * @param string $qParam Query a specific value. |
||
170 | * |
||
171 | * @return string |
||
172 | */ |
||
173 | public function generateListPath(Pagination $pagination = null, Filters $filters = null, $qParam = '') |
||
197 | |||
198 | /** |
||
199 | * Execute a http request. If payload == null no body will be sent. Empty body ('{}') is supported by sending a |
||
200 | * empty stdClass. |
||
201 | * |
||
202 | * @param string $path |
||
203 | * @param string $method |
||
204 | * @param mixed|null $payload |
||
205 | * |
||
206 | * @throws Exceptions\ValidationException if the API returns a 4xx http status code. Usually means invalid data was sent. |
||
207 | * @throws Exceptions\UnautorizedException if the API returns a 401 http status code. Check API token and key. |
||
208 | * @throws Exceptions\UnexpectedException if the API returns a 500 http status code or something unexpected happens (ie.: Network error). |
||
209 | * |
||
210 | * @return stdClass |
||
211 | */ |
||
212 | protected function httpRequest($path, $method, $payload = null) |
||
244 | |||
245 | /** |
||
246 | * Find by path. |
||
247 | * |
||
248 | * @param string $path |
||
249 | * |
||
250 | * @return stdClass |
||
251 | */ |
||
252 | public function getByPath($path) |
||
258 | |||
259 | /** |
||
260 | * Create a new item in Moip. |
||
261 | * |
||
262 | * @param string $path |
||
263 | * |
||
264 | * @return stdClass |
||
265 | */ |
||
266 | public function createResource($path) |
||
272 | |||
273 | /** |
||
274 | * Delete a new item in Moip. |
||
275 | * |
||
276 | * @param $path |
||
277 | * |
||
278 | * @return mixed |
||
279 | */ |
||
280 | public function deleteByPath($path) |
||
284 | } |
||
285 |