-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathEntityCollection.php
182 lines (160 loc) · 4.46 KB
/
EntityCollection.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
<?php
namespace FiveamCode\LaravelNotionApi\Entities\Collections;
use FiveamCode\LaravelNotionApi\Entities\Database;
use FiveamCode\LaravelNotionApi\Entities\Entity;
use FiveamCode\LaravelNotionApi\Entities\Page;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
use FiveamCode\LaravelNotionApi\Query\StartCursor;
use Illuminate\Support\Arr;
use Illuminate\Support\Collection;
/**
* Class EntityCollection.
*/
class EntityCollection
{
/**
* @var array
*/
protected array $responseData = [];
/**
* @var array
*/
protected array $rawResults = [];
/**
* @var bool
*/
protected bool $hasMore = false;
/**
* @var string
*/
protected ?string $nextCursor = null;
/**
* @var Collection
*/
protected Collection $collection;
/**
* EntityCollection constructor.
*
* @param array|null $responseData
*
* @throws HandlingException
* @throws NotionException
*/
public function __construct(array $responseData = null)
{
$this->setResponseData($responseData);
}
/**
* @param array $responseData
*
* @throws HandlingException
* @throws NotionException
*/
protected function setResponseData(array $responseData): void
{
// TODO
// Currently, the API returns not-found objects with status code 200 -
// so we have to check here on the given status code in the paylaod,
// if the object was not found.
if (
array_key_exists('object', $responseData)
&& $responseData['object'] === 'error'
&& Arr::exists($responseData, 'status') && $responseData['status'] === 404
) {
throw NotionException::instance('Not found', compact('responseData'));
}
if (! Arr::exists($responseData, 'object')) {
throw HandlingException::instance('invalid json-array: no object given');
}
if (! Arr::exists($responseData, 'results')) {
throw HandlingException::instance('invalid json-array: no results given');
}
if ($responseData['object'] !== 'list') {
throw HandlingException::instance('invalid json-array: the given object is not a list');
}
$this->responseData = $responseData;
$this->fillFromRaw();
$this->collectChildren();
}
protected function collectChildren(): void
{
$this->collection = new Collection();
foreach ($this->rawResults as $pageChild) {
if (Arr::exists($pageChild, 'object')) {
if ($pageChild['object'] == 'page') {
$this->collection->add(new Page($pageChild));
}
if ($pageChild['object'] == 'database') {
$this->collection->add(new Database($pageChild));
}
}
}
}
protected function fillFromRaw()
{
$this->fillResult();
$this->fillCursorInformation();
}
protected function fillResult()
{
$this->rawResults = $this->responseData['results'];
}
protected function fillCursorInformation()
{
if (Arr::exists($this->responseData, 'has_more')) {
$this->hasMore = $this->responseData['has_more'];
}
if (Arr::exists($this->responseData, 'next_cursor')) {
$this->nextCursor = $this->responseData['next_cursor'];
}
}
/**
* @return array
*/
public function getRawResponse(): array
{
return $this->responseData;
}
/**
* @return string
*/
public function getRawNextCursor(): ?string
{
return $this->nextCursor;
}
/**
* @return Collection
*/
public function asCollection(): Collection
{
return $this->collection;
}
/**
* @return string
*/
public function asJson(): string
{
return $this->asCollection()->map(function (Entity $item) {
return $item->toArray();
});
}
/**
* @return bool
*/
public function hasMoreEntries(): bool
{
return $this->hasMore;
}
/**
* @return StartCursor
*/
public function nextCursor(): ?StartCursor
{
$rawNextCursor = $this->getRawNextCursor();
if ($rawNextCursor === null) {
return null;
}
return new StartCursor($rawNextCursor);
}
}