summaryrefslogtreecommitdiff
path: root/weave_basic_object.php
blob: ef30c6905b38f5b5cac4c3a74f2ded396a2e175c (plain)
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
<?php
 
# ***** BEGIN LICENSE BLOCK ***** 
# Version: MPL 1.1/GPL 2.0/LGPL 2.1 
# 
# The contents of this file are subject to the Mozilla Public License Version 
# 1.1 (the "License"); you may not use this file except in compliance with 
# the License. You may obtain a copy of the License at 
# http://www.mozilla.org/MPL/ 
# 
# Software distributed under the License is distributed on an "AS IS" basis, 
# WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License 
# for the specific language governing rights and limitations under the 
# License. 
# 
# The Original Code is Weave Basic Object Server 
# 
# The Initial Developer of the Original Code is 
# Mozilla Labs. 
# Portions created by the Initial Developer are Copyright (C) 2008 
# the Initial Developer. All Rights Reserved. 
# 
# Contributor(s): 
# Toby Elliott (telliott@mozilla.com) 
# 
# Alternatively, the contents of this file may be used under the terms of 
# either the GNU General Public License Version 2 or later (the "GPL"), or 
# the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), 
# in which case the provisions of the GPL or the LGPL are applicable instead 
# of those above. If you wish to allow use of your version of this file only 
# under the terms of either the GPL or the LGPL, and not to allow others to 
# use your version of this file under the terms of the MPL, indicate your 
# decision by deleting the provisions above and replace them with the notice 
# and other provisions required by the GPL or the LGPL. If you do not delete 
# the provisions above, a recipient may use your version of this file under 
# the terms of any one of the MPL, the GPL or the LGPL. 
# 
# ***** END LICENSE BLOCK ***** 
 
class wbo 
{
var $wbo_hash = array();
var $_collection;
var $_error = array();
function extract_json(&$json)
{
$extracted = is_string($json) ? json_decode($jsontrue) : $json;
 
#need to check the json was valid here... 
if ($extracted === null)
{
$this->_error[] = "unable to extract from json";
return 0;
}
#must have an id, or all sorts of badness happens. However, it can be added later 
if (array_key_exists('id'$extracted))
{
$this->id($extracted['id']);
}
if (array_key_exists('parentid'$extracted))
{
$this->parentid($extracted['parentid']);
}
if (array_key_exists('predecessorid'$extracted))
{
$this->predecessorid($extracted['predecessorid']);
}
 
if (array_key_exists('depth'$extracted))
{
$this->depth($extracted['depth']);
}
 
if (array_key_exists('sortindex'$extracted))
{
$this->sortindex($extracted['sortindex']);
}
if (array_key_exists('payload'$extracted))
{
$this->payload($extracted['payload']);
}
return 1;
}
function populate(&$datahash)
{
$this->id($datahash['id']);
$this->collection($datahash['collection']);
$this->parentid($datahash['parentid']);
$this->modified($datahash['modified']);
$this->predecessorid($datahash['predecessorid']);
$this->sortindex($datahash['sortindex']);
$this->payload($datahash['payload']);
 
$this->depth($datahash['depth']);
}
 
function id($id = null)
{
if (!is_null($id)) { $this->wbo_hash['id'] = $id; }
return array_key_exists('id'$this->wbo_hash) ?  $this->wbo_hash['id'] : null;
}
function collection($collection = null)
{
if (!is_null($collection)){ $this->_collection = $collection; }
return $this->_collection;
}
function parentid($parentid = null)
{
if (!is_null($parentid)){ $this->wbo_hash['parentid'] = $parentid; }
return array_key_exists('parentid'$this->wbo_hash) ?  $this->wbo_hash['parentid'] : null;
}
function parentid_exists()
{
return array_key_exists('parentid'$this->wbo_hash);
}
function predecessorid($predecessorid = null)
{
if (!is_null($predecessorid)){ $this->wbo_hash['predecessorid'] = $predecessorid; }
return array_key_exists('predecessorid'$this->wbo_hash) ?  $this->wbo_hash['predecessorid'] : null;
}
function predecessorid_exists()
{
return array_key_exists('predecessorid'$this->wbo_hash);
}
function modified($modified = null)
{
if (!is_null($modified)){ $this->wbo_hash['modified'] = round((float)$modified2); }
return array_key_exists('modified'$this->wbo_hash) ?  $this->wbo_hash['modified'] : null;
}
function modified_exists()
{
return array_key_exists('modified'$this->wbo_hash);
}
function payload($payload = null)
{
if (!is_null($payload)){ $this->wbo_hash['payload'] = $payload; }
return array_key_exists('payload'$this->wbo_hash) ?  $this->wbo_hash['payload'] : null;
}
function payload_exists()
{
return array_key_exists('payload'$this->wbo_hash);
}
 
function payload_size()
{
return mb_strlen($this->wbo_hash['payload']'8bit');
}
function sortindex($index = null)
{
if (!is_null($index)){ $this->wbo_hash['sortindex'] = (int)$index; }
return array_key_exists('sortindex'$this->wbo_hash) ?  $this->wbo_hash['sortindex'] : null;
}
 
function sortindex_exists()
{
return array_key_exists('sortindex'$this->wbo_hash);
}
function depth($depth = null)
{
if (!is_null($depth)){ $this->wbo_hash['depth'] = (int)$depth; }
return array_key_exists('depth'$this->wbo_hash) ?  $this->wbo_hash['depth'] : null;
}
 
function depth_exists()
{
return array_key_exists('depth'$this->wbo_hash);
}
function validate()
{
if (!$this->id(|| strlen($this->id()) > 64)
{ $this->_error[] = "invalid id"; }
 
if ($this->parentid_exists(&& strlen($this->parentid()) > 64)
{ $this->_error[] = "invalid parentid"; }
 
if ($this->predecessorid_exists(&& strlen($this->predecessorid()) > 64)
{ $this->_error[] = "invalid predecessorid"; }
 
if (!is_numeric($this->modified()))
{ $this->_error[] = "invalid modified date"; }
if (!$this->modified())
{ $this->_error[] = "no modification date"; }
 
if (!$this->_collection || strlen($this->_collection> 64)
{ $this->_error[] = "invalid collection"; }
if ($this->depth_exists(&& !is_numeric($this->depth()))
{ $this->_error[] = "invalid depth"; }
if ($this->sortindex_exists(&& !is_numeric($this->sortindex()))
{ $this->_error[] = "invalid sortindex"; }
if ($this->payload_exists())
{
if (!is_string($this->wbo_hash['payload']))
{ $this->_error[] = "payload needs to be json-encoded"; }
}
return !$this->get_error();
}
 
function get_error()
{
return $this->_error;
}
function clear_error()
{
$this->_error = array();
}
function json()
{
return json_encode($this->wbo_hash);
}
}
 
 
?>