1 | <?php |
||
13 | abstract class MoipResource implements JsonSerializable |
||
14 | { |
||
15 | /** |
||
16 | * Version of API. |
||
17 | * |
||
18 | * @const string |
||
19 | */ |
||
20 | const VERSION = 'v2'; |
||
21 | |||
22 | /** |
||
23 | * @var \Moip\Moip |
||
24 | */ |
||
25 | protected $moip; |
||
26 | |||
27 | /** |
||
28 | * @var \stdClass |
||
29 | */ |
||
30 | protected $data; |
||
31 | |||
32 | /** |
||
33 | * Initialize a new instance. |
||
34 | */ |
||
35 | abstract protected function initialize(); |
||
36 | |||
37 | /** |
||
38 | * Mount information of a determined object. |
||
39 | * |
||
40 | * @param \stdClass $response |
||
41 | * |
||
42 | * @return mixed |
||
43 | */ |
||
44 | abstract protected function populate(stdClass $response); |
||
45 | |||
46 | /** |
||
47 | * Create a new instance. |
||
48 | * |
||
49 | * @param \Moip\Moip $moip |
||
50 | */ |
||
51 | public function __construct(Moip $moip) |
||
57 | |||
58 | /** |
||
59 | * Get a key of an object if it exists. |
||
60 | * |
||
61 | * @param string $key |
||
62 | * @param \stdClass|null $data |
||
63 | * |
||
64 | * @return mixed |
||
65 | */ |
||
66 | protected function getIfSet($key, stdClass $data = null) |
||
76 | |||
77 | protected function getIfSetDateFmt($key, $fmt, stdClass $data = null) |
||
86 | |||
87 | /** |
||
88 | * Get a key, representing a date (Y-m-d), of an object if it exists. |
||
89 | * |
||
90 | * @param string $key |
||
91 | * @param stdClass|null $data |
||
92 | * |
||
93 | * @return \DateTime|null |
||
94 | */ |
||
95 | protected function getIfSetDate($key, stdClass $data = null) |
||
99 | |||
100 | /** |
||
101 | * Get a key representing a datetime (\Datetime::ATOM), of an object if it exists. |
||
102 | * |
||
103 | * @param string $key |
||
104 | * @param stdClass|null $data |
||
105 | * |
||
106 | * @return \DateTime|null |
||
107 | */ |
||
108 | protected function getIfSetDateTime($key, stdClass $data = null) |
||
112 | |||
113 | /** |
||
114 | * Specify data which should be serialized to JSON. |
||
115 | * |
||
116 | * @return \stdClass |
||
117 | */ |
||
118 | public function jsonSerialize() |
||
122 | |||
123 | /** |
||
124 | * Execute a http request. If payload == null no body will be sent. Empty body ('{}') is supported by sending a |
||
125 | * empty stdClass. |
||
126 | * |
||
127 | * @param string $path |
||
128 | * @param string $method |
||
129 | * @param mixed|null $payload |
||
130 | * |
||
131 | * @throws Exceptions\ValidationException if the API returns a 4xx http status code. Usually means invalid data was sent. |
||
132 | * @throws Exceptions\UnautorizedException if the API returns a 401 http status code. Check API token and key. |
||
133 | * @throws Exceptions\UnexpectedException if the API returns a 500 http status code or something unexpected happens (ie.: Network error). |
||
134 | * |
||
135 | * @return stdClass |
||
136 | */ |
||
137 | protected function httpRequest($path, $method, $payload = null) |
||
169 | |||
170 | /** |
||
171 | * Returns the HATEOAS structure, if any. |
||
172 | * |
||
173 | * @return null|Links |
||
174 | */ |
||
175 | public function getLinks() |
||
182 | |||
183 | /** |
||
184 | * Find by path. |
||
185 | * |
||
186 | * @param string $path |
||
187 | * |
||
188 | * @return stdClass |
||
189 | */ |
||
190 | public function getByPath($path) |
||
196 | |||
197 | /** |
||
198 | * Create a new item in Moip. |
||
199 | * |
||
200 | * @param string $path |
||
201 | * |
||
202 | * @return stdClass |
||
203 | */ |
||
204 | public function createResource($path) |
||
210 | } |
||
211 |