1 | <?php |
||
16 | abstract class MoipResource implements JsonSerializable |
||
17 | { |
||
18 | /** |
||
19 | * Version of API. |
||
20 | * |
||
21 | * @const string |
||
22 | */ |
||
23 | const VERSION = 'v2'; |
||
24 | |||
25 | /** |
||
26 | * @var \Moip\Moip |
||
27 | */ |
||
28 | protected $moip; |
||
29 | |||
30 | /** |
||
31 | * @var \stdClass |
||
32 | */ |
||
33 | protected $data; |
||
34 | |||
35 | /** |
||
36 | * Initialize a new instance. |
||
37 | */ |
||
38 | abstract protected function initialize(); |
||
39 | |||
40 | /** |
||
41 | * Mount information of a determined object. |
||
42 | * |
||
43 | * @param \stdClass $response |
||
44 | * |
||
45 | * @return mixed |
||
46 | */ |
||
47 | abstract protected function populate(stdClass $response); |
||
48 | |||
49 | /** |
||
50 | * Create a new instance. |
||
51 | * |
||
52 | * @param \Moip\Moip $moip |
||
53 | */ |
||
54 | public function __construct(Moip $moip) |
||
60 | |||
61 | /** |
||
62 | * Get a key of an object if it exists. |
||
63 | * |
||
64 | * @param string $key |
||
65 | * @param \stdClass|null $data |
||
66 | * |
||
67 | * @return mixed |
||
68 | */ |
||
69 | protected function getIfSet($key, stdClass $data = null) |
||
79 | |||
80 | /** |
||
81 | * @return \Moip\Helper\Links |
||
82 | */ |
||
83 | public function getLinks() |
||
91 | |||
92 | /** |
||
93 | * @param $key |
||
94 | * @param $fmt |
||
95 | * @param stdClass|null $data |
||
96 | * |
||
97 | * @return bool|\DateTime|null |
||
98 | */ |
||
99 | protected function getIfSetDateFmt($key, $fmt, stdClass $data = null) |
||
108 | |||
109 | /** |
||
110 | * Get a key, representing a date (Y-m-d), of an object if it exists. |
||
111 | * |
||
112 | * @param string $key |
||
113 | * @param stdClass|null $data |
||
114 | * |
||
115 | * @return \DateTime|null |
||
116 | */ |
||
117 | protected function getIfSetDate($key, stdClass $data = null) |
||
121 | |||
122 | /** |
||
123 | * Get a key representing a datetime (\Datetime::ATOM), of an object if it exists. |
||
124 | * |
||
125 | * @param string $key |
||
126 | * @param stdClass|null $data |
||
127 | * |
||
128 | * @return \DateTime|null |
||
129 | */ |
||
130 | protected function getIfSetDateTime($key, stdClass $data = null) |
||
134 | |||
135 | /** |
||
136 | * Specify data which should be serialized to JSON. |
||
137 | * |
||
138 | * @return \stdClass |
||
139 | */ |
||
140 | public function jsonSerialize() |
||
144 | |||
145 | /** |
||
146 | * Generate URL to request. |
||
147 | * |
||
148 | * @param $action |
||
149 | * @param $id |
||
150 | * |
||
151 | * @return string |
||
152 | */ |
||
153 | public function generatePath($action, $id = null) |
||
161 | |||
162 | /** |
||
163 | * Execute a http request. If payload == null no body will be sent. Empty body ('{}') is supported by sending a |
||
164 | * empty stdClass. |
||
165 | * |
||
166 | * @param string $path |
||
167 | * @param string $method |
||
168 | * @param mixed|null $payload |
||
169 | * |
||
170 | * @throws Exceptions\ValidationException if the API returns a 4xx http status code. Usually means invalid data was sent. |
||
171 | * @throws Exceptions\UnautorizedException if the API returns a 401 http status code. Check API token and key. |
||
172 | * @throws Exceptions\UnexpectedException if the API returns a 500 http status code or something unexpected happens (ie.: Network error). |
||
173 | * |
||
174 | * @return stdClass |
||
175 | */ |
||
176 | protected function httpRequest($path, $method, $payload = null) |
||
208 | |||
209 | /** |
||
210 | * Find by path. |
||
211 | * |
||
212 | * @param string $path |
||
213 | * |
||
214 | * @return stdClass |
||
215 | */ |
||
216 | public function getByPath($path) |
||
222 | |||
223 | /** |
||
224 | * Create a new item in Moip. |
||
225 | * |
||
226 | * @param string $path |
||
227 | * |
||
228 | * @return stdClass |
||
229 | */ |
||
230 | public function createResource($path) |
||
236 | |||
237 | /** |
||
238 | * Delete a new item in Moip. |
||
239 | * |
||
240 | * @param $path |
||
241 | * |
||
242 | * @return mixed |
||
243 | */ |
||
244 | public function deleteByPath($path) |
||
248 | } |
||
249 |