summaryrefslogtreecommitdiff
path: root/ext/spl/examples/dbareader.inc
diff options
context:
space:
mode:
authorLorry Tar Creator <lorry-tar-importer@baserock.org>2013-03-14 05:42:27 +0000
committer <>2013-04-03 16:25:08 +0000
commitc4dd7a1a684490673e25aaf4fabec5df138854c4 (patch)
tree4d57c44caae4480efff02b90b9be86f44bf25409 /ext/spl/examples/dbareader.inc
downloadphp2-master.tar.gz
Imported from /home/lorry/working-area/delta_php2/php-5.4.13.tar.bz2.HEADphp-5.4.13master
Diffstat (limited to 'ext/spl/examples/dbareader.inc')
-rw-r--r--ext/spl/examples/dbareader.inc96
1 files changed, 96 insertions, 0 deletions
diff --git a/ext/spl/examples/dbareader.inc b/ext/spl/examples/dbareader.inc
new file mode 100644
index 0000000..b097912
--- /dev/null
+++ b/ext/spl/examples/dbareader.inc
@@ -0,0 +1,96 @@
+<?php
+
+/** @file dbareader.inc
+ * @ingroup Examples
+ * @brief class DbaReader
+ * @author Marcus Boerger
+ * @date 2003 - 2005
+ *
+ * SPL - Standard PHP Library
+ */
+
+/** @ingroup Examples
+ * @brief This implements a DBA Iterator.
+ * @author Marcus Boerger
+ * @version 1.0
+ */
+class DbaReader implements Iterator
+{
+
+ protected $db = NULL;
+ private $key = false;
+ private $val = false;
+
+ /**
+ * Open database $file with $handler in read only mode.
+ *
+ * @param file Database file to open.
+ * @param handler Handler to use for database access.
+ */
+ function __construct($file, $handler) {
+ if (!$this->db = dba_open($file, 'r', $handler)) {
+ throw new exception('Could not open file ' . $file);
+ }
+ }
+
+ /**
+ * Close database.
+ */
+ function __destruct() {
+ dba_close($this->db);
+ }
+
+ /**
+ * Rewind to first element.
+ */
+ function rewind() {
+ $this->key = dba_firstkey($this->db);
+ $this->fetch_data();
+ }
+
+ /**
+ * Move to next element.
+ *
+ * @return void
+ */
+ function next() {
+ $this->key = dba_nextkey($this->db);
+ $this->fetch_data();
+ }
+
+ /**
+ * Fetches the current data if $key is valid
+ */
+ private function fetch_data() {
+ if ($this->key !== false) {
+ $this->val = dba_fetch($this->key, $this->db);
+ }
+ }
+
+ /**
+ * @return Current data.
+ */
+ function current() {
+ return $this->val;
+ }
+
+ /**
+ * @return Whether more elements are available.
+ */
+ function valid() {
+ if ($this->db && $this->key !== false) {
+ return true;
+ } else {
+ return false;
+ }
+ }
+
+ /**
+ * @return Current key.
+ */
+ function key() {
+ return $this->key;
+ }
+}
+
+?> \ No newline at end of file