X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Ffunctions.php;fp=lib%2Ffunctions.php;h=9e82d7200aef3ee44f6ca068bb29e6135f873874;hb=2451d97f1cb7b97e445b4dd839835b8673a4d0fc;hp=0000000000000000000000000000000000000000;hpb=3958595e9ff27162ae918db1453ddecd4840d481;p=m6w6%2Fseekat diff --git a/lib/functions.php b/lib/functions.php new file mode 100644 index 0000000..9e82d72 --- /dev/null +++ b/lib/functions.php @@ -0,0 +1,75 @@ +getMessage(); + } else { + $message = $error; + $error = new \Exception($error); + } + return $message; +} + +namespace seekat\API\Links; + +use React\Promise\{ + ExtendedPromiseInterface, + function reject +}; +use seekat\API; +use seekat\API\Call\Cache; + +/** + * Perform a GET request against the link's "first" relation + * + * @return ExtendedPromiseInterface + */ +function first(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface { + $links = $api->getLinks(); + if ($links && ($first = $links->getFirst())) { + return $api->withUrl($first)->get(null, null, $cache); + } + return reject($links); +} + +/** + * Perform a GET request against the link's "prev" relation + * + * @return ExtendedPromiseInterface + */ +function prev(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface { + $links = $api->getLinks(); + if ($links && ($prev = $links->getPrev())) { + return $api->withUrl($prev)->get(null, null, $cache); + } + return reject($links); +} + +/** + * Perform a GET request against the link's "next" relation + * + * @return ExtendedPromiseInterface + */ +function next(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface { + $links = $api->getLinks(); + if ($links && ($next = $links->getNext())) { + return $api->withUrl($next)->get(null, null, $cache); + } + return reject($links); +} + +/** + * Perform a GET request against the link's "last" relation + * + * @return ExtendedPromiseInterface + */ +function last(API $api, Cache\Service $cache = null) : ExtendedPromiseInterface { + $links = $api->getLinks(); + if ($links && ($last = $links->getLast())) { + return $api->withUrl($last)->get(null, null, $cache); + } + return reject($links); +} +