-
-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathDatabase.php
192 lines (162 loc) · 4.94 KB
/
Database.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
<?php
namespace FiveamCode\LaravelNotionApi\Endpoints;
use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection;
use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
use FiveamCode\LaravelNotionApi\Notion;
use FiveamCode\LaravelNotionApi\Query\Filters\Filter;
use FiveamCode\LaravelNotionApi\Query\Filters\FilterBag;
use FiveamCode\LaravelNotionApi\Query\Filters\Operators;
use FiveamCode\LaravelNotionApi\Query\Sorting;
use Illuminate\Support\Collection;
/**
* Class Database.
*/
class Database extends Endpoint
{
/**
* @var string
*/
private string $databaseId;
/**
* @var Filter|null
*/
private ?Filter $filter = null; // TODO breaking change as well
private $filterBag;
private array $filterData = [];
/**
* @var Collection
*/
private Collection $sorts;
/**
* Database constructor.
*
* @param string $databaseId
* @param Notion $notion
*
* @throws \FiveamCode\LaravelNotionApi\Exceptions\HandlingException
* @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException
*/
public function __construct(string $databaseId, Notion $notion)
{
$this->databaseId = $databaseId;
$this->sorts = new Collection();
parent::__construct($notion);
}
/**
* @return PageCollection
*
* @throws \FiveamCode\LaravelNotionApi\Exceptions\HandlingException
* @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException
*/
public function query(): PageCollection
{
$response = $this
->post(
$this->url(Endpoint::DATABASES."/{$this->databaseId}/query"),
$this->getPostData()
)
->json();
return new PageCollection($response);
}
public function getPostData(): array
{
$postData = [];
if ($this->sorts->isNotEmpty()) {
$postData['sorts'] = Sorting::sortQuery($this->sorts);
}
if ($this->filter !== null && ! is_null($this->filterBag)) {
throw new HandlingException('Please provide either a filter bag or a single filter.');
} elseif ($this->filter !== null || ! is_null($this->filterBag)) {
$postData['filter'] = $this->filterData;
}
if ($this->startCursor !== null) {
$postData['start_cursor'] = $this->startCursor->__toString();
}
if ($this->pageSize !== null) {
$postData['page_size'] = $this->pageSize;
}
return $postData;
}
/**
* @param $filter
* @return Database $this
*
* @throws HandlingException
*/
public function filterBy(Collection|Filter|FilterBag $filter): Database
{
if ($filter instanceof Collection) {
return $this->filterByCollection($filter);
}
if ($filter instanceof FilterBag) {
return $this->filterByBag($filter);
}
if ($filter instanceof Filter) {
return $this->filterBySingleFilter($filter);
}
return $this;
}
/**
* @param Filter $filter
* @return $this
*
* @throws HandlingException
*/
public function filterBySingleFilter(Filter $filter): Database
{
$this->filter = $filter;
$this->filterData = ['or' => [$filter->toQuery()]];
return $this;
}
/**
* @param FilterBag $filterBag
* @return Database $this
*/
public function filterByBag(FilterBag $filterBag): Database
{
$this->filterBag = $filterBag;
$this->filterData = $filterBag->toQuery();
return $this;
}
/**
* @param Collection $filterCollection
* @return Database $this
*/
public function filterByCollection(Collection $filterCollection): Database
{
$filterBag = new FilterBag(Operators::OR);
$filterBag->addFilters($filterCollection);
return $this->filterByBag($filterBag);
}
/**
* @param Collection|Sorting $sorts
* @return Database $this
*
* @throws HandlingException
*/
public function sortBy(Sorting|Collection $sorts): Database
{
$sortInstance = get_class($sorts);
switch ($sortInstance) {
case Sorting::class:
$this->sorts->push($sorts);
break;
case Collection::class:
$this->sorts = $sorts;
break;
default:
throw new HandlingException("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of sortings.");
}
return $this;
}
/**
* @param EntityCollection $entityCollection
* @return $this
*/
public function offsetByResponse(EntityCollection $entityCollection): Database
{
$this->offset($entityCollection->nextCursor());
return $this;
}
}