f0179b48087b2718e5a67426ae8aad915119128c
[m6w6/seekat] / lib / API / Iterator.php
1 <?php
2
3 namespace seekat\API;
4
5 use seekat\API;
6
7 class Iterator implements \Iterator
8 {
9 /**
10 * The endpoint
11 * @var \seekat\API
12 */
13 private $api;
14
15 /**
16 * The iterator's data
17 * @var array
18 */
19 private $data;
20
21 /**
22 * The current key
23 * @var int|string
24 */
25 private $key;
26
27 /**
28 * The current data entry
29 * @var mixed
30 */
31 private $cur;
32
33 /**
34 * Create a new iterator over $data returning \seekat\API instances
35 *
36 * @var \seekat\API $api The endpoint
37 * @var array|object $data
38 */
39 function __construct(API $api) {
40 $this->api = $api;
41 $this->data = (array) $api->export();
42 }
43
44 /**
45 * Get the current key
46 *
47 * @return int|string
48 */
49 function key() {
50 return $this->key;
51 }
52
53 /**
54 * Get the current data entry
55 *
56 * @return \seekat\API
57 */
58 function current() {
59 return $this->cur;
60 }
61
62 function next() {
63 if (list($key, $cur) = each($this->data)) {
64 $this->key = $key;
65 if ($this->api->$key->exists("url", $url)) {
66 $url = new \http\Url($url);
67 $this->cur = $this->api->withUrl($url)->withData($cur);
68 } else {
69 $this->cur = $this->api->$key->withData($cur);
70 }
71 } else {
72 $this->key = null;
73 $this->cur = null;
74 }
75 }
76
77 function valid() {
78 return isset($this->cur);
79 }
80
81 function rewind() {
82 if (is_array($this->data)) {
83 reset($this->data);
84 $this->next();
85 }
86 }
87 }