basic async-interop support; generator consumer missing
[m6w6/seekat] / lib / API / Links / functions.php
1 <?php
2
3 namespace seekat\API\Links;
4
5 use AsyncInterop\Promise;
6 use seekat\API;
7 use seekat\API\Call\Cache;
8 use seekat\API\Future;
9
10 /**
11 * Perform a GET request against the link's "first" relation
12 *
13 * @return Promise
14 */
15 function first(API $api, Cache\Service $cache = null) : Promise {
16 $links = $api->getLinks();
17 if ($links && ($first = $links->getFirst())) {
18 return $api->withUrl($first)->get(null, null, $cache);
19 }
20 return Future\reject($api->getFuture(), $links);
21 }
22
23 /**
24 * Perform a GET request against the link's "prev" relation
25 *
26 * @return Promise
27 */
28 function prev(API $api, Cache\Service $cache = null) : Promise {
29 $links = $api->getLinks();
30 if ($links && ($prev = $links->getPrev())) {
31 return $api->withUrl($prev)->get(null, null, $cache);
32 }
33 return Future\reject($api->getFuture(), $links);
34 }
35
36 /**
37 * Perform a GET request against the link's "next" relation
38 *
39 * @return Promise
40 */
41 function next(API $api, Cache\Service $cache = null) : Promise {
42 $links = $api->getLinks();
43 if ($links && ($next = $links->getNext())) {
44 return $api->withUrl($next)->get(null, null, $cache);
45 }
46 return Future\reject($api->getFuture(), $links);
47 }
48
49 /**
50 * Perform a GET request against the link's "last" relation
51 *
52 * @return Promise
53 */
54 function last(API $api, Cache\Service $cache = null) : Promise {
55 $links = $api->getLinks();
56 if ($links && ($last = $links->getLast())) {
57 return $api->withUrl($last)->get(null, null, $cache);
58 }
59 return Future\reject($api->getFuture(), $links);
60 }
61