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