Skip to content

Commit 00d7dca

Browse files
committed
added asJson
1 parent b56a06d commit 00d7dca

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/Endpoints/Search.php

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public function __construct(Notion $notion, string $searchText = "")
2525
parent::__construct($notion);
2626
}
2727

28-
public function query(): Collection
28+
public function query(): EntityCollection
2929
{
3030
$postData = [];
3131

@@ -43,19 +43,15 @@ public function query(): Collection
4343

4444
if ($this->searchText !== null)
4545
$postData['query'] = $this->searchText;
46-
47-
48-
46+
4947
$response = $this
5048
->post(
5149
$this->url(Endpoint::SEARCH),
5250
$postData
5351
)
5452
->json();
5553

56-
$pageCollection = new EntityCollection($response);
57-
58-
return $pageCollection->getResults();
54+
return new EntityCollection($response);
5955
}
6056

6157
public function sortByLastEditedTime(string $direction = "ascending"): Search

src/Entities/Collections/EntityCollection.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace FiveamCode\LaravelNotionApi\Entities\Collections;
44

55

6+
use FiveamCode\LaravelNotionApi\Entities\Entity;
67
use Illuminate\Support\Arr;
78
use Illuminate\Support\Collection;
89
use FiveamCode\LaravelNotionApi\Entities\Page;
@@ -64,8 +65,14 @@ public function getRawResults(): array
6465
return $this->rawResults;
6566
}
6667

67-
public function getResults(): Collection
68+
public function getResults(): Collection // toDo: rename asCollection()
6869
{
6970
return $this->collection;
7071
}
72+
73+
public function asJson(): string {
74+
return $this->collection->map(function (Entity $item) {
75+
return $item->toArray();
76+
});
77+
}
7178
}

src/Entities/Entity.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,4 +68,8 @@ public function getRaw(): array
6868
{
6969
return $this->responseData;
7070
}
71+
72+
public function toArray(): array {
73+
return get_object_vars($this);
74+
}
7175
}

0 commit comments

Comments
 (0)