initial checkin
[m6w6/seekat] / lib / API / Iterator.php
diff --git a/lib/API/Iterator.php b/lib/API/Iterator.php
new file mode 100644 (file)
index 0000000..8f57b09
--- /dev/null
@@ -0,0 +1,86 @@
+<?php
+
+namespace seekat\API;
+
+use seekat\API;
+
+class Iterator implements \Iterator {
+       /**
+        * The endpoint
+        * @var \seekat\API
+        */
+       private $api;
+
+       /**
+        * The iterator's data
+        * @var array
+        */
+       private $data;
+
+       /**
+        * The current key
+        * @var int|string
+        */
+       private $key;
+
+       /**
+        * The current data entry
+        * @var mixed
+        */
+       private $cur;
+
+       /**
+        * Create a new iterator over $data returning \seekat\API instances
+        *
+        * @var \seekat\API $api The endpoint
+        * @var array|object $data
+        */
+       function __construct(API $api) {
+               $this->api = $api;
+               $this->data = (array) $api->export();
+       }
+
+       /**
+        * Get the current key
+        *
+        * @return int|string
+        */
+       function key() {
+               return $this->key;
+       }
+
+       /**
+        * Get the current data entry
+        *
+        * @return \seekat\API
+        */
+       function current() {
+               return $this->cur;
+       }
+
+       function next() {
+               if (list($key, $cur) = each($this->data)) {
+                       $this->key = $key;
+                       if ($this->api->$key->exists("url", $url)) {
+                               $url = new \http\Url($url);
+                               $this->cur = $this->api->withUrl($url)->withData($cur);
+                       } else {
+                               $this->cur = $this->api->$key->withData($cur);
+                       }
+               } else {
+                       $this->key = null;
+                       $this->cur = null;
+               }
+       }
+
+       function valid() {
+               return isset($this->cur);
+       }
+
+       function rewind() {
+               if (is_array($this->data)) {
+                       reset($this->data);
+                       $this->next();
+               }
+       }
+}