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 | * Create a new connecttion. |
||
59 | * |
||
60 | * @return \Moip\Http\HTTPConnection |
||
61 | */ |
||
62 | protected function createConnection() |
||
66 | |||
67 | /** |
||
68 | * Get a key of an object if it exists. |
||
69 | * |
||
70 | * @param string $key |
||
71 | * @param \stdClass|null $data |
||
72 | * |
||
73 | * @return mixed |
||
74 | */ |
||
75 | protected function getIfSet($key, stdClass $data = null) |
||
87 | |||
88 | protected function getIfSetDateFmt($key, $fmt, stdClass $data = null) |
||
99 | |||
100 | /** |
||
101 | * Get a key, representing a date (Y-m-d), 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 getIfSetDate($key, stdClass $data = null) |
||
112 | |||
113 | /** |
||
114 | * Get a key representing a datetime (\Datetime::ATOM), of an object if it exists. |
||
115 | * |
||
116 | * @param string $key |
||
117 | * @param stdClass|null $data |
||
118 | * |
||
119 | * @return \DateTime|null |
||
120 | */ |
||
121 | protected function getIfSetDateTime($key, stdClass $data = null) |
||
125 | |||
126 | /** |
||
127 | * Specify data which should be serialized to JSON. |
||
128 | * |
||
129 | * @return \stdClass |
||
130 | */ |
||
131 | public function jsonSerialize() |
||
135 | |||
136 | /** |
||
137 | * Execute a http request. If payload == null no body will be sent. Empty body ('{}') is supported by sending a |
||
138 | * empty stdClass. |
||
139 | * |
||
140 | * @param $path |
||
141 | * @param $method |
||
142 | * @param mixed|null $payload |
||
143 | * |
||
144 | * @throws Exceptions\ValidationException if the API returns a 4xx http status code. Usually means invalid data was sent. |
||
145 | * @throws Exceptions\UnautorizedException if the API returns a 401 http status code. Check API token and key |
||
146 | * @throws Exceptions\UnexpectedException if the API returns a 500 http status code, this is not suppose to happen. Please report the error to moip |
||
147 | * |
||
148 | * @return stdClass |
||
149 | */ |
||
150 | protected function httpRequest($path, $method, $payload = null) |
||
181 | |||
182 | /** |
||
183 | * Find by path. |
||
184 | * |
||
185 | * @param string $path |
||
186 | * |
||
187 | * @return stdClass |
||
188 | */ |
||
189 | public function getByPath($path) |
||
195 | |||
196 | /** |
||
197 | * Create a new item in Moip. |
||
198 | * |
||
199 | * @param string $path |
||
200 | * |
||
201 | * @return stdClass |
||
202 | */ |
||
203 | public function createResource($path) |
||
209 | } |
||
210 |