php-src/ext/spl/examples/dbareader.inc

96 lines
1.6 KiB
PHP
Raw Normal View History

2003-07-16 21:52:03 +00:00
<?php
/** @file dbareader.inc
* @ingroup Examples
* @brief class DbaReader
* @author Marcus Boerger
2005-02-08 19:10:06 +00:00
* @date 2003 - 2005
*
* SPL - Standard PHP Library
*/
/** @ingroup Examples
* @brief This implements a DBA Iterator.
2003-07-16 21:52:03 +00:00
* @author Marcus Boerger
* @version 1.0
*/
2003-11-22 20:51:15 +00:00
class DbaReader implements Iterator
2003-07-16 21:52:03 +00:00
{
protected $db = NULL;
2003-07-16 21:52:03 +00:00
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) {
2004-07-28 22:52:11 +00:00
if (!$this->db = dba_open($file, 'r', $handler)) {
throw new exception('Could not open file ' . $file);
}
2003-07-16 21:52:03 +00:00
}
/**
* Close database.
*/
function __destruct() {
2004-07-28 22:52:11 +00:00
dba_close($this->db);
2003-07-16 21:52:03 +00:00
}
/**
* Rewind to first element.
*/
function rewind() {
2004-07-28 22:52:11 +00:00
$this->key = dba_firstkey($this->db);
$this->fetch_data();
2003-07-16 21:52:03 +00:00
}
/**
* Move to next element.
*
* @return void
*/
function next() {
2004-07-28 22:52:11 +00:00
$this->key = dba_nextkey($this->db);
$this->fetch_data();
2004-07-28 22:52:11 +00:00
}
/**
* Fetches the current data if $key is valid
*/
private function fetch_data() {
if ($this->key !== false) {
$this->val = dba_fetch($this->key, $this->db);
2003-07-16 21:52:03 +00:00
}
}
2004-07-28 22:52:11 +00:00
/**
* @return Current data.
*/
function current() {
return $this->val;
}
2003-07-16 21:52:03 +00:00
/**
* @return Whether more elements are available.
*/
function valid() {
2003-07-16 21:52:03 +00:00
if ($this->db && $this->key !== false) {
return true;
} else {
return false;
}
}
/**
* @return Current key.
*/
function key() {
return $this->key;
}
}
?>