-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcustomDocumentClass.php
360 lines (307 loc) · 9.4 KB
/
customDocumentClass.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<?php
namespace ArangoDBClient;
require __DIR__ . '/init.php';
abstract class AbstractEntity extends Document implements \JsonSerializable
{
/**
* Collection name.
*
* @var string
*/
protected $_collectionName;
/**
* Constructor.
*
* {@inheritdoc}
*
* @param array $options - optional, initial $options for document
*
* @throws \Exception
*/
public function __construct(array $options = null)
{
parent::__construct($options);
if (empty($this->_collectionName)) {
throw new \Exception('No collection name provided!!!', 666);
}
}
/**
* @return string
*/
public function getCollectionName()
{
return $this->_collectionName;
}
/**
* Sets internal key (eg. when using in forms).
*
* @param string $key
*/
public function setInternalKey($key)
{
parent::setInternalKey($key);
if (empty($this->_id)) {
$this->_id = $this->_collectionName . '/' . $key;
}
}
/**
* Called when entity is created
*/
public function onCreate()
{
}
/**
* Called when entity is saved
*/
public function onUpdate()
{
}
/**
* Specify data which should be serialized to JSON
*
* @link http://php.net/manual/en/jsonserializable.jsonserialize.php
* @return mixed data which can be serialized by <b>json_encode</b>,
* which is a value of any type other than a resource.
* @since 3.2
*/
public function jsonSerialize()
{
return $this->getAll();
}
}
abstract class AbstractCollection extends CollectionHandler
{
/**
* @var string collection name
*/
protected $_collectionName;
/**
* @var DocumentHandler
*/
protected $_documentHandler;
/**
* AbstractCollection constructor.
*
* {@inheritdoc}
*
* @param Connection $connection
*
* @throws \Exception
*/
public function __construct(Connection $connection)
{
parent::__construct($connection);
if (empty($this->_collectionName)) {
throw new \Exception('No collection name provided!!!', 666);
}
$this->_documentHandler = new DocumentHandler($connection);
$this->_documentHandler->setDocumentClass($this->_documentClass);
}
/**
* @return string
*/
public function getCollectionNameString()
{
return $this->_collectionName;
}
/**
* Get document(s) by specifying an example
*
* This will throw if the list cannot be fetched from the server
*
*
* @throws Exception
*
* @param mixed $document - the example document as a Document object or an array
* @param bool|array $options - optional, prior to v1.0.0 this was a boolean value for sanitize, since v1.0.0 it's an array of options.
* <p>Options are :<br>
* <li>'_sanitize' - True to remove _id and _rev attributes from result documents. Defaults to false.</li>
* <li>'sanitize' - Deprecated, please use '_sanitize'.</li>
* <li>'_hiddenAttributes' - Set an array of hidden attributes for created documents.
* <li>'hiddenAttributes' - Deprecated, please use '_hiddenAttributes'.</li>
* <p>
* This is actually the same as setting hidden attributes using setHiddenAttributes() on a document. <br>
* The difference is, that if you're returning a resultset of documents, the getAll() is already called <br>
* and the hidden attributes would not be applied to the attributes.<br>
* </p>
* </li>
* <li>'batchSize' - can optionally be used to tell the server to limit the number of results to be transferred in one batch</li>
* <li>'skip' - Optional, The number of documents to skip in the query.</li>
* <li>'limit' - Optional, The maximal amount of documents to return. 'skip' is applied before the limit restriction.</li>
* </p>
*
* @return cursor - Returns a cursor containing the result
*/
public function findByExample($document, $options = [])
{
return parent::byExample($this->_collectionName, $document, $options);
}
/**
* Find all documents for given keys
*
* @param array $ids - array of document keys
*
* @return array of matching entities
*/
public function findByIds($ids)
{
return $this->lookupByKeys($this->_collectionName, $ids);
}
/**
* Find by Example.
*
* @param array $example
*
* @return AbstractEntity|bool
*/
public function findOneByExample($example)
{
$cursor = $this->byExample($this->_collectionName, $example);
if ($cursor->getCount() > 0) {
/* @var $document AbstractEntity */
$document = $cursor->getAll()[0];
$document->setIsNew(false);
return $document;
}
return false;
}
/**
* Gets one document by given ID
*
* @param string|int $id
*
* @return AbstractEntity|null
* @throws ServerException
*/
public function findOneById($id)
{
try {
return $this->_documentHandler->getById($this->_collectionName, $id);
} catch (ServerException $e) {
if ($e->getServerMessage() === 'document not found') {
return null;
}
throw $e;
}
}
/**
* Gets internal collection name
*
* @return string
*/
public function getInternalCollectionName()
{
return $this->_collectionName;
}
/**
* Store a document to a collection
*
* {@inheritDoc}
*
* @param AbstractEntity $document
*
* @return mixed
*/
public function store($document)
{
if (is_null($document->get('_dateCreated'))) {
$document->set('_dateCreated', date('Y-m-d H:i:s'));
}
$document->set('_dateUpdated', date('Y-m-d H:i:s'));
if ($document->getIsNew()) {
if (method_exists($document, 'onCreate')) {
$document->onCreate();
}
return $this->_documentHandler->save($this->_collectionName, $document);
} else {
if (method_exists($document, 'onUpdate')) {
$document->onUpdate();
}
return $this->_documentHandler->replace($document);
}
}
/**
* Removes specified document from collection
*
* @param AbstractEntity $document
* @param $options
*
* @return array - an array containing an attribute 'removed' with the number of documents that were deleted, an an array 'ignored' with the number of not removed keys/documents
*/
public function removeDocument(AbstractEntity $document, $options = [])
{
return $this->removeByKeys($this->_collectionName, [$document->getInternalKey()], $options);
}
}
class User extends AbstractEntity
{
/**
* Collection name.
*
* @var string
*/
protected $_collectionName = 'users';
public function setName($value)
{
$this->set('name', trim($value));
}
public function setAge($value)
{
$this->set('age', (int) $value);
}
public function onCreate()
{
parent::onCreate();
$this->set('_dateCreated', date('Y-m-d H:i:s'));
}
public function onUpdate()
{
parent::onUpdate();
$this->set('_dateUpdated', date('Y-m-d H:i:s'));
}
}
class Users extends AbstractCollection
{
protected $_documentClass = '\ArangoDBClient\User';
protected $_collectionName = 'users';
public function getByAge($value)
{
return $this->findByExample(['age' => $value])->getAll();
}
}
try {
$connection = new Connection($connectionOptions);
$usersCollection = new Users($connection);
// set up a document collection "users"
$collection = new Collection('users');
try {
$usersCollection->create($collection);
} catch (\Exception $e) {
// collection may already exist - ignore this error for now
}
// create a new document
$user1 = new User();
$user1->setName(' John ');
$user1->setAge(19);
$usersCollection->store($user1);
var_dump($user1);
$user2 = new User();
$user2->setName('Marry');
$user2->setAge(19);
$usersCollection->store($user2);
var_dump(json_encode($user2));
// get document by example
$cursor = $usersCollection->findOneByExample(['age' => 19, 'name' => 'John']);
var_dump($cursor);
// get cursor by example
$cursor = $usersCollection->findByExample(['age' => 19]);
var_dump($cursor->getAll());
$array = $usersCollection->getByAge(19);
var_dump($array);
} catch (ConnectException $e) {
print $e . PHP_EOL;
} catch (ServerException $e) {
print $e . PHP_EOL;
} catch (ClientException $e) {
print $e . PHP_EOL;
}