-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathDocument.php
More file actions
460 lines (406 loc) · 11 KB
/
Document.php
File metadata and controls
460 lines (406 loc) · 11 KB
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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
<?php
namespace Utopia\Database;
use ArrayObject;
use Utopia\Database\Exception as DatabaseException;
use Utopia\Database\Exception\Structure as StructureException;
/**
* @extends ArrayObject<string, mixed>
*/
class Document extends ArrayObject
{
public const SET_TYPE_ASSIGN = 'assign';
public const SET_TYPE_PREPEND = 'prepend';
public const SET_TYPE_APPEND = 'append';
/**
* Construct.
*
* Construct a new fields object
*
* @param array<string, mixed> $input
* @throws DatabaseException
* @see ArrayObject::__construct
*
*/
public function __construct(array $input = [])
{
if (isset($input['$id']) && !\is_string($input['$id'])) {
throw new StructureException('$id must be of type string');
}
if (isset($input['$permissions']) && !is_array($input['$permissions'])) {
throw new StructureException('$permissions must be of type array');
}
foreach ($input as $key => &$value) {
if (!\is_array($value)) {
continue;
}
if ((isset($value['$id']) || isset($value['$collection']))) {
$input[$key] = new self($value);
continue;
}
foreach ($value as $childKey => $child) {
if ((isset($child['$id']) || isset($child['$collection'])) && (!$child instanceof self)) {
$value[$childKey] = new self($child);
}
}
}
parent::__construct($input);
}
/**
* @return string
*/
public function getId(): string
{
return $this->getAttribute('$id', '');
}
/**
* @return int
*/
public function getSequence(): int
{
return $this->getAttribute('$sequence', 0);
}
/**
* @return string
*/
public function getCollection(): string
{
return $this->getAttribute('$collection', '');
}
/**
* @return array<string>
*/
public function getPermissions(): array
{
return \array_values(\array_unique($this->getAttribute('$permissions', [])));
}
/**
* @return array<string>
*/
public function getRead(): array
{
return $this->getPermissionsByType(Database::PERMISSION_READ);
}
/**
* @return array<string>
*/
public function getCreate(): array
{
return $this->getPermissionsByType(Database::PERMISSION_CREATE);
}
/**
* @return array<string>
*/
public function getUpdate(): array
{
return $this->getPermissionsByType(Database::PERMISSION_UPDATE);
}
/**
* @return array<string>
*/
public function getDelete(): array
{
return $this->getPermissionsByType(Database::PERMISSION_DELETE);
}
/**
* @return array<string>
*/
public function getWrite(): array
{
return \array_unique(\array_intersect(
$this->getCreate(),
$this->getUpdate(),
$this->getDelete()
));
}
/**
* @return array<string>
*/
public function getPermissionsByType(string $type): array
{
$typePermissions = [];
foreach ($this->getPermissions() as $permission) {
if (!\str_starts_with($permission, $type)) {
continue;
}
$typePermissions[] = \str_replace([$type . '(', ')', '"', ' '], '', $permission);
}
return \array_unique($typePermissions);
}
/**
* @return string|null
*/
public function getCreatedAt(): ?string
{
return $this->getAttribute('$createdAt');
}
/**
* @return string|null
*/
public function getUpdatedAt(): ?string
{
return $this->getAttribute('$updatedAt');
}
/**
* @return int|null
*/
public function getTenant(): ?int
{
$tenant = $this->getAttribute('$tenant');
if ($tenant !== null) {
return (int)$tenant;
}
return null;
}
/**
* Get Document Attributes
*
* @return array<string, mixed>
*/
public function getAttributes(): array
{
$attributes = [];
$internalKeys = \array_map(
fn ($attr) => $attr['$id'],
Database::INTERNAL_ATTRIBUTES
);
foreach ($this as $attribute => $value) {
if (\in_array($attribute, $internalKeys)) {
continue;
}
$attributes[$attribute] = $value;
}
return $attributes;
}
/**
* Get Attribute.
*
* Method for getting a specific fields attribute. If $name is not found $default value will be returned.
*
* @param string $name
* @param mixed $default
*
* @return mixed
*/
public function getAttribute(string $name, mixed $default = null): mixed
{
if (isset($this[$name])) {
return $this[$name];
}
return $default;
}
/**
* Set Attribute.
*
* Method for setting a specific field attribute
*
* @param string $key
* @param mixed $value
* @param string $type
*
* @return static
*/
public function setAttribute(string $key, mixed $value, string $type = self::SET_TYPE_ASSIGN): static
{
switch ($type) {
case self::SET_TYPE_ASSIGN:
$this[$key] = $value;
break;
case self::SET_TYPE_APPEND:
$this[$key] = (!isset($this[$key]) || !\is_array($this[$key])) ? [] : $this[$key];
\array_push($this[$key], $value);
break;
case self::SET_TYPE_PREPEND:
$this[$key] = (!isset($this[$key]) || !\is_array($this[$key])) ? [] : $this[$key];
\array_unshift($this[$key], $value);
break;
}
return $this;
}
/**
* Set Attributes.
*
* @param array<string, mixed> $attributes
* @return static
*/
public function setAttributes(array $attributes): static
{
foreach ($attributes as $key => $value) {
$this->setAttribute($key, $value);
}
return $this;
}
/**
* Remove Attribute.
*
* Method for removing a specific field attribute
*
* @param string $key
*
* @return static
*/
public function removeAttribute(string $key): static
{
if (\array_key_exists($key, (array)$this)) {
unset($this[$key]);
}
/* @phpstan-ignore-next-line */
return $this;
}
/**
* Find.
*
* @param string $key
* @param mixed $find
* @param string $subject
*
* @return mixed
*/
public function find(string $key, $find, string $subject = ''): mixed
{
$subject = $this[$subject] ?? null;
$subject = (empty($subject)) ? $this : $subject;
if (is_array($subject)) {
foreach ($subject as $i => $value) {
if (isset($value[$key]) && $value[$key] === $find) {
return $value;
}
}
return false;
}
if (isset($subject[$key]) && $subject[$key] === $find) {
return $subject;
}
return false;
}
/**
* Find and Replace.
*
* Get array child by key and value match
*
* @param string $key
* @param mixed $find
* @param mixed $replace
* @param string $subject
*
* @return bool
*/
public function findAndReplace(string $key, $find, $replace, string $subject = ''): bool
{
$subject = &$this[$subject] ?? null;
$subject = (empty($subject)) ? $this : $subject;
if (is_array($subject)) {
foreach ($subject as $i => &$value) {
if (isset($value[$key]) && $value[$key] === $find) {
$value = $replace;
return true;
}
}
return false;
}
if (isset($subject[$key]) && $subject[$key] === $find) {
$subject[$key] = $replace;
return true;
}
return false;
}
/**
* Find and Remove.
*
* Get array child by key and value match
*
* @param string $key
* @param mixed $find
* @param string $subject
*
* @return bool
*/
public function findAndRemove(string $key, $find, string $subject = ''): bool
{
$subject = &$this[$subject] ?? null;
$subject = (empty($subject)) ? $this : $subject;
if (is_array($subject)) {
foreach ($subject as $i => &$value) {
if (isset($value[$key]) && $value[$key] === $find) {
unset($subject[$i]);
return true;
}
}
return false;
}
if (isset($subject[$key]) && $subject[$key] === $find) {
unset($subject[$key]);
return true;
}
return false;
}
/**
* Checks if document has data.
*
* @return bool
*/
public function isEmpty(): bool
{
return !\count($this);
}
/**
* Checks if a document key is set.
*
* @param string $key
*
* @return bool
*/
public function isSet(string $key): bool
{
return isset($this[$key]);
}
/**
* Get Array Copy.
*
* Outputs entity as a PHP array
*
* @param array<string> $allow
* @param array<string> $disallow
*
* @return array<string, mixed>
*/
public function getArrayCopy(array $allow = [], array $disallow = []): array
{
$array = parent::getArrayCopy();
$output = [];
foreach ($array as $key => &$value) {
if (!empty($allow) && !\in_array($key, $allow)) { // Export only allow fields
continue;
}
if (!empty($disallow) && \in_array($key, $disallow)) { // Don't export disallowed fields
continue;
}
if ($value instanceof self) {
$output[$key] = $value->getArrayCopy($allow, $disallow);
} elseif (\is_array($value)) {
foreach ($value as $childKey => &$child) {
if ($child instanceof self) {
$output[$key][$childKey] = $child->getArrayCopy($allow, $disallow);
} else {
$output[$key][$childKey] = $child;
}
}
if (empty($value)) {
$output[$key] = $value;
}
} else {
$output[$key] = $value;
}
}
return $output;
}
public function __clone()
{
foreach ($this as $key => $value) {
if ($value instanceof self) {
$this[$key] = clone $value;
} elseif (\is_array($value)) {
$this[$key] = \array_map(fn ($item) => $item instanceof self ? clone $item : $item, $value);
}
}
}
}