dbaarray.inc 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. <?php
  2. /** @file dbaarray.inc
  3. * @ingroup Examples
  4. * @brief class DbaArray
  5. * @author Marcus Boerger
  6. * @date 2003 - 2005
  7. *
  8. * SPL - Standard PHP Library
  9. */
  10. if (!class_exists("DbaReader", false)) require_once("dbareader.inc");
  11. /** @ingroup Examples
  12. * @brief This implements a DBA Array
  13. * @author Marcus Boerger
  14. * @version 1.0
  15. */
  16. class DbaArray extends DbaReader implements ArrayAccess
  17. {
  18. /**
  19. * Open database $file with $handler in read only mode.
  20. *
  21. * @param file Database file to open.
  22. * @param handler Handler to use for database access.
  23. */
  24. function __construct($file, $handler)
  25. {
  26. $this->db = dba_popen($file, "c", $handler);
  27. if (!$this->db) {
  28. throw new exception("Databse could not be opened");
  29. }
  30. }
  31. /**
  32. * Close database.
  33. */
  34. function __destruct()
  35. {
  36. parent::__destruct();
  37. }
  38. /**
  39. * Read an entry.
  40. *
  41. * @param $name key to read from
  42. * @return value associated with $name
  43. */
  44. function offsetGet($name)
  45. {
  46. $data = dba_fetch($name, $this->db);
  47. if($data) {
  48. //return unserialize($data);
  49. return $data;
  50. }
  51. else
  52. {
  53. return NULL;
  54. }
  55. }
  56. /**
  57. * Set an entry.
  58. *
  59. * @param $name key to write to
  60. * @param $value value to write
  61. */
  62. function offsetSet($name, $value)
  63. {
  64. //dba_replace($name, serialize($value), $this->db);
  65. dba_replace($name, $value, $this->db);
  66. return $value;
  67. }
  68. /**
  69. * @return whether key $name exists.
  70. */
  71. function offsetExists($name)
  72. {
  73. return dba_exists($name, $this->db);
  74. }
  75. /**
  76. * Delete a key/value pair.
  77. *
  78. * @param $name key to delete.
  79. */
  80. function offsetUnset($name)
  81. {
  82. return dba_delete($name, $this->db);
  83. }
  84. }
  85. ?>