Skip to content

Commit b72001b

Browse files
committed
Merge branch 'dev' into tests/basic-functionality
2 parents 3b5c55c + 8a493ac commit b72001b

File tree

9 files changed

+159
-17
lines changed

9 files changed

+159
-17
lines changed

src/Endpoints/Search.php

Lines changed: 103 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,111 @@
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 ?string $filter = null;
19+
private ?Sorting $sort = null;
920

10-
// toDo: Think about it. 🧐
11-
}
21+
22+
public function __construct(Notion $notion, string $searchText = "")
23+
{
24+
$this->searchText = $searchText;
25+
parent::__construct($notion);
26+
}
27+
28+
public function query(): Collection
29+
{
30+
$postData = [];
31+
32+
if ($this->sort !== null)
33+
$postData["sort"] = $this->sort->toArray();
34+
35+
if ($this->filter !== null)
36+
$postData["filter"] = ['property' => 'object', 'value' => $this->filter];
37+
38+
if ($this->startCursor !== null)
39+
$postData["start_cursor"] = $this->startCursor;
40+
41+
if ($this->pageSize !== null)
42+
$postData["page_size"] = $this->pageSize;
43+
44+
if ($this->searchText !== null)
45+
$postData['query'] = $this->searchText;
46+
47+
48+
49+
$response = $this
50+
->post(
51+
$this->url(Endpoint::SEARCH),
52+
$postData
53+
)
54+
->json();
55+
56+
$pageCollection = new EntityCollection($response);
57+
58+
return $pageCollection->getResults();
59+
}
60+
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 getIds(){
92+
$idCollection = new Collection();
93+
$results = $this->query();
94+
95+
foreach ($results as $result) {
96+
$idCollection->add($result->getId());
97+
}
98+
return $idCollection;
99+
}
100+
101+
public function filterBy(string $filter)
102+
{
103+
$this->filter = $filter;
104+
return $this;
105+
}
106+
107+
public function sortBy(Sorting $sort)
108+
{
109+
$this->sort = $sort;
110+
return $this;
111+
}
112+
}

src/Entities/Collections/BlockCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class BlockCollection extends EntityCollection
1313
{
14-
protected function collectChildren()
14+
protected function collectChildren() : void
1515
{
1616
$this->collection = new Collection();
1717
foreach ($this->rawResults as $blockChild) {

src/Entities/Collections/DatabaseCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class DatabaseCollection extends EntityCollection
1313
{
14-
protected function collectChildren()
14+
protected function collectChildren() : void
1515
{
1616
$this->collection = new Collection();
1717
foreach ($this->rawResults as $databaseChild) {

src/Entities/Collections/EntityCollection.php

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

33
namespace FiveamCode\LaravelNotionApi\Entities\Collections;
44

5-
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
6-
use FiveamCode\LaravelNotionApi\Notion;
5+
76
use Illuminate\Support\Arr;
87
use Illuminate\Support\Collection;
8+
use FiveamCode\LaravelNotionApi\Entities\Page;
9+
use FiveamCode\LaravelNotionApi\Entities\Database;
10+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
911

1012

11-
//TODO:WORK IN PROGRESS
12-
abstract class EntityCollection
13+
class EntityCollection
1314
{
1415
protected array $responseData = [];
1516
protected array $rawResults = [];
@@ -20,8 +21,6 @@ public function __construct(array $reponseData = null)
2021
$this->setResponseData($reponseData);
2122
}
2223

23-
protected abstract function collectChildren();
24-
2524
protected function setResponseData(array $reponseData): void
2625
{
2726
if (!Arr::exists($reponseData, 'object')) throw HandlingException::instance("invalid json-array: no object given");
@@ -33,6 +32,17 @@ protected function setResponseData(array $reponseData): void
3332
$this->collectChildren();
3433
}
3534

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

src/Entities/Collections/PageCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class PageCollection extends EntityCollection
1313
{
14-
protected function collectChildren()
14+
protected function collectChildren() : void
1515
{
1616
$this->collection = new Collection();
1717
foreach ($this->rawResults as $pageChild) {

src/Entities/Collections/UserCollection.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111

1212
class UserCollection extends EntityCollection
1313
{
14-
protected function collectChildren()
14+
protected function collectChildren() : void
1515
{
1616
$this->collection = new Collection();
1717
foreach ($this->rawResults as $userChild) {

src/Entities/Database.php

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
class Database extends Entity
1212
{
1313
protected string $title = "";
14+
protected string $objectType = "";
1415
protected array $rawTitle = [];
1516
protected array $rawProperties = [];
1617
protected DateTime $createdTime;
@@ -29,26 +30,40 @@ private function fillFromRaw()
2930
{
3031
$this->fillId();
3132
$this->fillTitle();
33+
$this->fillObjectType();
3234
$this->fillProperties();
3335
$this->fillCreatedTime();
3436
$this->fillLastEditedTime();
3537
}
3638

37-
private function fillTitle() : void
39+
private function fillTitle(): void
3840
{
3941
if (Arr::exists($this->responseData, 'title') && is_array($this->responseData['title'])) {
4042
$this->title = Arr::first($this->responseData['title'], null, ['plain_text' => ''])['plain_text'];
4143
$this->rawTitle = $this->responseData['title'];
4244
}
4345
}
4446

45-
private function fillProperties() : void
47+
private function fillObjectType(): void
48+
{
49+
if (Arr::exists($this->responseData, 'object')) {
50+
$this->objectType = $this->responseData['object'];
51+
}
52+
}
53+
54+
private function fillProperties(): void
4655
{
4756
if (Arr::exists($this->responseData, 'properties')) {
4857
$this->rawProperties = $this->responseData['properties'];
4958
}
5059
}
5160

61+
public function getObjectType(): string
62+
{
63+
return $this->objectType;
64+
}
65+
66+
5267
public function getTitle(): string
5368
{
5469
return $this->title;

src/Entities/Page.php

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
class Page extends Entity
1313
{
1414
protected string $title = "";
15+
protected string $objectType = "";
1516
protected array $rawProperties = [];
1617
protected Collection $propertyCollection;
1718
protected DateTime $createdTime;
@@ -28,12 +29,20 @@ protected function setResponseData(array $responseData): void
2829
private function fillFromRaw(): void
2930
{
3031
$this->fillId();
32+
$this->fillObjectType();
3133
$this->fillProperties();
32-
$this->fillTitle();
34+
$this->fillTitle(); //!Warning: call after 'fillProperties', since title is included within properties
3335
$this->fillCreatedTime();
3436
$this->fillLastEditedTime();
3537
}
3638

39+
private function fillObjectType(): void
40+
{
41+
if (Arr::exists($this->responseData, 'object')) {
42+
$this->objectType = $this->responseData['object'];
43+
}
44+
}
45+
3746
private function fillProperties(): void
3847
{
3948
if (Arr::exists($this->responseData, 'properties')) {
@@ -45,6 +54,7 @@ private function fillProperties(): void
4554
}
4655
}
4756

57+
4858
private function fillTitle(): void
4959
{
5060
$titleProperty = $this->propertyCollection->filter(function ($property) {
@@ -81,6 +91,11 @@ public function getProperty(string $propertyName): ?Property
8191
return $property;
8292
}
8393

94+
public function getObjectType(): string
95+
{
96+
return $this->objectType;
97+
}
98+
8499
public function getRawProperties(): array
85100
{
86101
return $this->rawProperties;

src/Notion.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ public function users(): Users
142142
/**
143143
* @return Search
144144
*/
145-
public function search(): Search
145+
public function search(?string $searchText = ""): Search
146146
{
147-
return new Search($this);
147+
$search = new Search($this, $searchText);
148+
return $search;
148149
}
149150

150151
/**

0 commit comments

Comments
 (0)