-
Notifications
You must be signed in to change notification settings - Fork 0
/
CouchData.class.php
executable file
·172 lines (151 loc) · 3.6 KB
/
CouchData.class.php
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
class CouchData implements ArrayAccess
{
static public
$specialFields = array(
'_id' => true,
'_rev' => true,
'_attachments' => false,
'_deleted' => false,
'_revisions' => false,
'_rev_infos' => false,
'_conflicts' => false,
'_deleted_conflicts' => false
);
protected
$data = array(), // variable holding all data
$readOnly = false; // is this a readonly object?
/**
* makeArray converts the argument to an array if possible
*
* @param mixed $date is a CouchData object, JSON string, null or array
* @return array The argument in array format
* @author The Young Shepherd
**/
static public function makeArray($data)
{
if ($data instanceof CouchData)
{
$data = $data->getData();
}
elseif (is_string($data))
{
$data = Zend_Json::decode($data);
}
if (is_null($data))
{
$data = array();
}
if (is_array($data))
{
return $data;
}
throw new CouchException('Error converting value to array');
}
public function __construct($data = array(), $readOnly = false)
{
$this->setData(self::makeArray($data));
$this->readOnly = $readOnly;
}
public function isReadOnly()
{
return $this->readOnly;
}
public function __toString()
{
return Zend_JSON::encode($this->getData());
}
public function getData()
{
return $this->data;
}
/**
* tests if this is a writeable object, otherwise throw exception
*
* @return true
* @throws CouchException when the object is not writable
* @author The Young Shepherd
**/
public function assertWritable()
{
if ($this->readOnly)
{
throw new CouchException('Cannot write to a readonly CouchData object');
}
return true;
}
public function setData(array $data)
{
$this->assertWritable();
$this->data = $data;
}
public function get($key, $default = null)
{
return isset($this[$key]) ? $this[$key] : $default;
}
/**
* __get implements object access to metadata properties of this dat object
*
* @return mixed the value
* @author The Young Shepherd
**/
public function __get($name)
{
if (isset($this->$name))
{
return $this->data['_'.$name];
}
throw new RuntimeException(sprintf('Property \'%s\' does not exist', $name));
}
public function __set($name, $value)
{
if (in_array('_'.$name, self::$specialFields))
{
$this->assertWritable();
$this->data['_'.$name] = $value;
}
else
{
throw new RuntimeException(sprintf('Property \'%s\' does not exist', $name));
}
}
public function __isset($name)
{
return isset($this->data['_'.$name]) && array_key_exists('_'.$name, self::$specialFields);
}
public function __unset($name)
{
if (isset(self::$specialFields['_'.$name]) && !self::$specialFields['_'.$name])
{
$this->assertWritable();
unset($this->data['_'.$name]);
}
else
{
throw new RuntimeException(sprintf('Property \'%s\' does not exist', $name));
}
}
//implementing ArrayAccess
public function offsetGet($name)
{
if (isset($this[$name]))
{
return $this->data[$name];
}
throw new RuntimeException(sprintf('Key \'%s\' does not exist', $name));
}
public function offsetSet($name, $value)
{
$this->assertWritable();
$this->data[$name] = $value;
}
public function offsetExists($name)
{
return array_key_exists($name, $this->data);
}
public function offsetUnset($name)
{
$this->assertWritable();
unset($this->data[$name]);
}
}