|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* @file TDoc.php |
|
5
|
|
|
* @brief This file contains the TDoc trait. |
|
6
|
|
|
* @details |
|
7
|
|
|
* @author Filippo F. Fadda |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
|
|
11
|
|
|
namespace EoC\Doc; |
|
12
|
|
|
|
|
13
|
|
|
|
|
14
|
|
|
use EoC\Extension\TProperty; |
|
15
|
|
|
use EoC\Helper; |
|
16
|
|
|
|
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @brief Implements the IDoc interface and add many functions. |
|
20
|
|
|
* @see AbstractDoc.dox |
|
21
|
|
|
*/ |
|
22
|
|
|
trait TDoc { |
|
23
|
|
|
use TProperty; |
|
24
|
|
|
|
|
25
|
|
|
protected $meta = []; |
|
26
|
|
|
|
|
27
|
|
|
|
|
28
|
|
|
abstract protected function fixDocId(); |
|
29
|
|
|
|
|
30
|
|
|
|
|
31
|
|
|
abstract public function getPath(); |
|
32
|
|
|
|
|
33
|
|
|
|
|
34
|
|
|
public function resetMetadata() { |
|
35
|
|
|
unset($this->meta); |
|
36
|
|
|
$this->meta = []; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
|
|
40
|
|
|
public function getMetadata($name) { |
|
41
|
|
|
return @$this->meta[$name]; |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
|
|
45
|
|
|
public function isMetadataPresent($name) { |
|
46
|
|
|
return (array_key_exists($name, $this->meta)) ? TRUE : FALSE; |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
|
|
50
|
|
|
public function setMetadata($name, $value, $override = TRUE, $allowNull = TRUE) { |
|
51
|
|
|
if (is_null($value) && !$allowNull) |
|
52
|
|
|
return; |
|
53
|
|
|
|
|
54
|
|
|
if ($this->isMetadataPresent($name) && !$override) |
|
55
|
|
|
return; |
|
56
|
|
|
|
|
57
|
|
|
$this->meta[$name] = $value; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
public function unsetMetadata($name) { |
|
62
|
|
|
if (array_key_exists($name, $this->meta)) |
|
63
|
|
|
unset($this->meta[$name]); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
public function setClass($value) { |
|
68
|
|
|
$this->meta['class'] = $value; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
|
|
72
|
|
|
public function setType($value) { |
|
73
|
|
|
$this->meta['type'] = $value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
|
|
77
|
|
|
public function getType() { |
|
78
|
|
|
return $this->meta['type']; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
public function hasType() { |
|
83
|
|
|
return FALSE; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
|
|
87
|
|
|
public function assignJson($json) { |
|
88
|
|
|
$this->meta = array_merge($this->meta, Helper\ArrayHelper::fromJson($json, TRUE)); |
|
89
|
|
|
$this->fixDocId(); |
|
90
|
|
|
} |
|
91
|
|
|
|
|
92
|
|
|
|
|
93
|
|
|
public function assignArray(array $array) { |
|
94
|
|
|
if (Helper\ArrayHelper::isAssociative($array)) { |
|
95
|
|
|
$this->meta = array_merge($this->meta, $array); |
|
96
|
|
|
$this->fixDocId(); |
|
97
|
|
|
} |
|
98
|
|
|
else |
|
99
|
|
|
throw new \InvalidArgumentException("\$array must be an associative array."); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
|
|
103
|
|
|
public function assignObject(\stdClass $object) { |
|
104
|
|
|
$this->meta = array_merge($this->meta, get_object_vars($object)); |
|
105
|
|
|
$this->fixDocId(); |
|
106
|
|
|
} |
|
107
|
|
|
|
|
108
|
|
|
|
|
109
|
|
|
public function asJson() { |
|
110
|
|
|
$json = json_encode($this->meta, |
|
111
|
|
|
JSON_UNESCAPED_UNICODE | |
|
112
|
|
|
JSON_PARTIAL_OUTPUT_ON_ERROR | |
|
113
|
|
|
JSON_PRESERVE_ZERO_FRACTION |
|
114
|
|
|
); |
|
115
|
|
|
|
|
116
|
|
|
if ($json === FALSE) |
|
117
|
|
|
throw new \RuntimeException(json_last_error_msg()); |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
|
|
|
|
121
|
|
|
public function asArray() { |
|
122
|
|
|
return $this->meta; |
|
123
|
|
|
} |
|
124
|
|
|
|
|
125
|
|
|
|
|
126
|
|
|
public function delete() { |
|
127
|
|
|
$this->meta['_deleted'] = TRUE; |
|
128
|
|
|
} |
|
129
|
|
|
|
|
130
|
|
|
|
|
131
|
|
|
public function isDeleted() { |
|
132
|
|
|
return $this->meta['_deleted']; |
|
133
|
|
|
} |
|
134
|
|
|
|
|
135
|
|
|
|
|
136
|
|
|
public function getRevisions() { |
|
137
|
|
|
return (array_key_exists('_revisions', $this->meta)) ? $this->meta['_revisions'] : NULL; |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
|
|
|
|
141
|
|
|
public function getId() { |
|
142
|
|
|
return $this->meta['_id']; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
|
|
146
|
|
|
public function issetId() { |
|
147
|
|
|
return isset($this->meta['_id']); |
|
148
|
|
|
} |
|
149
|
|
|
|
|
150
|
|
|
|
|
151
|
|
|
public function setId($value) { |
|
152
|
|
|
$this->meta['_id'] = (string)$value; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
|
|
156
|
|
|
public function unsetId() { |
|
157
|
|
|
if ($this->isMetadataPresent('_id')) |
|
158
|
|
|
unset($this->meta['_id']); |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
|
|
162
|
|
|
public function getRev() { |
|
163
|
|
|
return $this->meta['_rev']; |
|
164
|
|
|
} |
|
165
|
|
|
|
|
166
|
|
|
|
|
167
|
|
|
public function issetRev() { |
|
168
|
|
|
return isset($this->meta['_rev']); |
|
169
|
|
|
} |
|
170
|
|
|
|
|
171
|
|
|
|
|
172
|
|
|
public function setRev($value) { |
|
173
|
|
|
$this->meta['_rev'] = (string)$value; |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
|
|
177
|
|
|
public function unsetRev() { |
|
178
|
|
|
if ($this->isMetadataPresent('_rev')) |
|
179
|
|
|
unset($this->meta['_rev']); |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
|
|
} |