9e82d7200aef3ee44f6ca068bb29e6135f873874
[m6w6/seekat] / lib / functions.php
1 <?php
2
3 namespace seekat\Exception;
4
5 function message(&$error) : string {
6 if ($error instanceof \Throwable) {
7 $message = $error->getMessage();
8 } else {
9 $message = $error;
10 $error = new \Exception($error);
11 }
12 return $message;
13 }
14
15 namespace seekat\API\Links;
16
17 use React\Promise\{
18 ExtendedPromiseInterface,
19 function reject
20 };
21 use seekat\API;
22 use seekat\API\Call\Cache;
23
24 /**
25 * Perform a GET request against the link's "first" relation
26 *
27 * @return ExtendedPromiseInterface
28 */
29 function first(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface {
30 $links = $api->getLinks();
31 if ($links && ($first = $links->getFirst())) {
32 return $api->withUrl($first)->get(null, null, $cache);
33 }
34 return reject($links);
35 }
36
37 /**
38 * Perform a GET request against the link's "prev" relation
39 *
40 * @return ExtendedPromiseInterface
41 */
42 function prev(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface {
43 $links = $api->getLinks();
44 if ($links && ($prev = $links->getPrev())) {
45 return $api->withUrl($prev)->get(null, null, $cache);
46 }
47 return reject($links);
48 }
49
50 /**
51 * Perform a GET request against the link's "next" relation
52 *
53 * @return ExtendedPromiseInterface
54 */
55 function next(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface {
56 $links = $api->getLinks();
57 if ($links && ($next = $links->getNext())) {
58 return $api->withUrl($next)->get(null, null, $cache);
59 }
60 return reject($links);
61 }
62
63 /**
64 * Perform a GET request against the link's "last" relation
65 *
66 * @return ExtendedPromiseInterface
67 */
68 function last(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface {
69 $links = $api->getLinks();
70 if ($links && ($last = $links->getLast())) {
71 return $api->withUrl($last)->get(null, null, $cache);
72 }
73 return reject($links);
74 }
75