Skip to content

Commit 27f4fe9

Browse files
committed
Merge branch 'dev' into feature/resolve
2 parents 31bd869 + b3bb528 commit 27f4fe9

File tree

13 files changed

+157
-49
lines changed

13 files changed

+157
-49
lines changed

src/Endpoints/Comments.php

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
namespace FiveamCode\LaravelNotionApi\Endpoints;
44

55
use FiveamCode\LaravelNotionApi\Entities\Collections\CommentCollection;
6-
use FiveamCode\LaravelNotionApi\Entities\Comment;
6+
use FiveamCode\LaravelNotionApi\Entities\Comment as CommentEntity;
77
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
88
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
99
use FiveamCode\LaravelNotionApi\Notion;
@@ -38,10 +38,11 @@ public function __construct(Notion $notion)
3838
}
3939

4040
/**
41-
* Retrieve block children
41+
* Retrieve a list of comments
4242
* url: https://api.notion.com/{version}/comments?block_id=* [get]
4343
* notion-api-docs: https://developers.notion.com/reference/retrieve-a-comment.
4444
*
45+
* @param string $blockId
4546
* @return CommentCollection
4647
*
4748
* @throws HandlingException
@@ -63,7 +64,7 @@ public function ofBlock(string $blockId): CommentCollection
6364
public function onDiscussion(string $discussionId): self
6465
{
6566
if ($this->pageId !== null) {
66-
throw new HandlingException('You can only use ``->onDiscussion(...)`` or ``->onPage(...)``.');
67+
throw new HandlingException('You can only use `onDiscussion()` or `onPage()`.');
6768
}
6869

6970
$this->discussionId = $discussionId;
@@ -78,18 +79,29 @@ public function onDiscussion(string $discussionId): self
7879
public function onPage(string $pageId): self
7980
{
8081
if ($this->discussionId !== null) {
81-
throw new HandlingException('You can only use ``->onDiscussion(...)`` or ``->onPage(...)``.');
82+
throw new HandlingException('You can only use `onDiscussion()` or `onPage()`.');
8283
}
8384

8485
$this->pageId = $pageId;
8586

8687
return $this;
8788
}
8889

89-
public function create($comment): Comment
90+
/**
91+
* Create a comment
92+
* url: https://api.notion.com/{version}/comments [post]
93+
* notion-api-docs: https://developers.notion.com/reference/create-a-comment.
94+
*
95+
* @param CommentEntity $comment
96+
* @return CommentEntity
97+
*
98+
* @throws HandlingException
99+
* @throws NotionException
100+
*/
101+
public function create($comment): CommentEntity
90102
{
91103
if ($this->discussionId === null && $this->pageId === null) {
92-
throw new HandlingException('You must use ``->onDiscussion(...)`` or ``->onPage(...)``.');
104+
throw new HandlingException('You must use `onDiscussion()` or `onPage()`.');
93105
}
94106

95107
$body = $comment->getRawResponse();
@@ -106,6 +118,6 @@ public function create($comment): Comment
106118
$body
107119
);
108120

109-
return new Comment($response->json());
121+
return new CommentEntity($response->json());
110122
}
111123
}

src/Endpoints/Database.php

Lines changed: 38 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use FiveamCode\LaravelNotionApi\Notion;
99
use FiveamCode\LaravelNotionApi\Query\Filters\Filter;
1010
use FiveamCode\LaravelNotionApi\Query\Filters\FilterBag;
11+
use FiveamCode\LaravelNotionApi\Query\Filters\Operators;
1112
use FiveamCode\LaravelNotionApi\Query\Sorting;
1213
use Illuminate\Support\Collection;
1314

@@ -60,6 +61,18 @@ public function __construct(string $databaseId, Notion $notion)
6061
* @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException
6162
*/
6263
public function query(): PageCollection
64+
{
65+
$response = $this
66+
->post(
67+
$this->url(Endpoint::DATABASES."/{$this->databaseId}/query"),
68+
$this->getPostData()
69+
)
70+
->json();
71+
72+
return new PageCollection($response);
73+
}
74+
75+
public function getPostData(): array
6376
{
6477
$postData = [];
6578

@@ -81,28 +94,20 @@ public function query(): PageCollection
8194
$postData['page_size'] = $this->pageSize;
8295
}
8396

84-
$response = $this
85-
->post(
86-
$this->url(Endpoint::DATABASES."/{$this->databaseId}/query"),
87-
$postData
88-
)
89-
->json();
90-
91-
return new PageCollection($response);
97+
return $postData;
9298
}
9399

94100
/**
95101
* @param $filter
96102
* @return Database $this
97103
*
98104
* @throws HandlingException
99-
*
100-
* @todo As soon as this package drops PHP 7.4 support, we can use union types here (FilterBag and Filter)
101105
*/
102-
public function filterBy($filter): Database // TODO that's a breaking change
106+
public function filterBy(Collection|Filter|FilterBag $filter): Database
103107
{
104-
$this->checkFilterType($filter);
105-
108+
if ($filter instanceof Collection) {
109+
return $this->filterByCollection($filter);
110+
}
106111
if ($filter instanceof FilterBag) {
107112
return $this->filterByBag($filter);
108113
}
@@ -113,6 +118,12 @@ public function filterBy($filter): Database // TODO that's a breaking change
113118
return $this;
114119
}
115120

121+
/**
122+
* @param Filter $filter
123+
* @return $this
124+
*
125+
* @throws HandlingException
126+
*/
116127
public function filterBySingleFilter(Filter $filter): Database
117128
{
118129
$this->filter = $filter;
@@ -123,7 +134,7 @@ public function filterBySingleFilter(Filter $filter): Database
123134

124135
/**
125136
* @param FilterBag $filterBag
126-
* @return $this
137+
* @return Database $this
127138
*/
128139
public function filterByBag(FilterBag $filterBag): Database
129140
{
@@ -133,6 +144,18 @@ public function filterByBag(FilterBag $filterBag): Database
133144
return $this;
134145
}
135146

147+
/**
148+
* @param Collection $filterCollection
149+
* @return Database $this
150+
*/
151+
public function filterByCollection(Collection $filterCollection): Database
152+
{
153+
$filterBag = new FilterBag(Operators::OR);
154+
$filterBag->addFilters($filterCollection);
155+
156+
return $this->filterByBag($filterBag);
157+
}
158+
136159
/**
137160
* @param Collection|Sorting $sorts
138161
* @return Database $this
@@ -150,7 +173,7 @@ public function sortBy(Sorting|Collection $sorts): Database
150173
$this->sorts = $sorts;
151174
break;
152175
default:
153-
throw new HandlingException("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of Sortings.");
176+
throw new HandlingException("The parameter 'sorts' must be either a instance of the class Sorting or a Collection of sortings.");
154177
}
155178

156179
return $this;
@@ -166,11 +189,4 @@ public function offsetByResponse(EntityCollection $entityCollection): Database
166189

167190
return $this;
168191
}
169-
170-
private function checkFilterType($filter): void
171-
{
172-
if (! ($filter instanceof Filter || $filter instanceof FilterBag)) {
173-
throw new HandlingException('Please provide either a filter bag or a single filter.');
174-
}
175-
}
176192
}

src/Entities/Blocks/Block.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,13 +59,10 @@ protected function setResponseData(array $responseData): void
5959

6060
protected function fillFromRaw(): void
6161
{
62-
parent::fillEntityBase();
62+
parent::fillEssentials();
6363
$this->fillType();
6464
$this->fillRawContent();
6565
$this->fillHasChildren();
66-
$this->fillParentAttributes();
67-
$this->fillArchivedAttributes();
68-
$this->fillTimestampableAttributes();
6966
}
7067

7168
private function fillType(): void

src/Entities/Comment.php

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,9 @@ protected function setResponseData(array $responseData): void
7777

7878
private function fillFromRaw(): void
7979
{
80-
parent::fillEntityBase();
80+
parent::fillEssentials();
8181
$this->fillRichText();
8282
$this->fillDiscussionId();
83-
$this->fillParentAttributes();
84-
$this->fillTimestampableAttributes();
8583
}
8684

8785
private function fillDiscussionId(): void

src/Entities/Database.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,17 +99,14 @@ protected function setResponseData(array $responseData): void
9999

100100
private function fillFromRaw()
101101
{
102-
parent::fillEntityBase();
102+
parent::fillEssentials();
103103
$this->fillIcon();
104104
$this->fillCover();
105105
$this->fillTitle();
106106
$this->fillIsInline();
107107
$this->fillDescription();
108108
$this->fillProperties();
109109
$this->fillDatabaseUrl();
110-
$this->fillParentAttributes();
111-
$this->fillArchivedAttributes();
112-
$this->fillTimestampableAttributes();
113110
}
114111

115112
private function fillTitle(): void

src/Entities/Entity.php

Lines changed: 39 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,10 +72,48 @@ protected function setResponseData(array $responseData): void
7272
$this->responseData = $responseData;
7373
}
7474

75-
protected function fillEntityBase(): void
75+
protected function fillEssentials(): void
7676
{
7777
$this->fillId();
7878
$this->fillObjectType();
79+
$this->fillTraitAttributes();
80+
}
81+
82+
private function fillTraitAttributes(): void
83+
{
84+
$traitMapping = [
85+
'FiveamCode\LaravelNotionApi\Traits\HasTimestamps' => function ($entity) {
86+
$entity->fillTimestampableAttributes();
87+
},
88+
'FiveamCode\LaravelNotionApi\Traits\HasParent' => function ($entity) {
89+
$entity->fillParentAttributes();
90+
},
91+
'FiveamCode\LaravelNotionApi\Traits\HasArchive' => function ($entity) {
92+
$entity->fillArchivedAttributes();
93+
},
94+
];
95+
96+
$traits = $this->class_uses_deep($this);
97+
foreach ($traits as $trait) {
98+
if (Arr::exists($traitMapping, $trait)) {
99+
$traitMapping[$trait]($this);
100+
}
101+
}
102+
}
103+
104+
private function class_uses_deep($class, $autoload = true)
105+
{
106+
$traits = [];
107+
108+
do {
109+
$traits = array_merge(class_uses($class, $autoload), $traits);
110+
} while ($class = get_parent_class($class));
111+
112+
foreach ($traits as $trait => $same) {
113+
$traits = array_merge(class_uses($trait, $autoload), $traits);
114+
}
115+
116+
return array_unique($traits);
79117
}
80118

81119
private function fillId()

src/Entities/Page.php

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -111,15 +111,12 @@ protected function setResponseData(array $responseData): void
111111

112112
private function fillFromRaw(): void
113113
{
114-
parent::fillEntityBase();
114+
parent::fillEssentials();
115115
$this->fillProperties();
116116
$this->fillTitle(); // This has to be called after fillProperties(), since title is provided by properties
117117
$this->fillPageUrl();
118118
$this->fillIcon();
119119
$this->fillCover();
120-
$this->fillParentAttributes();
121-
$this->fillArchivedAttributes();
122-
$this->fillTimestampableAttributes();
123120
}
124121

125122
/**

src/Entities/Properties/Property.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected function setResponseData(array $responseData): void
6262

6363
protected function fillFromRaw(): void
6464
{
65-
parent::fillEntityBase();
65+
parent::fillEssentials();
6666
$this->fillType();
6767
$this->fillContent();
6868
}

src/Entities/PropertyItems/SelectItem.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ protected function setResponseData(array $responseData): void
3636

3737
protected function fillFromRaw(): void
3838
{
39-
parent::fillEntityBase();
39+
parent::fillEssentials();
4040
$this->fillName();
4141
$this->fillColor();
4242
}

src/Entities/User.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ protected function setResponseData(array $responseData): void
3737

3838
private function fillFromRaw(): void
3939
{
40-
parent::fillEntityBase();
40+
parent::fillEssentials();
4141
$this->fillName();
4242
$this->fillAvatarUrl();
4343
}

0 commit comments

Comments
 (0)