Skip to content
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
c3d4b48
add the ability to resolve users and parents
johguentner Feb 6, 2023
31bd869
Apply fixes from StyleCI (#115)
mechelon Feb 6, 2023
27f4fe9
Merge branch 'dev' into feature/resolve
johguentner Feb 6, 2023
9a3dbb3
fix: modify changed method within `NotionParent`
johguentner Feb 6, 2023
3c9c845
add prototypical relation resolving
johguentner Feb 16, 2023
29c991a
Apply fixes from StyleCI (#124)
mechelon Feb 16, 2023
68cb62f
Merge branch 'dev' into feature/resolve
johguentner Apr 30, 2023
1e4f0ff
Merge branch 'dev' into feature/resolve
johguentner May 2, 2023
246396f
polish phpdocs of comment endpoint
johguentner May 2, 2023
d27f4ff
Apply fixes from StyleCI (#145)
mechelon May 2, 2023
9a3730b
polish: add newline to improve readability
johguentner May 2, 2023
69e15bd
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner May 2, 2023
55fe958
Apply fixes from StyleCI (#146)
mechelon May 2, 2023
2b11b70
add `parentOf` to access parents easily
johguentner May 2, 2023
a82ebeb
Apply fixes from StyleCI (#147)
mechelon May 2, 2023
eb7b39b
polish `PestHttpRecorder::class`
johguentner Apr 30, 2023
c83c9b1
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner May 3, 2023
cd8116d
Apply fixes from StyleCI (#148)
mechelon May 3, 2023
e73207a
add tests for `Notion::resolve()`; polish record
johguentner Jun 9, 2023
13a560d
build snapshots for resolve and
johguentner Jun 9, 2023
1c27736
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner Jun 9, 2023
44626bb
Apply fixes from StyleCI (#152)
mechelon Jun 9, 2023
b01fc99
add simple phpdbg code coverage (cmd)
johguentner Jun 10, 2023
0cbd9de
add additional tests for resolve endpoint
johguentner Jun 10, 2023
1f6aa11
Apply fixes from StyleCI (#153)
mechelon Jun 10, 2023
f3aa549
fix type for workspace in `NotionParent::class`
johguentner Jun 10, 2023
7b7395b
add resolve/parent tests regarding NotionParent
johguentner Jun 10, 2023
016009e
add snapshot for parent testing
johguentner Jun 10, 2023
f945728
Apply fixes from StyleCI (#155)
mechelon Jun 10, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 66 additions & 0 deletions src/Endpoints/Resolve.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace FiveamCode\LaravelNotionApi\Endpoints;

use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
use FiveamCode\LaravelNotionApi\Entities\Database;
use FiveamCode\LaravelNotionApi\Entities\NotionParent;
use FiveamCode\LaravelNotionApi\Entities\Page;
use FiveamCode\LaravelNotionApi\Entities\User;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
use FiveamCode\LaravelNotionApi\Notion;

/**
* Class Resolve.
*/
class Resolve extends Endpoint
{
/**
* Block constructor.
*
* @param Notion $notion
*
* @throws HandlingException
* @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException
*/
public function __construct(Notion $notion)
{
parent::__construct($notion);
}

/**
* @param User $user
* @return User
*
* @throws HandlingException
* @throws NotionException
*/
public function user(User $user): User
{
return $this->notion->users()->find($user->getId());
}

/**
* @param NotionParent $parent
* @return Page|Database|Block
*
* @throws HandlingException
* @throws NotionException
*/
public function parent(NotionParent $parent): Page|Database|Block
{
switch ($parent->getObjectType()) {
case 'page_id':
return $this->notion->pages()->find($parent->getId());
case 'database_id':
return $this->notion->databases()->find($parent->getId());
case 'block_id':
return $this->notion->block($parent->getId())->retrieve();
case 'workspace_id':
throw new HandlingException('A Notion Workspace cannot be resolved by the Notion API.');
default:
throw new HandlingException('Unknown parent type while resolving the notion parent');
}
}
}
68 changes: 68 additions & 0 deletions src/Entities/NotionParent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

namespace FiveamCode\LaravelNotionApi\Entities;

use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;

/**
* Class NotionParent.
*/
class NotionParent extends Entity
{
/**
* @param array $responseData
*
* @throws HandlingException
* @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException
*/
protected function setResponseData(array $responseData): void
{
parent::setResponseData($responseData);
if (
$responseData['object'] !== 'page_id'
&& $responseData['object'] !== 'database_id'
&& $responseData['object'] !== 'workspace_id'
&& $responseData['object'] !== 'block_id'
) {
throw HandlingException::instance('invalid json-array: the given object is not a valid parent');
}
$this->fillFromRaw();
}

private function fillFromRaw(): void
{
parent::fillEssentials();
}

/**
* @return bool
*/
public function isBlock(): bool
{
return $this->getObjectType() === 'block_id';
}

/**
* @return bool
*/
public function isPage(): bool
{
return $this->getObjectType() === 'page_id';
}

/**
* @return bool
*/
public function isDatabase(): bool
{
return $this->getObjectType() === 'database_id';
}

/**
* @return bool
*/
public function isWorkspace(): bool
{
return $this->getObjectType() === 'workspace_id';
}
}
6 changes: 6 additions & 0 deletions src/Notion.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
use FiveamCode\LaravelNotionApi\Endpoints\Databases;
use FiveamCode\LaravelNotionApi\Endpoints\Endpoint;
use FiveamCode\LaravelNotionApi\Endpoints\Pages;
use FiveamCode\LaravelNotionApi\Endpoints\Resolve;
use FiveamCode\LaravelNotionApi\Endpoints\Search;
use FiveamCode\LaravelNotionApi\Endpoints\Users;
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
Expand Down Expand Up @@ -196,6 +197,11 @@ public function comments(): Comments
return new Comments($this);
}

public function resolve(): Resolve
{
return new Resolve($this);
}

/**
* @return string
*/
Expand Down
12 changes: 12 additions & 0 deletions src/Traits/HasParent.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace FiveamCode\LaravelNotionApi\Traits;

use FiveamCode\LaravelNotionApi\Entities\NotionParent;
use Illuminate\Support\Arr;

/**
Expand Down Expand Up @@ -54,4 +55,15 @@ public function getParentType(): string
{
return $this->parentType;
}

/**
* @return NotionParent
*/
public function getParent()
{
return new NotionParent([
'id' => $this->getParentId(),
'object' => $this->getParentType(),
]);
}
}