9776da9b696f7c719b05b676c970c69e092e5fc0
[m6w6/seekat] / lib / functions.php
1 <?php
2
3 namespace seekat;
4
5 /**
6 * Generate a human readable represenation of a variable
7 * @param mixed $arg
8 * @param bool $export whether to var_export the $arg
9 * @return string
10 */
11 function typeof($arg, $export = false) {
12 $type = (is_object($arg)
13 ? "instance of ".get_class($arg)
14 : gettype($arg)
15 );
16 if ($export) {
17 $type .= ": ".var_export($arg, true);
18 }
19 return $type;
20 }
21
22 namespace seekat\Exception;
23
24 /**
25 * Canonicalize an error message from a string or Exception
26 * @param string|Exception $error
27 * @return string
28 */
29 function message(&$error) : string {
30 if ($error instanceof \Throwable) {
31 $message = $error->getMessage();
32 } else {
33 $message = $error;
34 $error = new \Exception($error);
35 }
36 return $message;
37 }
38
39 namespace seekat\API\Links;
40
41 use React\Promise\{
42 ExtendedPromiseInterface,
43 function reject
44 };
45 use seekat\API;
46 use seekat\API\Call\Cache;
47
48 /**
49 * Perform a GET request against the link's "first" relation
50 *
51 * @return ExtendedPromiseInterface
52 */
53 function first(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface {
54 $links = $api->getLinks();
55 if ($links && ($first = $links->getFirst())) {
56 return $api->withUrl($first)->get(null, null, $cache);
57 }
58 return reject($links);
59 }
60
61 /**
62 * Perform a GET request against the link's "prev" relation
63 *
64 * @return ExtendedPromiseInterface
65 */
66 function prev(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface {
67 $links = $api->getLinks();
68 if ($links && ($prev = $links->getPrev())) {
69 return $api->withUrl($prev)->get(null, null, $cache);
70 }
71 return reject($links);
72 }
73
74 /**
75 * Perform a GET request against the link's "next" relation
76 *
77 * @return ExtendedPromiseInterface
78 */
79 function next(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface {
80 $links = $api->getLinks();
81 if ($links && ($next = $links->getNext())) {
82 return $api->withUrl($next)->get(null, null, $cache);
83 }
84 return reject($links);
85 }
86
87 /**
88 * Perform a GET request against the link's "last" relation
89 *
90 * @return ExtendedPromiseInterface
91 */
92 function last(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface {
93 $links = $api->getLinks();
94 if ($links && ($last = $links->getLast())) {
95 return $api->withUrl($last)->get(null, null, $cache);
96 }
97 return reject($links);
98 }
99