Skip to content

Commit 2b11b70

Browse files
committed
add parentOf to access parents easily
1 parent 55fe958 commit 2b11b70

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/Endpoints/Resolve.php

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,15 @@
44

55
use FiveamCode\LaravelNotionApi\Entities\Blocks\Block;
66
use FiveamCode\LaravelNotionApi\Entities\Database;
7+
use FiveamCode\LaravelNotionApi\Entities\Entity;
78
use FiveamCode\LaravelNotionApi\Entities\NotionParent;
89
use FiveamCode\LaravelNotionApi\Entities\Page;
910
use FiveamCode\LaravelNotionApi\Entities\Properties\Relation;
1011
use FiveamCode\LaravelNotionApi\Entities\User;
1112
use FiveamCode\LaravelNotionApi\Exceptions\HandlingException;
1213
use FiveamCode\LaravelNotionApi\Exceptions\NotionException;
1314
use FiveamCode\LaravelNotionApi\Notion;
15+
use FiveamCode\LaravelNotionApi\Traits\HasParent;
1416
use Illuminate\Support\Collection;
1517

1618
/**
@@ -32,6 +34,8 @@ public function __construct(Notion $notion)
3234
}
3335

3436
/**
37+
* Resolve User
38+
*
3539
* @param User $user
3640
* @return User
3741
*
@@ -44,6 +48,26 @@ public function user(User $user): User
4448
}
4549

4650
/**
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+
*
4771
* @param NotionParent $parent
4872
* @return Page|Database|Block
4973
*
@@ -67,6 +91,8 @@ public function parent(NotionParent $parent): Page|Database|Block
6791
}
6892

6993
/**
94+
* Resolve Relations
95+
*
7096
* @param Relation $relation
7197
* @return Collection<Page>
7298
*

0 commit comments

Comments
 (0)