Skip to content

Commit e0cc3f0

Browse files
committed
implement simple filter and sort for search-queries
1 parent 7f9a4b7 commit e0cc3f0

File tree

1 file changed

+48
-22
lines changed

1 file changed

+48
-22
lines changed

src/Endpoints/Search.php

Lines changed: 48 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -15,36 +15,33 @@
1515
class Search extends Endpoint
1616
{
1717
private string $searchText;
18-
private Collection $filter;
19-
private Collection $sorts;
18+
private ?string $filter = null;
19+
private ?Sorting $sort = null;
2020

2121

2222
public function __construct(Notion $notion, string $searchText = "")
2323
{
24-
$this->sorts = new Collection();
25-
$this->filter = new Collection();
2624
$this->searchText = $searchText;
27-
2825
parent::__construct($notion);
2926
}
3027

3128
public function query(): Collection
3229
{
3330
$postData = [];
3431

35-
// if ($this->sorts->isNotEmpty())
36-
// $postData["sort"] = Sorting::sortQuery($this->sorts);
32+
if ($this->sort !== null)
33+
$postData["sort"] = $this->sort->toArray();
3734

38-
// if ($this->filter->isNotEmpty())
39-
// $postData["filter"]["or"] = Filter::filterQuery($this->filter); // TODO Compound filters!
35+
if ($this->filter !== null)
36+
$postData["filter"] = ['property' => 'object', 'value' => $this->filter];
4037

4138
if ($this->startCursor !== null)
4239
$postData["start_cursor"] = $this->startCursor;
4340

4441
if ($this->pageSize !== null)
4542
$postData["page_size"] = $this->pageSize;
4643

47-
if($this->searchText !== null)
44+
if ($this->searchText !== null)
4845
$postData['query'] = $this->searchText;
4946

5047

@@ -54,23 +51,52 @@ public function query(): Collection
5451
$this->url(Endpoint::SEARCH),
5552
$postData
5653
)
57-
5854
->json();
5955

6056
$pageCollection = new EntityCollection($response);
6157

6258
return $pageCollection->getResults();
6359
}
6460

65-
// public function filterBy(Collection $filter)
66-
// {
67-
// $this->filter = $filter;
68-
// return $this;
69-
// }
70-
71-
// public function sortBy(Collection $sorts)
72-
// {
73-
// $this->sorts = $sorts;
74-
// return $this;
75-
// }
61+
public function sortByLastEditedTime(string $direction = "ascending"): Search
62+
{
63+
$this->sort = Sorting::timestampSort("last_edited_time", $direction);
64+
return $this;
65+
}
66+
67+
public function onlyDatabases() : Search
68+
{
69+
$this->filter = "database";
70+
return $this;
71+
}
72+
73+
public function onlyPages() : Search
74+
{
75+
$this->filter = "page";
76+
return $this;
77+
}
78+
79+
public function getTitles()
80+
{
81+
$titleCollection = new Collection();
82+
$results = $this->query();
83+
84+
foreach ($results as $result) {
85+
$titleCollection->add($result->getTitle());
86+
}
87+
88+
return $titleCollection;
89+
}
90+
91+
public function filterBy(string $filter)
92+
{
93+
$this->filter = $filter;
94+
return $this;
95+
}
96+
97+
public function sortBy(Sorting $sort)
98+
{
99+
$this->sort = $sort;
100+
return $this;
101+
}
76102
}

0 commit comments

Comments
 (0)