File: dbaarray.inc

package info (click to toggle)
php5 5.6.7%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie-kfreebsd
  • size: 150,376 kB
  • sloc: ansic: 727,510; php: 21,966; sh: 12,356; cpp: 8,763; xml: 6,105; yacc: 1,551; exp: 1,514; makefile: 1,461; pascal: 1,048; awk: 538; perl: 315; sql: 22
file content (95 lines) | stat: -rw-r--r-- 1,639 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
<?php

/** @file dbaarray.inc
 * @ingroup Examples
 * @brief class DbaArray
 * @author  Marcus Boerger
 * @date    2003 - 2005
 *
 * SPL - Standard PHP Library
 */

if (!class_exists("DbaReader", false)) require_once("dbareader.inc");

/** @ingroup Examples
 * @brief   This implements a DBA Array
 * @author  Marcus Boerger
 * @version 1.0
 */
class DbaArray extends DbaReader implements ArrayAccess
{

	/**
	 * 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)
	{
		$this->db = dba_popen($file, "c", $handler);
		if (!$this->db) {
			throw new exception("Databse could not be opened");
		}
	}

	/**
	 * Close database.
	 */
	function __destruct()
	{
		parent::__destruct();
	}

	/**
	 * Read an entry.
	 *
	 * @param $name key to read from
	 * @return value associated with $name
	 */
	function offsetGet($name)
	{
		$data = dba_fetch($name, $this->db); 
		if($data) {
			//return unserialize($data);
			return $data;
		}
		else 
		{
			return NULL;
		}
	}

	/**
	 * Set an entry.
	 *
	 * @param $name key to write to
	 * @param $value value to write
	 */
	function offsetSet($name, $value)
	{
		//dba_replace($name, serialize($value), $this->db);
		dba_replace($name, $value, $this->db);
		return $value;
	}

	/**
	 * @return whether key $name exists.
	 */
	function offsetExists($name)
	{
		return dba_exists($name, $this->db);
	}

	/**
	 * Delete a key/value pair.
	 *
	 * @param $name key to delete.
	 */
	function offsetUnset($name)
	{
		return dba_delete($name, $this->db);
	}
}

?>