xref: /PHP-5.3/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			if (ini_get('magic_quotes_runtime')) {
55				$data = stripslashes($data);
56			}
57			//return unserialize($data);
58			return $data;
59		}
60		else
61		{
62			return NULL;
63		}
64	}
65
66	/**
67	 * Set an entry.
68	 *
69	 * @param $name key to write to
70	 * @param $value value to write
71	 */
72	function offsetSet($name, $value)
73	{
74		//dba_replace($name, serialize($value), $this->db);
75		dba_replace($name, $value, $this->db);
76		return $value;
77	}
78
79	/**
80	 * @return whether key $name exists.
81	 */
82	function offsetExists($name)
83	{
84		return dba_exists($name, $this->db);
85	}
86
87	/**
88	 * Delete a key/value pair.
89	 *
90	 * @param $name key to delete.
91	 */
92	function offsetUnset($name)
93	{
94		return dba_delete($name, $this->db);
95	}
96}
97
98?>