From abd38899f6f1fcf7c8f16eb83356c8ce1070cf71 Mon Sep 17 00:00:00 2001 From: dakkar Date: Thu, 17 Mar 2011 00:01:25 +0000 Subject: import from http://tobyelliott.wordpress.com/2009/09/11/weave-minimal-server/ --- weave_basic_object.php | 241 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 241 insertions(+) create mode 100644 weave_basic_object.php (limited to 'weave_basic_object.php') diff --git a/weave_basic_object.php b/weave_basic_object.php new file mode 100644 index 0000000..ef30c69 --- /dev/null +++ b/weave_basic_object.php @@ -0,0 +1,241 @@ +_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)$modified, 2); } + 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); + } +} + + +?> \ No newline at end of file -- cgit v1.2.3