Skip to content

Commit 7f9a4b7

Browse files
committed
add prototypical search-endpoint, allow general EntityCollections
implement collectChildren of EntityCollection as general method for collecting diverse entities at the same time (needed for search). currently implemented for object-types 'page' and 'database'.
1 parent b9454e7 commit 7f9a4b7

File tree

3 files changed

+85
-8
lines changed

3 files changed

+85
-8
lines changed

src/Endpoints/Search.php

Lines changed: 67 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,75 @@
22

33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

5+
use FiveamCode\LaravelNotionApi\Entities\Collections\EntityCollection;
6+
use FiveamCode\LaravelNotionApi\Entities\Collections\PageCollection;
7+
use Illuminate\Support\Collection;
58
use FiveamCode\LaravelNotionApi\Notion;
9+
use FiveamCode\LaravelNotionApi\Query\Filter;
10+
use FiveamCode\LaravelNotionApi\Query\Sorting;
11+
use FiveamCode\LaravelNotionApi\Query\StartCursor;
12+
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
13+
use Symfony\Component\VarDumper\Cloner\Data;
614

715
class Search extends Endpoint
816
{
17+
private string $searchText;
18+
private Collection $filter;
19+
private Collection $sorts;
920

10-
// toDo: Think about it. 🧐
11-
}
21+
22+
public function __construct(Notion $notion, string $searchText = "")
23+
{
24+
$this->sorts = new Collection();
25+
$this->filter = new Collection();
26+
$this->searchText = $searchText;
27+
28+
parent::__construct($notion);
29+
}
30+
31+
public function query(): Collection
32+
{
33+
$postData = [];
34+
35+
// if ($this->sorts->isNotEmpty())
36+
// $postData["sort"] = Sorting::sortQuery($this->sorts);
37+
38+
// if ($this->filter->isNotEmpty())
39+
// $postData["filter"]["or"] = Filter::filterQuery($this->filter); // TODO Compound filters!
40+
41+
if ($this->startCursor !== null)
42+
$postData["start_cursor"] = $this->startCursor;
43+
44+
if ($this->pageSize !== null)
45+
$postData["page_size"] = $this->pageSize;
46+
47+
if($this->searchText !== null)
48+
$postData['query'] = $this->searchText;
49+
50+
51+
52+
$response = $this
53+
->post(
54+
$this->url(Endpoint::SEARCH),
55+
$postData
56+
)
57+
58+
->json();
59+
60+
$pageCollection = new EntityCollection($response);
61+
62+
return $pageCollection->getResults();
63+
}
64+
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+
// }
76+
}

src/Entities/Collections/EntityCollection.php

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,16 @@
22

33
namespace FiveamCode\LaravelNotionApi\Entities\Collections;
44

5+
use FiveamCode\LaravelNotionApi\Entities\Database;
6+
use FiveamCode\LaravelNotionApi\Entities\Entity;
7+
use FiveamCode\LaravelNotionApi\Entities\Page;
58
use FiveamCode\LaravelNotionApi\Exceptions\WrapperException;
69
use FiveamCode\LaravelNotionApi\Notion;
710
use Illuminate\Support\Arr;
811
use Illuminate\Support\Collection;
912

1013

11-
//TODO:WORK IN PROGRESS
12-
abstract class EntityCollection
14+
class EntityCollection
1315
{
1416
protected array $responseData = [];
1517
protected array $rawResults = [];
@@ -20,8 +22,6 @@ public function __construct(array $reponseData = null)
2022
$this->setResponseData($reponseData);
2123
}
2224

23-
protected abstract function collectChildren();
24-
2525
protected function setResponseData(array $reponseData): void
2626
{
2727
if (!Arr::exists($reponseData, 'object')) throw WrapperException::instance("invalid json-array: no object given");
@@ -33,6 +33,17 @@ protected function setResponseData(array $reponseData): void
3333
$this->collectChildren();
3434
}
3535

36+
protected function collectChildren(): void
37+
{
38+
$this->collection = new Collection();
39+
foreach ($this->rawResults as $pageChild) {
40+
if (Arr::exists($pageChild, 'object')) {
41+
if($pageChild['object'] == 'page') $this->collection->add(new Page($pageChild));
42+
if($pageChild['object'] == 'database') $this->collection->add(new Database($pageChild));
43+
}
44+
}
45+
}
46+
3647
protected function fillFromRaw()
3748
{
3849
$this->fillResult();

src/Notion.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,9 +134,10 @@ public function users(): Users
134134
/**
135135
* @return Search
136136
*/
137-
public function search(): Search
137+
public function search(?string $searchText = ""): Search
138138
{
139-
return new Search($this);
139+
$search = new Search($this, $searchText);
140+
return $search;
140141
}
141142

142143
/**

0 commit comments

Comments
 (0)