1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Vindi; |
4
|
|
|
|
5
|
|
|
abstract class Resource |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* @var \Vindi\ApiRequester |
9
|
|
|
*/ |
10
|
|
|
public $apiRequester; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Resource constructor. |
14
|
|
|
* |
15
|
|
|
* @param array $arguments |
16
|
|
|
*/ |
17
|
416 |
|
public function __construct($arguments = []) |
18
|
|
|
{ |
19
|
416 |
|
if (key_exists(Vindi::VINDI_API_KEY, $arguments)) { |
20
|
|
|
Vindi::setApiKey($arguments[Vindi::VINDI_API_KEY]); |
21
|
|
|
} |
22
|
|
|
|
23
|
416 |
|
if (key_exists(Vindi::VINDI_API_URI, $arguments)) { |
24
|
|
|
Vindi::setApiUri($arguments[Vindi::VINDI_API_URI]); |
25
|
|
|
} |
26
|
416 |
|
$this->apiRequester = new ApiRequester; |
27
|
416 |
|
} |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* The endpoint at default state that will hit the API. |
31
|
|
|
* |
32
|
|
|
* @return string |
33
|
|
|
*/ |
34
|
|
|
abstract public function endpoint(); |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Build url that will hit the API. |
38
|
|
|
* |
39
|
|
|
* @param int $id The resource's id. |
40
|
|
|
* @param string $additionalEndpoint Additional endpoint that will be appended to the URL. |
41
|
|
|
* |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
296 |
|
public function url($id = null, $additionalEndpoint = null) |
45
|
|
|
{ |
46
|
296 |
|
$endpoint = $this->endpoint(); |
47
|
|
|
|
48
|
296 |
|
if (! is_null($id)) { |
49
|
216 |
|
$endpoint .= '/' . $id; |
50
|
216 |
|
} |
51
|
296 |
|
if (! is_null($additionalEndpoint)) { |
52
|
96 |
|
$endpoint .= '/' . $additionalEndpoint; |
53
|
96 |
|
} |
54
|
|
|
|
55
|
296 |
|
return $endpoint; |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Retrieve all resources. |
60
|
|
|
* |
61
|
|
|
* @param array $params Pagination and Filter parameters. |
62
|
|
|
* |
63
|
|
|
* @return mixed |
64
|
|
|
*/ |
65
|
40 |
|
public function all(array $params = []) |
66
|
|
|
{ |
67
|
40 |
|
return $this->apiRequester->request('GET', $this->url(), ['query' => $params]); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* Create a new resource. |
72
|
|
|
* |
73
|
|
|
* @param array $form_params The request body. |
74
|
|
|
* |
75
|
|
|
* @return mixed |
76
|
|
|
*/ |
77
|
40 |
|
public function create(array $form_params = []) |
78
|
|
|
{ |
79
|
40 |
|
return $this->apiRequester->request('POST', $this->url(), ['json' => $form_params]); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Retrieve a specific resource. |
84
|
|
|
* |
85
|
|
|
* @param int $id The resource's id. |
86
|
|
|
* |
87
|
|
|
* @return mixed |
88
|
|
|
*/ |
89
|
40 |
|
public function retrieve($id = null) |
90
|
|
|
{ |
91
|
40 |
|
return $this->apiRequester->request('GET', $this->url($id)); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Update a specific resource. |
96
|
|
|
* |
97
|
|
|
* @param int $id The resource's id. |
98
|
|
|
* @param array $form_params The request body. |
99
|
|
|
* |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
40 |
|
public function update($id = null, array $form_params = []) |
103
|
|
|
{ |
104
|
40 |
|
return $this->apiRequester->request('PUT', $this->url($id), ['json' => $form_params]); |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Delete a specific resource. |
109
|
|
|
* |
110
|
|
|
* @param int $id The resource's id. |
111
|
|
|
* @param array $form_params The request body. |
112
|
|
|
* |
113
|
|
|
* @return mixed |
114
|
|
|
*/ |
115
|
40 |
|
public function delete($id = null, array $form_params = []) |
116
|
|
|
{ |
117
|
40 |
|
return $this->apiRequester->request('DELETE', $this->url($id), ['json' => $form_params]); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* Make a GET request to an additional endpoint for a specific resource. |
122
|
|
|
* |
123
|
|
|
* @param int $id The resource's id. |
124
|
|
|
* @param string $additionalEndpoint Additional endpoint that will be appended to the URL. |
125
|
|
|
* |
126
|
|
|
* @return mixed |
127
|
|
|
*/ |
128
|
42 |
|
public function get($id = null, $additionalEndpoint = null) |
129
|
|
|
{ |
130
|
42 |
|
return $this->apiRequester->request('GET', $this->url($id, $additionalEndpoint)); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Make a POST request to an additional endpoint for a specific resource. |
135
|
|
|
* |
136
|
|
|
* @param int $id The resource's id. |
137
|
|
|
* @param string $additionalEndpoint Additional endpoint that will be appended to the URL. |
138
|
|
|
* @param array $form_params The request body. |
139
|
|
|
* |
140
|
|
|
* @return mixed |
141
|
|
|
*/ |
142
|
54 |
|
public function post($id = null, $additionalEndpoint = null, array $form_params = []) |
143
|
|
|
{ |
144
|
54 |
|
return $this->apiRequester->request('POST', $this->url($id, $additionalEndpoint), ['json' => $form_params]); |
145
|
|
|
} |
146
|
|
|
|
147
|
|
|
/** |
148
|
|
|
* Return the last response from a preview request |
149
|
|
|
* |
150
|
|
|
* @return mixed |
151
|
|
|
*/ |
152
|
80 |
|
public function getLastResponse() |
153
|
|
|
{ |
154
|
80 |
|
return $this->apiRequester->lastResponse; |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|