1 | <?php |
||
24 | class Builder |
||
25 | { |
||
26 | /** |
||
27 | * @var Restful |
||
28 | */ |
||
29 | protected $model; |
||
30 | |||
31 | /** |
||
32 | * @var Client |
||
33 | */ |
||
34 | protected $client; |
||
35 | |||
36 | /** |
||
37 | * @var Grammar |
||
38 | */ |
||
39 | protected $grammar; |
||
40 | |||
41 | /** |
||
42 | * @var mixed[] |
||
43 | */ |
||
44 | protected $wheres = []; |
||
45 | |||
46 | /** |
||
47 | * @var string[] |
||
48 | */ |
||
49 | private $order; |
||
50 | |||
51 | /** |
||
52 | * @var int |
||
53 | */ |
||
54 | private $limit; |
||
55 | |||
56 | /** |
||
57 | * @var int[] |
||
58 | */ |
||
59 | private $goodStatusCodes = [ |
||
60 | Response::HTTP_OK, |
||
61 | Response::HTTP_CREATED, |
||
62 | Response::HTTP_ACCEPTED |
||
63 | ]; |
||
64 | /** |
||
65 | * @var LoggerInterface |
||
66 | */ |
||
67 | private $logger; |
||
68 | |||
69 | /** |
||
70 | * Builder constructor. |
||
71 | * @param Client $client |
||
72 | * @param LoggerInterface $logger |
||
73 | * @param Restful $model |
||
74 | */ |
||
75 | public function __construct(Client $client, LoggerInterface $logger, Restful $model) |
||
81 | |||
82 | /** |
||
83 | * What model are we querying? |
||
84 | * @param string $class |
||
85 | * @return Builder |
||
86 | */ |
||
87 | public static function model(string $class) |
||
92 | |||
93 | /** |
||
94 | * Returns a new instance of the model |
||
95 | * @return Model |
||
96 | */ |
||
97 | private function newModelInstance(): Model |
||
101 | |||
102 | /** |
||
103 | * Get the index route for the model via the options route. |
||
104 | * @param string $route |
||
105 | * @param null $id |
||
106 | * @return Route |
||
107 | * @throws Exceptions\UndefinedIndexException |
||
108 | * @throws RouteNotFoundException |
||
109 | */ |
||
110 | private function getModelRoute(string $route, $id = null): Route |
||
125 | |||
126 | /** |
||
127 | * Get the JSON from the given URL. |
||
128 | * @param Route $route |
||
129 | * @param array $queryString |
||
130 | * @param array $postData |
||
131 | * @return MessageInterface |
||
132 | * @throws Exceptions\UndefinedIndexException |
||
133 | */ |
||
134 | private function getResponse(Route $route, array $queryString = [], array $postData = []): ?MessageInterface |
||
165 | |||
166 | /** |
||
167 | * Extract JSON from a Response. |
||
168 | * @param MessageInterface $response |
||
169 | * @return mixed |
||
170 | * @throws Exceptions\UndefinedIndexException |
||
171 | */ |
||
172 | private function parseJsonFromResponse(MessageInterface $response) |
||
198 | |||
199 | /** |
||
200 | * Is the JSON responses a paginatable one? |
||
201 | * @param mixed[] $json |
||
202 | * @return bool |
||
203 | */ |
||
204 | private function isPaginatedResponse(array $json): bool |
||
208 | |||
209 | /** |
||
210 | * Extract data from the JSON |
||
211 | * @param $json |
||
212 | * @return array |
||
213 | * @throws InvalidArgumentException |
||
214 | */ |
||
215 | private function extractDataFromJson($json): array |
||
223 | |||
224 | /** |
||
225 | * Add a where clause. |
||
226 | * @param string $column |
||
227 | * @param $value |
||
228 | * @return Builder |
||
229 | */ |
||
230 | public function where(string $column, $value): self |
||
240 | |||
241 | /** |
||
242 | * Add a whereIn clause. |
||
243 | * @param string $column |
||
244 | * @param mixed[] $values |
||
245 | * @return Builder |
||
246 | */ |
||
247 | public function whereIn(string $column, array $values): self |
||
257 | |||
258 | /** |
||
259 | * Return all the wheres |
||
260 | * @return mixed[] |
||
261 | */ |
||
262 | public function getWheres(): array |
||
266 | |||
267 | /** |
||
268 | * Set an order |
||
269 | * @param string $column |
||
270 | * @param string $direction |
||
271 | * @return $this\ |
||
272 | */ |
||
273 | public function order(string $column, string $direction): self |
||
282 | |||
283 | /** |
||
284 | * Get the order |
||
285 | * @return array|null |
||
286 | */ |
||
287 | public function getOrder(): ?array |
||
291 | |||
292 | /** |
||
293 | * Specify a limit |
||
294 | * @param int $limit |
||
295 | * @return $this |
||
296 | */ |
||
297 | public function limit(int $limit): self |
||
303 | |||
304 | /** |
||
305 | * Get the limit |
||
306 | * @return int|null |
||
307 | */ |
||
308 | public function getLimit(): ?int |
||
312 | |||
313 | /** |
||
314 | * Call the API, and return the models. |
||
315 | * @return Collection |
||
316 | * @throws Exceptions\UndefinedIndexException |
||
317 | */ |
||
318 | public function get(): Collection |
||
350 | |||
351 | /** |
||
352 | * @param string $route |
||
353 | * @param mixed[] $data |
||
354 | * @param mixed $id |
||
355 | * @return bool |
||
356 | * @throws Exceptions\UndefinedIndexException |
||
357 | */ |
||
358 | private function _call(string $route, array $data, $id = null): bool |
||
368 | |||
369 | /** |
||
370 | * @param mixed[] $data |
||
371 | * @return bool |
||
372 | * @throws Exceptions\UndefinedIndexException |
||
373 | */ |
||
374 | public function insert(array $data): bool |
||
378 | |||
379 | /** |
||
380 | * @param mixed $id |
||
381 | * @param mixed[] $data |
||
382 | * @return bool |
||
383 | * @throws Exceptions\UndefinedIndexException |
||
384 | */ |
||
385 | public function update($id, array $data): bool |
||
389 | |||
390 | /** |
||
391 | * @param mixed $id |
||
392 | * @return bool |
||
393 | * @throws Exceptions\UndefinedIndexException |
||
394 | */ |
||
395 | public function delete($id): bool |
||
399 | |||
400 | /** |
||
401 | * Should we log requests? |
||
402 | * @return bool |
||
403 | */ |
||
404 | private function shouldLog(): bool |
||
408 | } |
||
409 |