Skip to content

Commit 4de936a

Browse files
committed
added basic filter structure
1 parent d7c11de commit 4de936a

File tree

3 files changed

+97
-26
lines changed

3 files changed

+97
-26
lines changed

src/Endpoints/Database.php

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -30,50 +30,38 @@ public function __construct(string $databaseId, Notion $notion)
3030

3131
public function query(): array
3232
{
33-
34-
$filterJson = '
35-
{
36-
"property": "Tags",
37-
"multi_select": {
38-
"contains": "great"
39-
}
40-
}';
41-
42-
43-
$filter = json_decode($filterJson);
44-
4533
if ($this->sorts->isNotEmpty())
4634
$postData["sorts"] = Sorting::sortQuery($this->sorts);
4735

4836
if ($this->filter->isNotEmpty())
49-
$postData["filter"] = []; //Filter::filterQuery($this->filter);
37+
$postData["filter"]["or"] = Filter::filterQuery($this->filter); // TODO Compound filters!
5038

5139
if ($this->startCursor !== null)
5240
$postData["start_cursor"] = $this->startCursor;
5341

5442
if ($this->pageSize !== null)
5543
$postData["page_size"] = $this->pageSize;
5644

57-
$response = $this->post(
58-
$this->url(Endpoint::DATABASES . "/{$this->databaseId}/query"),
59-
$postData
60-
)
45+
$response = $this
46+
->post(
47+
$this->url(Endpoint::DATABASES . "/{$this->databaseId}/query"),
48+
$postData
49+
)
6150
->json();
6251

6352
// toDo return Database Entity
6453
dd($response);
6554
}
6655

67-
public function filterBy()
56+
public function filterBy(Collection $filter)
6857
{
69-
58+
$this->filter = $filter;
7059
return $this;
7160
}
7261

73-
public function sortBy(Collection $sortings)
62+
public function sortBy(Collection $sorts)
7463
{
75-
$this->sorts = $sortings;
76-
64+
$this->sorts = $sorts;
7765
return $this;
7866
}
7967
}

src/Query/CompoundFilter.php

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,15 @@
22

33
namespace FiveamCode\LaravelNotionApi\Query;
44

5+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
6+
57
class CompoundFilter extends QueryHelper
68
{
9+
// toDo
710

11+
public function __construct()
12+
{
13+
parent::__construct();
14+
throw new WrapperException("not implemented yet.");
15+
}
816
}

src/Query/Filter.php

Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,96 @@
22

33
namespace FiveamCode\LaravelNotionApi\Query;
44

5+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
6+
use Illuminate\Support\Collection;
7+
58
class Filter extends QueryHelper
69
{
10+
private ?string $filterType = null;
11+
private ?array $filterConditions = null;
12+
private ?array $filterDefinition = null;
13+
714

8-
public function __construct(string $property )
15+
public function __construct(
16+
string $property,
17+
string $filterType = null,
18+
array $filterConditions = null,
19+
array $filterDefinition = null
20+
)
921
{
1022
parent::__construct();
1123

1224
$this->property = $property;
25+
$this->filterType = $filterType;
26+
$this->filterConditions = $filterConditions;
27+
$this->filterDefinition = $filterDefinition;
28+
}
1329

30+
/**
31+
* Returns a text filter instance.
32+
*
33+
* @see https://developers.notion.com/reference/post-database-query#text-filter-condition
34+
*
35+
* @param string $property
36+
* @param array $filterConditions
37+
* @return Filter
38+
*/
39+
public static function textFilter(string $property, array $filterConditions): Filter
40+
{
41+
return new Filter($property, "text", $filterConditions);
42+
}
1443

44+
/**
45+
* This method allows you to define every filter that is offered
46+
* by Notion but not implemented in this package yet. Provide the
47+
* filter definition as an array like explained in the Notion docs.
48+
* Use with caution; this method will be removed in the future and
49+
* is marked as deprecated from the start!
50+
*
51+
* @see https://developers.notion.com/reference/post-database-query#post-database-query-filter
52+
*
53+
* @param string $property
54+
* @param array $filterDefinition
55+
*
56+
* @deprecated
57+
*/
58+
public static function rawFilter(string $property, array $filterDefinition): Filter
59+
{
60+
return new Filter($property, null, null, $filterDefinition);
1561
}
1662

17-
// public static function textFilter(string $property, ) {
18-
//
19-
// }
63+
public function toArray(): array
64+
{
65+
if ($this->filterDefinition !== null && $this->filterType === null && $this->filterConditions === null) {
66+
return array_merge(
67+
["property" => $this->property],
68+
$this->filterDefinition
69+
);
70+
}
71+
elseif ($this->filterType !== null && $this->filterConditions !== null && $this->filterDefinition === null) {
72+
return [
73+
"property" => $this->property,
74+
$this->filterType => $this->filterConditions
75+
];
76+
}
77+
else
78+
throw WrapperException::instance("Invalid filter definition.", ["invalidFilter" => $this]);
79+
80+
}
81+
82+
83+
public static function filterQuery(Collection $filter): array
84+
{
85+
86+
$queryFilter = new Collection();
87+
88+
$filter->each(function (Filter $filter) use ($queryFilter) {
89+
$queryFilter->add($filter->toArray());
90+
});
91+
92+
return $queryFilter->toArray();
93+
94+
}
2095

2196

2297
}

0 commit comments

Comments
 (0)