-
-
Notifications
You must be signed in to change notification settings - Fork 50
add the ability to resolve users and parents #116
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all 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 31bd869
Apply fixes from StyleCI (#115)
mechelon 27f4fe9
Merge branch 'dev' into feature/resolve
johguentner 9a3dbb3
fix: modify changed method within `NotionParent`
johguentner 3c9c845
add prototypical relation resolving
johguentner 29c991a
Apply fixes from StyleCI (#124)
mechelon 68cb62f
Merge branch 'dev' into feature/resolve
johguentner 1e4f0ff
Merge branch 'dev' into feature/resolve
johguentner 246396f
polish phpdocs of comment endpoint
johguentner d27f4ff
Apply fixes from StyleCI (#145)
mechelon 9a3730b
polish: add newline to improve readability
johguentner 69e15bd
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner 55fe958
Apply fixes from StyleCI (#146)
mechelon 2b11b70
add `parentOf` to access parents easily
johguentner a82ebeb
Apply fixes from StyleCI (#147)
mechelon eb7b39b
polish `PestHttpRecorder::class`
johguentner c83c9b1
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner cd8116d
Apply fixes from StyleCI (#148)
mechelon e73207a
add tests for `Notion::resolve()`; polish record
johguentner 13a560d
build snapshots for resolve and
johguentner 1c27736
Merge branch 'feature/resolve' of https://github.com/5am-code/laravel…
johguentner 44626bb
Apply fixes from StyleCI (#152)
mechelon b01fc99
add simple phpdbg code coverage (cmd)
johguentner 0cbd9de
add additional tests for resolve endpoint
johguentner 1f6aa11
Apply fixes from StyleCI (#153)
mechelon f3aa549
fix type for workspace in `NotionParent::class`
johguentner 7b7395b
add resolve/parent tests regarding NotionParent
johguentner 016009e
add snapshot for parent testing
johguentner f945728
Apply fixes from StyleCI (#155)
mechelon File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,5 @@ vendor | |
.phpunit.result.cache | ||
coverage/ | ||
.phpunit.cache/ | ||
.env* | ||
.env* | ||
coverage-report |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
<?php | ||
|
||
namespace FiveamCode\LaravelNotionApi\Endpoints; | ||
|
||
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block; | ||
use FiveamCode\LaravelNotionApi\Entities\Database; | ||
use FiveamCode\LaravelNotionApi\Entities\Entity; | ||
use FiveamCode\LaravelNotionApi\Entities\NotionParent; | ||
use FiveamCode\LaravelNotionApi\Entities\Page; | ||
use FiveamCode\LaravelNotionApi\Entities\Properties\Relation; | ||
use FiveamCode\LaravelNotionApi\Entities\User; | ||
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; | ||
use FiveamCode\LaravelNotionApi\Exceptions\NotionException; | ||
use FiveamCode\LaravelNotionApi\Notion; | ||
use FiveamCode\LaravelNotionApi\Traits\HasParent; | ||
use Illuminate\Support\Collection; | ||
|
||
/** | ||
* 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); | ||
} | ||
|
||
/** | ||
* Resolve User. | ||
* | ||
* @param User $user | ||
* @return User | ||
* | ||
* @throws HandlingException | ||
* @throws NotionException | ||
*/ | ||
public function user(User $user): User | ||
{ | ||
return $this->notion->users()->find($user->getId()); | ||
} | ||
|
||
/** | ||
* Resolve Parent of an entity. | ||
* | ||
* @param Entity $entity | ||
* @return Page|Database|Block | ||
* | ||
* @throws HandlingException | ||
* @throws NotionException | ||
*/ | ||
public function parentOf(Entity $entity) | ||
{ | ||
if (! in_array(HasParent::class, class_uses_recursive(get_class($entity)))) { | ||
throw new HandlingException("The given entity '{$entity->getObjectType()}' does not have a parent."); | ||
} | ||
|
||
return $this->parent($entity->getParent()); | ||
} | ||
|
||
/** | ||
* Resolve Parent. | ||
* | ||
* @param NotionParent $parent | ||
* @return Page|Database|Block | ||
* | ||
* @throws HandlingException | ||
* @throws NotionException | ||
*/ | ||
public function parent(NotionParent $parent): Page|Database|Block|NotionParent | ||
{ | ||
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': | ||
return $parent; | ||
// 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'); | ||
} | ||
} | ||
|
||
/** | ||
* Resolve Relations. | ||
* | ||
* @param Relation $relation | ||
* @return Collection<Page> | ||
* | ||
* @throws HandlingException | ||
* @throws NotionException | ||
*/ | ||
public function relations(Relation $relation, bool $onlyTitles = false): Collection | ||
{ | ||
$pages = collect(); | ||
$relationIds = $relation->getRelation()->map(function ($o) { | ||
return $o['id']; | ||
}); | ||
|
||
foreach ($relationIds as $relationId) { | ||
if ($onlyTitles) { | ||
$pages->add($this->notion->pages()->find($relationId)->getTitle()); | ||
} else { | ||
$pages->add($this->notion->pages()->find($relationId)); | ||
} | ||
} | ||
|
||
return $pages; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
<?php | ||
|
||
namespace FiveamCode\LaravelNotionApi\Entities; | ||
|
||
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; | ||
|
||
/** | ||
* Class NotionParent. | ||
*/ | ||
class NotionParent extends Entity | ||
johguentner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
/** | ||
* @param array $responseData | ||
* | ||
* @throws HandlingException | ||
* @throws \FiveamCode\LaravelNotionApi\Exceptions\NotionException | ||
johguentner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
*/ | ||
protected function setResponseData(array $responseData): void | ||
{ | ||
parent::setResponseData($responseData); | ||
johguentner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
if ( | ||
$responseData['object'] !== 'page_id' | ||
&& $responseData['object'] !== 'database_id' | ||
&& $responseData['object'] !== 'workspace' | ||
&& $responseData['object'] !== 'block_id' | ||
) { | ||
throw HandlingException::instance('invalid json-array: the given object is not a valid parent'); | ||
} | ||
|
||
$this->fillFromRaw(); | ||
johguentner marked this conversation as resolved.
Show resolved
Hide resolved
|
||
} | ||
|
||
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'; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.