|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* tubee |
|
7
|
|
|
* |
|
8
|
|
|
* @copyright Copryright (c) 2017-2019 gyselroth GmbH (https://gyselroth.com) |
|
9
|
|
|
* @license GPL-3.0 https://opensource.org/licenses/GPL-3.0 |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Tubee; |
|
13
|
|
|
|
|
14
|
|
|
use Generator; |
|
15
|
|
|
use MongoDB\BSON\ObjectIdInterface; |
|
16
|
|
|
use Psr\Http\Message\ServerRequestInterface; |
|
17
|
|
|
use Tubee\Collection\CollectionInterface; |
|
18
|
|
|
use Tubee\DataObject\DataObjectInterface; |
|
19
|
|
|
use Tubee\DataObjectRelation\Factory as DataObjectRelationFactory; |
|
20
|
|
|
use Tubee\Resource\AbstractResource; |
|
21
|
|
|
use Tubee\Resource\AttributeResolver; |
|
22
|
|
|
|
|
23
|
|
|
class DataObject extends AbstractResource implements DataObjectInterface |
|
24
|
|
|
{ |
|
25
|
|
|
/** |
|
26
|
|
|
* Datatype. |
|
27
|
|
|
* |
|
28
|
|
|
* @var CollectionInterface |
|
29
|
|
|
*/ |
|
30
|
|
|
protected $collection; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Data object relation factory. |
|
34
|
|
|
* |
|
35
|
|
|
* @var DataObjectRelationFactory |
|
36
|
|
|
*/ |
|
37
|
|
|
protected $relation_factory; |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Relations. |
|
41
|
|
|
* |
|
42
|
|
|
* @var null|array |
|
43
|
|
|
*/ |
|
44
|
|
|
protected $relations; |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Data object. |
|
48
|
|
|
*/ |
|
49
|
|
|
public function __construct(array $resource, CollectionInterface $collection, DataObjectRelationFactory $relation_factory) |
|
50
|
|
|
{ |
|
51
|
|
|
$this->resource = $resource; |
|
52
|
|
|
$this->collection = $collection; |
|
53
|
|
|
$this->relation_factory = $relation_factory; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritdoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function decorate(ServerRequestInterface $request): array |
|
60
|
|
|
{ |
|
61
|
|
|
$collection = $this->collection->getName(); |
|
62
|
|
|
$namespace = $this->collection->getResourceNamespace()->getName(); |
|
63
|
|
|
|
|
64
|
|
|
$resource = [ |
|
65
|
|
|
'_links' => [ |
|
66
|
|
|
'namespace' => ['href' => (string) $request->getUri()->withPath('/api/v1/namespaces/'.$namespace)], |
|
67
|
|
|
'collection' => ['href' => (string) $request->getUri()->withPath('/api/v1/namespaces/'.$namespace.'/collections/'.$collection)], |
|
68
|
|
|
], |
|
69
|
|
|
'kind' => 'DataObject', |
|
70
|
|
|
'namespace' => $namespace, |
|
71
|
|
|
'collection' => $collection, |
|
72
|
|
|
'data' => $this->getData(), |
|
73
|
|
|
'status' => function ($object) { |
|
74
|
|
|
$endpoints = $object->getEndpoints(); |
|
75
|
|
|
foreach ($endpoints as &$endpoint) { |
|
76
|
|
|
$endpoint['last_sync'] = $endpoint['last_sync']->toDateTime()->format('c'); |
|
77
|
|
|
$endpoint['last_successful_sync'] = isset($endpoint['last_successful_sync']) ? $endpoint['last_successful_sync']->toDateTime()->format('c') : null; |
|
78
|
|
|
$endpoint['success'] = isset($endpoint['success']) ? $endpoint['success'] : null; |
|
79
|
|
|
$endpoint['garbage'] = isset($endpoint['garbage']) ? $endpoint['garbage'] : false; |
|
80
|
|
|
$endpoint['result'] = isset($endpoint['result']) ? $endpoint['result'] : null; |
|
81
|
|
|
$endpoint['exception'] = isset($endpoint['exception']) ? $endpoint['exception'] : null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
|
|
return ['endpoints' => $endpoints]; |
|
85
|
|
|
}, |
|
86
|
|
|
]; |
|
87
|
|
|
|
|
88
|
|
|
return AttributeResolver::resolve($request, $this, $resource); |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* {@inheritdoc} |
|
93
|
|
|
*/ |
|
94
|
|
|
public function getHistory(?array $query = null, ?int $offset = null, ?int $limit = null): iterable |
|
95
|
|
|
{ |
|
96
|
|
|
return $this->collection->getObjectHistory($this->getId(), $query, $offset, $limit); |
|
|
|
|
|
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* {@inheritdoc} |
|
101
|
|
|
*/ |
|
102
|
|
|
public function toArray(): array |
|
103
|
|
|
{ |
|
104
|
|
|
$resource = $this->resource; |
|
105
|
|
|
$resource['namespace'] = $this->collection->getResourceNamespace()->getName(); |
|
106
|
|
|
$resource['collection'] = $this->collection->getName(); |
|
107
|
|
|
|
|
108
|
|
|
return $resource; |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* {@inheritdoc} |
|
113
|
|
|
*/ |
|
114
|
|
|
public function getCollection(): CollectionInterface |
|
115
|
|
|
{ |
|
116
|
|
|
return $this->collection; |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* Get endpoints. |
|
121
|
|
|
*/ |
|
122
|
|
|
public function getEndpoints(): array |
|
123
|
|
|
{ |
|
124
|
|
|
if (!isset($this->resource['endpoints'])) { |
|
125
|
|
|
return []; |
|
126
|
|
|
} |
|
127
|
|
|
|
|
128
|
|
|
return $this->resource['endpoints']; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Add or update relation. |
|
133
|
|
|
*/ |
|
134
|
|
|
public function createOrUpdateRelation(DataObjectInterface $object, array $context = [], bool $simulate = false, ?array $endpoints = null): ObjectIdInterface |
|
135
|
|
|
{ |
|
136
|
|
|
return $this->relation_factory->createOrUpdate($this, $object, $context, $simulate, $endpoints); |
|
137
|
|
|
} |
|
138
|
|
|
|
|
139
|
|
|
/** |
|
140
|
|
|
* Delete relation. |
|
141
|
|
|
*/ |
|
142
|
|
|
public function deleteRelation(DataObjectInterface $object, bool $simulate = false): bool |
|
143
|
|
|
{ |
|
144
|
|
|
return $this->relation_factory->deleteFromObject($this, $object, $simulate); |
|
145
|
|
|
} |
|
146
|
|
|
|
|
147
|
|
|
/** |
|
148
|
|
|
* Get relations as array (and cache). |
|
149
|
|
|
*/ |
|
150
|
|
|
public function getResolvedRelationsAsArray(): array |
|
151
|
|
|
{ |
|
152
|
|
|
if ($this->relations === null) { |
|
153
|
|
|
$this->relations = []; |
|
154
|
|
|
foreach ($this->getRelations() as $relation) { |
|
155
|
|
|
$resource = $relation->toArray(); |
|
156
|
|
|
$resource['object'] = $relation->getDataObject()->toArray(); |
|
157
|
|
|
$this->relations[] = $resource; |
|
158
|
|
|
} |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
return $this->relations; |
|
162
|
|
|
} |
|
163
|
|
|
|
|
164
|
|
|
/** |
|
165
|
|
|
* Get relatives. |
|
166
|
|
|
*/ |
|
167
|
|
|
public function getRelations(): Generator |
|
168
|
|
|
{ |
|
169
|
|
|
return $this->relation_factory->getAllFromObject($this); |
|
170
|
|
|
} |
|
171
|
|
|
|
|
172
|
|
|
/** |
|
173
|
|
|
* Get relative. |
|
174
|
|
|
*/ |
|
175
|
|
|
public function getRelation(string $name): Generator |
|
176
|
|
|
{ |
|
177
|
|
|
return $this->relation_factory->getOneFromObject($this, $name); |
|
178
|
|
|
} |
|
179
|
|
|
} |
|
180
|
|
|
|
This check marks calls to methods that do not seem to exist on an object.
This is most likely the result of a method being renamed without all references to it being renamed likewise.