dbareader.inc 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. /** @file dbareader.inc
  3. * @ingroup Examples
  4. * @brief class DbaReader
  5. * @author Marcus Boerger
  6. * @date 2003 - 2005
  7. *
  8. * SPL - Standard PHP Library
  9. */
  10. /** @ingroup Examples
  11. * @brief This implements a DBA Iterator.
  12. * @author Marcus Boerger
  13. * @version 1.0
  14. */
  15. class DbaReader implements Iterator
  16. {
  17. protected $db = NULL;
  18. private $key = false;
  19. private $val = false;
  20. /**
  21. * Open database $file with $handler in read only mode.
  22. *
  23. * @param file Database file to open.
  24. * @param handler Handler to use for database access.
  25. */
  26. function __construct($file, $handler) {
  27. if (!$this->db = dba_open($file, 'r', $handler)) {
  28. throw new exception('Could not open file ' . $file);
  29. }
  30. }
  31. /**
  32. * Close database.
  33. */
  34. function __destruct() {
  35. dba_close($this->db);
  36. }
  37. /**
  38. * Rewind to first element.
  39. */
  40. function rewind() {
  41. $this->key = dba_firstkey($this->db);
  42. $this->fetch_data();
  43. }
  44. /**
  45. * Move to next element.
  46. *
  47. * @return void
  48. */
  49. function next() {
  50. $this->key = dba_nextkey($this->db);
  51. $this->fetch_data();
  52. }
  53. /**
  54. * Fetches the current data if $key is valid
  55. */
  56. private function fetch_data() {
  57. if ($this->key !== false) {
  58. $this->val = dba_fetch($this->key, $this->db);
  59. }
  60. }
  61. /**
  62. * @return Current data.
  63. */
  64. function current() {
  65. return $this->val;
  66. }
  67. /**
  68. * @return Whether more elements are available.
  69. */
  70. function valid() {
  71. if ($this->db && $this->key !== false) {
  72. return true;
  73. } else {
  74. return false;
  75. }
  76. }
  77. /**
  78. * @return Current key.
  79. */
  80. function key() {
  81. return $this->key;
  82. }
  83. }
  84. ?>