/
CouchDocument.class.php
executable file
·63 lines (53 loc) · 1.17 KB
/
CouchDocument.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
<?php
/*
idee is om een restrictie op een property te zetten waaraan dat veld moet voldoen
dus
naam => test
en naam => validatorstring(required true)
valideert en blijft valideren ook na toevoegen
*/
class CouchDocument extends CouchData
{
protected
$db = null;
static public function create(CouchDb $db, $data, $class = null)
{
if (null === $class)
{
$data = CouchData::makeArray($data);
$class = isset($data['php_class']) ? $data['php_class'] : __CLASS__;
}
return new $class($db, $data);
}
public function __construct(CouchDb $db, $rawData = array())
{
parent::__construct($rawData);
$this->db = $db;
// store as metadata the PHP class of this document
$this['php_class'] = get_class($this);
}
public function exists()
{
return isset($this->rev);
}
public function save()
{
if ($this->exists())
{
// Updating an existing document
return $this->db->update($this);
}
else
{
// Create a new document
return $this->db->create($this);
}
}
public function delete()
{
if ($this->exists())
{
return $this->db->delete($this);
}
}
}