|
| 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\Entity; |
| 8 | +use FiveamCode\LaravelNotionApi\Entities\NotionParent; |
| 9 | +use FiveamCode\LaravelNotionApi\Entities\Page; |
| 10 | +use FiveamCode\LaravelNotionApi\Entities\Properties\Relation; |
| 11 | +use FiveamCode\LaravelNotionApi\Entities\User; |
| 12 | +use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; |
| 13 | +use FiveamCode\LaravelNotionApi\Exceptions\NotionException; |
| 14 | +use FiveamCode\LaravelNotionApi\Notion; |
| 15 | +use FiveamCode\LaravelNotionApi\Traits\HasParent; |
| 16 | +use Illuminate\Support\Collection; |
| 17 | + |
| 18 | +/** |
| 19 | + * Class Resolve. |
| 20 | + */ |
| 21 | +class Resolve extends Endpoint |
| 22 | +{ |
| 23 | + /** |
| 24 | + * Block constructor. |
| 25 | + * |
| 26 | + * @param Notion $notion |
| 27 | + * |
| 28 | + * @throws HandlingException |
| 29 | + * @throws \FiveamCode\LaravelNotionApi\Exceptions\LaravelNotionAPIException |
| 30 | + */ |
| 31 | + public function __construct(Notion $notion) |
| 32 | + { |
| 33 | + parent::__construct($notion); |
| 34 | + } |
| 35 | + |
| 36 | + /** |
| 37 | + * Resolve User. |
| 38 | + * |
| 39 | + * @param User $user |
| 40 | + * @return User |
| 41 | + * |
| 42 | + * @throws HandlingException |
| 43 | + * @throws NotionException |
| 44 | + */ |
| 45 | + public function user(User $user): User |
| 46 | + { |
| 47 | + return $this->notion->users()->find($user->getId()); |
| 48 | + } |
| 49 | + |
| 50 | + /** |
| 51 | + * Resolve Parent of an entity. |
| 52 | + * |
| 53 | + * @param Entity $entity |
| 54 | + * @return Page|Database|Block |
| 55 | + * |
| 56 | + * @throws HandlingException |
| 57 | + * @throws NotionException |
| 58 | + */ |
| 59 | + public function parentOf(Entity $entity) |
| 60 | + { |
| 61 | + if (! in_array(HasParent::class, class_uses_recursive(get_class($entity)))) { |
| 62 | + throw new HandlingException("The given entity '{$entity->getObjectType()}' does not have a parent."); |
| 63 | + } |
| 64 | + |
| 65 | + return $this->parent($entity->getParent()); |
| 66 | + } |
| 67 | + |
| 68 | + /** |
| 69 | + * Resolve Parent. |
| 70 | + * |
| 71 | + * @param NotionParent $parent |
| 72 | + * @return Page|Database|Block |
| 73 | + * |
| 74 | + * @throws HandlingException |
| 75 | + * @throws NotionException |
| 76 | + */ |
| 77 | + public function parent(NotionParent $parent): Page|Database|Block|NotionParent |
| 78 | + { |
| 79 | + switch ($parent->getObjectType()) { |
| 80 | + case 'page_id': |
| 81 | + return $this->notion->pages()->find($parent->getId()); |
| 82 | + case 'database_id': |
| 83 | + return $this->notion->databases()->find($parent->getId()); |
| 84 | + case 'block_id': |
| 85 | + return $this->notion->block($parent->getId())->retrieve(); |
| 86 | + case 'workspace': |
| 87 | + return $parent; |
| 88 | + // throw new HandlingException('A Notion Workspace cannot be resolved by the Notion API.'); |
| 89 | + default: |
| 90 | + throw new HandlingException('Unknown parent type while resolving the notion parent'); |
| 91 | + } |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Resolve Relations. |
| 96 | + * |
| 97 | + * @param Relation $relation |
| 98 | + * @return Collection<Page> |
| 99 | + * |
| 100 | + * @throws HandlingException |
| 101 | + * @throws NotionException |
| 102 | + */ |
| 103 | + public function relations(Relation $relation, bool $onlyTitles = false): Collection |
| 104 | + { |
| 105 | + $pages = collect(); |
| 106 | + $relationIds = $relation->getRelation()->map(function ($o) { |
| 107 | + return $o['id']; |
| 108 | + }); |
| 109 | + |
| 110 | + foreach ($relationIds as $relationId) { |
| 111 | + if ($onlyTitles) { |
| 112 | + $pages->add($this->notion->pages()->find($relationId)->getTitle()); |
| 113 | + } else { |
| 114 | + $pages->add($this->notion->pages()->find($relationId)); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + return $pages; |
| 119 | + } |
| 120 | +} |
0 commit comments