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