1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Moip\Resource; |
4
|
|
|
|
5
|
|
|
use JsonSerializable; |
6
|
|
|
use Moip\Http\HTTPConnection; |
7
|
|
|
use Moip\Http\HTTPRequest; |
8
|
|
|
use Moip\Moip; |
9
|
|
|
use RuntimeException; |
10
|
|
|
use stdClass; |
11
|
|
|
|
12
|
|
|
abstract class MoipResource implements JsonSerializable { |
13
|
|
|
/** |
14
|
|
|
* Version of API. |
15
|
|
|
* |
16
|
|
|
* @const string |
17
|
|
|
*/ |
18
|
|
|
const VERSION = 'v2'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var \Moip\Moip |
22
|
|
|
*/ |
23
|
|
|
protected $moip; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @var \stdClass |
27
|
|
|
*/ |
28
|
|
|
protected $data; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Initialize a new instance. |
32
|
|
|
*/ |
33
|
|
|
abstract protected function initialize(); |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* Mount information of a determined object. |
37
|
|
|
* |
38
|
|
|
* @param \stdClass $response |
39
|
|
|
* |
40
|
|
|
* @return mixed |
41
|
|
|
*/ |
42
|
|
|
abstract protected function populate(stdClass $response); |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Create a new instance. |
46
|
|
|
* |
47
|
|
|
* @param \Moip\Moip $moip |
48
|
|
|
*/ |
49
|
|
|
public function __construct(Moip $moip) { |
50
|
|
|
$this->moip = $moip; |
51
|
|
|
$this->data = new stdClass(); |
52
|
|
|
$this->initialize(); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Create a new connecttion. |
57
|
|
|
* |
58
|
|
|
* @return \Moip\Http\HTTPConnection |
59
|
|
|
*/ |
60
|
|
|
protected function createConnection() { |
61
|
|
|
return $this->moip->createConnection(new HTTPConnection()); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get a key of an object if he exist. |
66
|
|
|
* |
67
|
|
|
* @param string $key |
68
|
|
|
* @param \stdClass|null $data |
69
|
|
|
* |
70
|
|
|
* @return mixed |
71
|
|
|
*/ |
72
|
|
|
protected function getIfSet($key, stdClass $data = null) { |
73
|
|
|
|
74
|
|
|
if(empty($data)){ |
75
|
|
|
$data = $this->data; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
if (isset($data->$key)) { |
79
|
|
|
return $data->$key; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
return null; |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
protected function getIfSetDateFmt($key, $fmt, stdClass $data=null){ |
86
|
|
|
$val = $this->getIfSet($key, $data); |
87
|
|
|
if (!empty($val)) { |
88
|
|
|
$dt = \DateTime::createFromFormat($fmt, $val); |
89
|
|
|
return $dt ? $dt : null; |
90
|
|
|
} |
91
|
|
|
return null; |
92
|
|
|
|
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Get a key, representing a date (Y-m-d), of an object if it exists. |
97
|
|
|
* @param string $key |
98
|
|
|
* @param stdClass|null $data |
99
|
|
|
* @return \DateTime|null |
100
|
|
|
*/ |
101
|
|
|
protected function getIfSetDate($key, stdClass $data = null) { |
102
|
|
|
|
103
|
|
|
return $this->getIfSetDateFmt($key, 'Y-m-d', $data); |
104
|
|
|
|
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Get a key representing a datetime (\Datetime::ATOM), of an object if it exists. |
109
|
|
|
* @param string $key |
110
|
|
|
* @param stdClass|null $data |
111
|
|
|
* @return \DateTime|null |
112
|
|
|
*/ |
113
|
|
|
|
114
|
|
|
protected function getIfSetDateTime($key, stdClass $data = null) { |
115
|
|
|
return $this->getIfSetDateFmt($key, \DateTime::ATOM, $data); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Specify data which should be serialized to JSON. |
120
|
|
|
* |
121
|
|
|
* @return \stdClass |
122
|
|
|
*/ |
123
|
|
|
public function jsonSerialize() { |
124
|
|
|
return $this->data; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* Find by path. |
129
|
|
|
* |
130
|
|
|
* @param string $path |
131
|
|
|
* |
132
|
|
|
* @return stdClass |
133
|
|
|
*/ |
134
|
|
|
public function getByPath($path) { |
135
|
|
|
$httpConnection = $this->createConnection(); |
136
|
|
|
$httpConnection->addHeader('Content-Type', 'application/json'); |
137
|
|
|
|
138
|
|
|
$httpResponse = $httpConnection->execute($path, HTTPRequest::GET); |
139
|
|
|
|
140
|
|
|
if ($httpResponse->getStatusCode() != 200) { |
141
|
|
|
throw new RuntimeException($httpResponse->getStatusMessage(), $httpResponse->getStatusCode()); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
return $this->populate(json_decode($httpResponse->getContent())); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Create a new item in Moip. |
149
|
|
|
* |
150
|
|
|
* @param string $path |
151
|
|
|
* |
152
|
|
|
* @return stdClass |
153
|
|
|
*/ |
154
|
|
|
public function createResource($path) { |
155
|
|
|
$body = json_encode($this, JSON_UNESCAPED_SLASHES); |
156
|
|
|
|
157
|
|
|
$httpConnection = $this->createConnection(); |
158
|
|
|
$httpConnection->addHeader('Content-Type', 'application/json'); |
159
|
|
|
$httpConnection->addHeader('Content-Length', strlen($body)); |
160
|
|
|
$httpConnection->setRequestBody($body); |
161
|
|
|
|
162
|
|
|
$httpResponse = $httpConnection->execute($path, HTTPRequest::POST); |
163
|
|
|
|
164
|
|
|
if ($httpResponse->getStatusCode() != 201) { |
165
|
|
|
throw new RuntimeException($httpResponse->getStatusMessage(), $httpResponse->getStatusCode()); |
166
|
|
|
} |
167
|
|
|
|
168
|
|
|
return $this->populate(json_decode($httpResponse->getContent())); |
169
|
|
|
} |
170
|
|
|
} |
171
|
|
|
|