1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Igorsgm\Ghost\Apis; |
4
|
|
|
|
5
|
|
|
use Igorsgm\Ghost\Models\Resources\BaseResource; |
6
|
|
|
use Igorsgm\Ghost\Responses\ErrorResponse; |
7
|
|
|
use Igorsgm\Ghost\Responses\SuccessResponse; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
|
10
|
|
|
abstract class BaseApi |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var string |
14
|
|
|
*/ |
15
|
|
|
protected $baseUrl; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var BaseResource |
19
|
|
|
*/ |
20
|
|
|
protected $resource; |
21
|
|
|
|
22
|
|
|
public string $resourceId = ''; |
23
|
|
|
|
24
|
|
|
public string $resourceSlug = ''; |
25
|
|
|
|
26
|
|
|
public string $include = ''; |
27
|
|
|
|
28
|
|
|
public string $fields = ''; |
29
|
|
|
|
30
|
|
|
public string $formats = ''; |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @var string |
34
|
|
|
*/ |
35
|
|
|
protected $source = ''; |
36
|
|
|
|
37
|
|
|
public string $limit = ''; |
38
|
|
|
|
39
|
|
|
public string $filter = ''; |
40
|
|
|
|
41
|
|
|
public string $page = ''; |
42
|
|
|
|
43
|
|
|
public string $order = ''; |
44
|
|
|
|
45
|
|
|
protected string $key; |
46
|
|
|
|
47
|
|
|
protected string $domain; |
48
|
|
|
|
49
|
|
|
protected string $version; |
50
|
|
|
|
51
|
105 |
|
protected function buildEndpoint(): string |
52
|
|
|
{ |
53
|
105 |
|
$endpoint = $this->resource->getResourceName(); |
54
|
|
|
|
55
|
105 |
|
if (! empty($this->resourceId)) { |
56
|
28 |
|
$endpoint .= "/$this->resourceId"; |
57
|
78 |
|
} elseif (! empty($this->resourceSlug)) { |
58
|
10 |
|
$endpoint .= "/slug/$this->resourceSlug"; |
59
|
|
|
} |
60
|
|
|
|
61
|
105 |
|
return $endpoint; |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param string $suffix |
66
|
|
|
*/ |
67
|
104 |
|
protected function makeApiUrl($suffix = ''): string |
68
|
|
|
{ |
69
|
104 |
|
return sprintf('%s/%s/?%s', $this->baseUrl, $this->buildEndpoint().$suffix, $this->buildParams()); |
70
|
|
|
} |
71
|
|
|
|
72
|
114 |
|
protected function buildParams(): string |
73
|
|
|
{ |
74
|
114 |
|
$params = ['include', 'fields', 'formats', 'source', 'filter', 'limit', 'page', 'order', 'key']; |
75
|
|
|
|
76
|
114 |
|
$queryParams = []; |
77
|
114 |
|
foreach ($params as $param) { |
78
|
114 |
|
$queryParams[$param] = $this->{$param} ?: null; |
79
|
|
|
} |
80
|
|
|
|
81
|
114 |
|
return http_build_query($queryParams); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @return ErrorResponse|SuccessResponse |
86
|
|
|
*/ |
87
|
102 |
|
protected function handleResponse($response) |
88
|
|
|
{ |
89
|
102 |
|
if ($response->failed()) { |
90
|
4 |
|
return new ErrorResponse($response); |
91
|
|
|
} |
92
|
|
|
|
93
|
98 |
|
return new SuccessResponse($this, $this->resource, $response); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
/** |
97
|
|
|
* Return resource from slug |
98
|
|
|
* |
99
|
|
|
* |
100
|
|
|
* @return array|ErrorResponse|mixed |
101
|
|
|
*/ |
102
|
9 |
|
public function fromSlug(string $slug) |
103
|
|
|
{ |
104
|
9 |
|
$this->resourceSlug = $slug; |
105
|
9 |
|
$response = $this->get(); |
|
|
|
|
106
|
|
|
|
107
|
9 |
|
if ($response instanceof ErrorResponse) { |
108
|
1 |
|
return $response; |
109
|
|
|
} |
110
|
|
|
|
111
|
8 |
|
return data_get($response->data, 0, []); |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
/** |
115
|
|
|
* @return Collection|array[] |
116
|
|
|
*/ |
117
|
15 |
|
public function all() |
118
|
|
|
{ |
119
|
15 |
|
return $this->limit('all')->get(); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* @return BaseResource|ErrorResponse |
124
|
|
|
*/ |
125
|
5 |
|
public function first() |
126
|
|
|
{ |
127
|
5 |
|
$response = $this->limit(1)->get(); |
128
|
|
|
|
129
|
5 |
|
return $response->data->first(); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* @return array |
134
|
|
|
*/ |
135
|
22 |
|
public function paginate($limit = null) |
136
|
|
|
{ |
137
|
22 |
|
if (isset($limit)) { |
138
|
22 |
|
$this->limit = $limit; |
139
|
|
|
} |
140
|
|
|
|
141
|
22 |
|
return $this->get(); |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Return resource from ID |
146
|
|
|
* |
147
|
|
|
* |
148
|
|
|
* @return BaseResource|ErrorResponse |
149
|
|
|
*/ |
150
|
17 |
|
public function find(string $id) |
151
|
|
|
{ |
152
|
17 |
|
$this->resourceId = $id; |
153
|
17 |
|
$response = $this->get(); |
154
|
|
|
|
155
|
17 |
|
if ($response instanceof ErrorResponse) { |
156
|
3 |
|
return $response; |
157
|
|
|
} |
158
|
|
|
|
159
|
14 |
|
return data_get($response->data, 0, []); |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Apply fine-grained filters to target specific data. |
164
|
|
|
* |
165
|
|
|
* @param string $filter |
166
|
|
|
* @return $this |
167
|
|
|
* |
168
|
|
|
* @see https://ghost.org/docs/content-api/#filtering |
169
|
|
|
* @see https://gist.github.com/ErisDS/f516a859355d515aa6ad |
170
|
|
|
*/ |
171
|
1 |
|
public function filter($filter): BaseApi |
172
|
|
|
{ |
173
|
1 |
|
$this->filter = $filter; |
174
|
|
|
|
175
|
1 |
|
return $this; |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Limit how many records are returned at once |
180
|
|
|
* |
181
|
|
|
* @param int|string $limit |
182
|
|
|
* @return $this |
183
|
|
|
* |
184
|
|
|
* @see https://ghost.org/docs/content-api/#limit |
185
|
|
|
*/ |
186
|
34 |
|
public function limit($limit): BaseApi |
187
|
|
|
{ |
188
|
34 |
|
$this->limit = strval($limit); |
189
|
|
|
|
190
|
34 |
|
return $this; |
191
|
|
|
} |
192
|
|
|
|
193
|
|
|
/** |
194
|
|
|
* @return $this |
195
|
|
|
*/ |
196
|
123 |
|
public function setResource(string $resourceClass): BaseApi |
197
|
|
|
{ |
198
|
123 |
|
$this->resource = resolve($resourceClass); |
|
|
|
|
199
|
|
|
|
200
|
123 |
|
return $this; |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @return BaseResource |
205
|
|
|
*/ |
206
|
19 |
|
public function getResource() |
207
|
|
|
{ |
208
|
19 |
|
return $this->resource; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Alias for Ghost's include. |
213
|
|
|
* |
214
|
|
|
* The following includes are available: |
215
|
|
|
* Posts & Pages: authors, tags |
216
|
|
|
* Authors: count.posts |
217
|
|
|
* Tags: count.posts |
218
|
|
|
* Tiers: monthly_price, yearly_price, benefits |
219
|
|
|
* |
220
|
|
|
* @param string|array ...$includes |
221
|
|
|
* @return $this |
222
|
|
|
* |
223
|
|
|
* @see https://ghost.org/docs/content-api/#include |
224
|
|
|
*/ |
225
|
11 |
|
public function include(...$includes): BaseApi |
226
|
|
|
{ |
227
|
11 |
|
$this->include = collect($includes)->flatten()->implode(','); |
228
|
|
|
|
229
|
11 |
|
return $this; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Alias for include method |
234
|
|
|
* |
235
|
|
|
* @return $this |
236
|
|
|
*/ |
237
|
2 |
|
public function with(...$includes): BaseApi |
238
|
|
|
{ |
239
|
2 |
|
return $this->include(...$includes); |
240
|
|
|
} |
241
|
|
|
|
242
|
|
|
/** |
243
|
|
|
* Limit the fields returned to the response object |
244
|
|
|
* |
245
|
|
|
* @param string|array ...$fields |
246
|
|
|
* @return $this |
247
|
|
|
* |
248
|
|
|
* @see https://ghost.org/docs/content-api/#fields |
249
|
|
|
*/ |
250
|
3 |
|
public function fields(...$fields): BaseApi |
251
|
|
|
{ |
252
|
3 |
|
$this->fields = collect($fields)->flatten()->implode(','); |
253
|
|
|
|
254
|
3 |
|
return $this; |
255
|
|
|
} |
256
|
|
|
|
257
|
|
|
/** |
258
|
|
|
* Optionally request different format for posts and pages |
259
|
|
|
* By default, Admin API expects and returns content in the mobiledoc format only. |
260
|
|
|
* To include html in the response use this parameter. |
261
|
|
|
* |
262
|
|
|
* Possible formats: html, plaintext, mobiledoc. |
263
|
|
|
* |
264
|
|
|
* |
265
|
|
|
* @return $this |
266
|
|
|
* |
267
|
|
|
* @see https://ghost.org/docs/content-api/#formats |
268
|
|
|
*/ |
269
|
2 |
|
public function formats(string $format): BaseApi |
270
|
|
|
{ |
271
|
2 |
|
$this->formats = $format; |
272
|
|
|
|
273
|
2 |
|
return $this; |
274
|
|
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* @return $this |
278
|
|
|
* |
279
|
|
|
* @see https://ghost.org/docs/content-api/#page |
280
|
|
|
*/ |
281
|
14 |
|
public function page(int $page): BaseApi |
282
|
|
|
{ |
283
|
14 |
|
$this->page = strval($page); |
284
|
|
|
|
285
|
14 |
|
return $this; |
286
|
|
|
} |
287
|
|
|
|
288
|
|
|
/** |
289
|
|
|
* @return $this |
290
|
|
|
* |
291
|
|
|
* @see https://ghost.org/docs/content-api/#order |
292
|
|
|
*/ |
293
|
3 |
|
public function order(string $attr, string $order = 'DESC'): BaseApi |
294
|
|
|
{ |
295
|
3 |
|
$this->order = $attr.' '.strtolower($order); |
296
|
|
|
|
297
|
3 |
|
return $this; |
298
|
|
|
} |
299
|
|
|
|
300
|
|
|
/** |
301
|
|
|
* Alias for order method |
302
|
|
|
* |
303
|
|
|
* @return $this |
304
|
|
|
*/ |
305
|
1 |
|
public function orderBy(string $attr, string $order = 'DESC'): BaseApi |
306
|
|
|
{ |
307
|
1 |
|
return $this->order($attr, $order); |
308
|
|
|
} |
309
|
|
|
} |
310
|
|
|
|