Skip to content

Commit c3d4b48

Browse files
committed
add the ability to resolve users and parents
- some entities have parents or expose user-ids, without additional information - by resolving these within the "endpoint" (not a real notion enpoint) ``Resolve::class``, the additional information can be easily obtained
1 parent 4ef3d6e commit c3d4b48

File tree

4 files changed

+156
-1
lines changed

4 files changed

+156
-1
lines changed

src/Endpoints/Resolve.php

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Endpoints;
4+
5+
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
6+
use FiveamCode\LaravelNotionApi\Entities\Collections\CommentCollection;
7+
use FiveamCode\LaravelNotionApi\Entities\Comment;
8+
use FiveamCode\LaravelNotionApi\Entities\Database;
9+
use FiveamCode\LaravelNotionApi\Entities\NotionParent;
10+
use FiveamCode\LaravelNotionApi\Entities\Page;
11+
use FiveamCode\LaravelNotionApi\Entities\Properties\Relation;
12+
use FiveamCode\LaravelNotionApi\Entities\User;
13+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
14+
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
15+
use FiveamCode\LaravelNotionApi\Notion;
16+
17+
/**
18+
* Class Resolve.
19+
*/
20+
class Resolve extends Endpoint
21+
{
22+
/**
23+
* Block constructor.
24+
*
25+
* @param Notion $notion
26+
*
27+
* @throws HandlingException
28+
* @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException
29+
*/
30+
public function __construct(Notion $notion)
31+
{
32+
parent::__construct($notion);
33+
}
34+
35+
/**
36+
* @param User $user
37+
*
38+
* @return User
39+
* @throws HandlingException
40+
* @throws NotionException
41+
*/
42+
public function user(User $user): User
43+
{
44+
return $this->notion->users()->find($user->getId());
45+
}
46+
47+
/**
48+
* @param NotionParent $parent
49+
*
50+
* @return Page|Database|Block
51+
* @throws HandlingException
52+
* @throws NotionException
53+
*/
54+
public function parent(NotionParent $parent): Page|Database|Block
55+
{
56+
switch ($parent->getObjectType()) {
57+
case 'page_id':
58+
return $this->notion->pages()->find($parent->getId());
59+
case 'database_id':
60+
return $this->notion->databases()->find($parent->getId());
61+
case 'block_id':
62+
return $this->notion->block($parent->getId())->retrieve();
63+
case 'workspace_id':
64+
throw new HandlingException('A Notion Workspace cannot be resolved by the Notion API.');
65+
default:
66+
throw new HandlingException('Unknown parent type while resolving the notion parent');
67+
}
68+
}
69+
}

src/Entities/NotionParent.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
namespace FiveamCode\LaravelNotionApi\Entities;
4+
5+
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
6+
7+
/**
8+
* Class NotionParent.
9+
*/
10+
class NotionParent extends Entity
11+
{
12+
/**
13+
* @param array $responseData
14+
*
15+
* @throws HandlingException
16+
* @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException
17+
*/
18+
protected function setResponseData(array $responseData): void
19+
{
20+
parent::setResponseData($responseData);
21+
if (
22+
$responseData['object'] !== 'page_id'
23+
&& $responseData['object'] !== 'database_id'
24+
&& $responseData['object'] !== 'workspace_id'
25+
&& $responseData['object'] !== 'block_id'
26+
) {
27+
throw HandlingException::instance('invalid json-array: the given object is not a valid parent');
28+
}
29+
$this->fillFromRaw();
30+
}
31+
32+
private function fillFromRaw(): void
33+
{
34+
parent::fillEntityBase();
35+
}
36+
37+
/**
38+
* @return bool
39+
*/
40+
public function isBlock(): bool
41+
{
42+
return $this->getObjectType() === 'block_id';
43+
}
44+
45+
/**
46+
* @return bool
47+
*/
48+
public function isPage(): bool
49+
{
50+
return $this->getObjectType() === 'page_id';
51+
}
52+
53+
/**
54+
* @return bool
55+
*/
56+
public function isDatabase(): bool
57+
{
58+
return $this->getObjectType() === 'database_id';
59+
}
60+
61+
/**
62+
* @return bool
63+
*/
64+
public function isWorkspace(): bool
65+
{
66+
return $this->getObjectType() === 'workspace_id';
67+
}
68+
}

src/Notion.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
use FiveamCode\LaravelNotionApi\Endpoints\Databases;
99
use FiveamCode\LaravelNotionApi\Endpoints\Endpoint;
1010
use FiveamCode\LaravelNotionApi\Endpoints\Pages;
11+
use FiveamCode\LaravelNotionApi\Endpoints\Resolve;
1112
use FiveamCode\LaravelNotionApi\Endpoints\Search;
1213
use FiveamCode\LaravelNotionApi\Endpoints\Users;
1314
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
@@ -196,6 +197,11 @@ public function comments(): Comments
196197
return new Comments($this);
197198
}
198199

200+
public function resolve(): Resolve
201+
{
202+
return new Resolve($this);
203+
}
204+
199205
/**
200206
* @return string
201207
*/
@@ -221,7 +227,7 @@ public function getConnection(): ?PendingRequest
221227
*/
222228
public function checkValidVersion(string $version): void
223229
{
224-
if (! $this->validVersions->contains($version)) {
230+
if (!$this->validVersions->contains($version)) {
225231
throw HandlingException::instance('Invalid version for Notion-API endpoint', ['invalidVersion' => $version]);
226232
}
227233
}

src/Traits/HasParent.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace FiveamCode\LaravelNotionApi\Traits;
44

5+
use FiveamCode\LaravelNotionApi\Entities\NotionParent;
56
use Illuminate\Support\Arr;
67

78
/**
@@ -54,4 +55,15 @@ public function getParentType(): string
5455
{
5556
return $this->parentType;
5657
}
58+
59+
/**
60+
* @return NotionParent
61+
*/
62+
public function getParent()
63+
{
64+
return new NotionParent([
65+
'id' => $this->getParentId(),
66+
'object' => $this->getParentType()
67+
]);
68+
}
5769
}

0 commit comments

Comments
 (0)