Skip to content

Commit 516ccc7

Browse files
committed
Merge branch 'feature/resolve' into feature/notion-models-and-commands
2 parents 99c2523 + 9a3dbb3 commit 516ccc7

File tree

4 files changed

+152
-0
lines changed

4 files changed

+152
-0
lines changed

src/Endpoints/Resolve.php

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

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::fillEssentials();
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: 6 additions & 0 deletions
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
*/

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)