PageRenderTime 7ms CodeModel.GetById 1ms app.highlight 4ms RepoModel.GetById 1ms app.codeStats 0ms

/framework/experimental/couch/CouchDocument.php

http://zoop.googlecode.com/
PHP | 94 lines | 78 code | 16 blank | 0 comment | 9 complexity | 8e4ae6fed4ab860e099cfeed893d9756 MD5 | raw file
 1<?php
 2class CouchDocument
 3{
 4	private $id;
 5	private $rev;
 6	private $data;
 7	
 8	static function findAll()
 9	{
10		return CouchModule::getConnection(self::getConnectionName())->getAllDocuments();
11	}
12	
13	function __construct($id, $rev = null)
14	{
15		$this->id = $id;
16		$this->rev = $rev;
17	}
18	
19	static private function getConnectionName()
20	{
21		return 'default';
22	}
23	
24	private function getDb()
25	{
26		return CouchModule::getConnection(self::getConnectionName());
27	}
28	
29	public function getId()
30	{
31		return $this->id;
32	}
33	
34	public function getRev()
35	{
36		return $this->rev;
37	}
38	
39	public function load()
40	{
41		$dbName = $this->getDb()->getdbName();
42		$resp = $this->getDb()->getHttp()->send("GET", "/$dbName/{$this->id}");
43		foreach($resp as $key => $value)
44		{
45			if($key == '_id')
46				$this->id = $value;
47			else if($key == '_rev')
48				$this->rev = $value;
49			else
50			{
51				$this->data->$key = $value;
52			}
53		}
54	}
55	
56	public function save()
57	{
58		$dbName = $this->getDb()->getDbName();
59		
60		$data->_id = $this->id;
61		if($this->rev)
62			$data->_rev = $this->rev;
63		foreach($this->data as $key => $val)
64		{
65			$data->$key = $val;
66		}
67		
68		$data = json_encode($data);
69		
70		$resp = $this->getDb()->getHttp()->send("PUT", "/$dbName/$this->id", $data);
71		
72		if($resp->ok != true)
73			trigger_error("saving document '{$this->id}:{$this->rev}' failed");
74		
75		$this->rev = $resp->rev;
76	}
77	
78	public function getData()
79	{
80		return $this->data();
81	}
82	
83	function __get($varname)
84	{
85		if(!isset($this->data->$varname))
86			trigger_error("field does not exist: $varname");
87		return $this->data->$varname;
88	}
89	
90	function __set($varname, $value)
91	{
92		$this->data->$varname = $value;
93	}	
94}