From bef69a5f298fbe8bade38f33ea43fd2a3d4a0539 Mon Sep 17 00:00:00 2001 From: johguentner Date: Sat, 4 Feb 2023 18:21:05 +0100 Subject: [PATCH 01/21] refactor timestampables of entities into traid - move properties created_time, last_updated_time, etc. to its own trait - remove attributes and methods from entity classes (which are applied by traid) - apply traid to according entity classes (database, page and block) - add created_by, last_updated_by to this new trait --- src/Entities/Blocks/Block.php | 32 ++------- src/Entities/Database.php | 31 ++------ src/Entities/Entity.php | 14 +--- src/Entities/Page.php | 31 ++------ src/Traits/TimestampableEntity.php | 110 +++++++++++++++++++++++++++++ 5 files changed, 124 insertions(+), 94 deletions(-) create mode 100644 src/Traits/TimestampableEntity.php diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index 68fb58e..226d180 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -5,6 +5,7 @@ use DateTime; use FiveamCode\LaravelNotionApi\Entities\Entity; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Traits\TimestampableEntity; use Illuminate\Support\Arr; /** @@ -12,6 +13,8 @@ */ class Block extends Entity { + use TimestampableEntity; + /** * @var string */ @@ -37,16 +40,6 @@ class Block extends Entity */ protected string $text = '[warning: unsupported in notion api]'; - /** - * @var DateTime - */ - protected DateTime $createdTime; - - /** - * @var DateTime - */ - protected DateTime $lastEditedTime; - /** * @param array $responseData * @@ -69,8 +62,7 @@ protected function fillFromRaw(): void $this->fillType(); $this->fillRawContent(); $this->fillHasChildren(); - $this->fillCreatedTime(); - $this->fillLastEditedTime(); + $this->fillTimestampableProperties(); } private function fillType(): void @@ -126,22 +118,6 @@ public function hasChildren(): bool return $this->hasChildren; } - /** - * @return DateTime - */ - public function getCreatedTime(): DateTime - { - return $this->createdTime; - } - - /** - * @return DateTime - */ - public function getLastEditedTime(): DateTime - { - return $this->lastEditedTime; - } - public function getContent() { return $this->content; diff --git a/src/Entities/Database.php b/src/Entities/Database.php index 453c106..bba8a97 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -3,8 +3,10 @@ namespace FiveamCode\LaravelNotionApi\Entities; use DateTime; +use FiveamCode\LaravelNotionApi\Entities\Properties\People; use FiveamCode\LaravelNotionApi\Entities\Properties\Property; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Traits\TimestampableEntity; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -13,6 +15,8 @@ */ class Database extends Entity { + use TimestampableEntity; + /** * @var string */ @@ -73,15 +77,7 @@ class Database extends Entity */ protected Collection $properties; - /** - * @var DateTime - */ - protected DateTime $createdTime; - /** - * @var DateTime - */ - protected DateTime $lastEditedTime; protected function setResponseData(array $responseData): void { @@ -101,8 +97,7 @@ private function fillFromRaw() $this->fillObjectType(); $this->fillProperties(); $this->fillDatabaseUrl(); - $this->fillCreatedTime(); - $this->fillLastEditedTime(); + $this->fillTimestampableProperties(); } private function fillTitle(): void @@ -268,20 +263,4 @@ public function getPropertyKeys(): array { return $this->propertyKeys; } - - /** - * @return DateTime - */ - public function getCreatedTime(): DateTime - { - return $this->createdTime; - } - - /** - * @return array - */ - public function getLastEditedTime(): DateTime - { - return $this->lastEditedTime; - } } diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 9e571bd..1f28497 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -3,6 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Entities; use Carbon\Carbon; +use DateTime; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use Illuminate\Support\Arr; @@ -68,19 +69,6 @@ protected function setResponseData(array $responseData): void $this->responseData = $responseData; } - protected function fillCreatedTime() - { - if (Arr::exists($this->responseData, 'created_time')) { - $this->createdTime = new Carbon($this->responseData['created_time']); - } - } - - protected function fillLastEditedTime() - { - if (Arr::exists($this->responseData, 'last_edited_time')) { - $this->lastEditedTime = new Carbon($this->responseData['last_edited_time']); - } - } protected function fillId() { diff --git a/src/Entities/Page.php b/src/Entities/Page.php index 77e6609..4a236ee 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -17,6 +17,7 @@ use FiveamCode\LaravelNotionApi\Entities\Properties\Title; use FiveamCode\LaravelNotionApi\Entities\Properties\Url; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Traits\TimestampableEntity; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -25,6 +26,8 @@ */ class Page extends Entity { + use TimestampableEntity; + /** * @var string */ @@ -80,16 +83,6 @@ class Page extends Entity */ protected Collection $properties; - /** - * @var DateTime - */ - protected DateTime $createdTime; - - /** - * @var DateTime - */ - protected DateTime $lastEditedTime; - /** * Page constructor. * @@ -128,8 +121,7 @@ private function fillFromRaw(): void $this->fillPageUrl(); $this->fillIcon(); $this->fillCover(); - $this->fillCreatedTime(); - $this->fillLastEditedTime(); + $this->fillTimestampableProperties(); } private function fillObjectType(): void @@ -474,19 +466,4 @@ public function getPropertyKeys(): array return $this->propertyKeys; } - /** - * @return DateTime - */ - public function getCreatedTime(): DateTime - { - return $this->createdTime; - } - - /** - * @return DateTime - */ - public function getLastEditedTime(): DateTime - { - return $this->lastEditedTime; - } } diff --git a/src/Traits/TimestampableEntity.php b/src/Traits/TimestampableEntity.php new file mode 100644 index 0000000..e021c58 --- /dev/null +++ b/src/Traits/TimestampableEntity.php @@ -0,0 +1,110 @@ +fillCreatedTime(); + $this->fillLastEditedTime(); + $this->fillCreatedBy(); + $this->fillLastEditedBy(); + } + + protected function fillCreatedTime(): void + { + if (Arr::exists($this->responseData, 'created_time')) { + $this->createdTime = new Carbon($this->responseData['created_time']); + } + } + + protected function fillLastEditedTime(): void + { + if (Arr::exists($this->responseData, 'last_edited_time')) { + $this->lastEditedTime = new Carbon($this->responseData['last_edited_time']); + } + } + + protected function fillCreatedBy(): void + { + if (Arr::exists($this->responseData, 'created_by')) { + $this->createdBy = new User($this->responseData['created_by']); + } + } + + protected function fillLastEditedBy(): void + { + if (Arr::exists($this->responseData, 'last_edited_by')) { + $this->lastEditedBy = new User($this->responseData['last_edited_by']); + } + } + + /** + * @return DateTime + */ + public function getCreatedTime(): DateTime + { + return $this->createdTime; + } + + /** + * @return DateTime + */ + public function getLastEditedTime(): DateTime + { + return $this->lastEditedTime; + } + + /** + * @return User + */ + public function getCreatedBy(): User + { + return $this->createdBy; + } + + /** + * @return User + */ + public function getLastEditedBy(): User + { + return $this->lastEditedBy; + } +} From 0a982bb2450d8c17feb23980eafab9c658e50d77 Mon Sep 17 00:00:00 2001 From: Di Date: Sat, 4 Feb 2023 18:21:21 +0100 Subject: [PATCH 02/21] Apply fixes from StyleCI (#104) --- src/Entities/Blocks/Block.php | 1 - src/Entities/Database.php | 4 ---- src/Entities/Entity.php | 3 --- src/Entities/Page.php | 1 - src/Traits/TimestampableEntity.php | 5 +---- 5 files changed, 1 insertion(+), 13 deletions(-) diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index 226d180..d15538f 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -2,7 +2,6 @@ namespace FiveamCode\LaravelNotionApi\Entities\Blocks; -use DateTime; use FiveamCode\LaravelNotionApi\Entities\Entity; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Traits\TimestampableEntity; diff --git a/src/Entities/Database.php b/src/Entities/Database.php index bba8a97..e447107 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -2,8 +2,6 @@ namespace FiveamCode\LaravelNotionApi\Entities; -use DateTime; -use FiveamCode\LaravelNotionApi\Entities\Properties\People; use FiveamCode\LaravelNotionApi\Entities\Properties\Property; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Traits\TimestampableEntity; @@ -77,8 +75,6 @@ class Database extends Entity */ protected Collection $properties; - - protected function setResponseData(array $responseData): void { parent::setResponseData($responseData); diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 1f28497..5b02440 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -2,8 +2,6 @@ namespace FiveamCode\LaravelNotionApi\Entities; -use Carbon\Carbon; -use DateTime; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use Illuminate\Support\Arr; @@ -69,7 +67,6 @@ protected function setResponseData(array $responseData): void $this->responseData = $responseData; } - protected function fillId() { $this->id = $this->responseData['id']; diff --git a/src/Entities/Page.php b/src/Entities/Page.php index 4a236ee..eebc602 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -465,5 +465,4 @@ public function getPropertyKeys(): array { return $this->propertyKeys; } - } diff --git a/src/Traits/TimestampableEntity.php b/src/Traits/TimestampableEntity.php index e021c58..b148c70 100644 --- a/src/Traits/TimestampableEntity.php +++ b/src/Traits/TimestampableEntity.php @@ -4,17 +4,14 @@ use Carbon\Carbon; use DateTime; -use FiveamCode\LaravelNotionApi\Entities\Entity; use FiveamCode\LaravelNotionApi\Entities\User; use Illuminate\Support\Arr; /** - * Trait UpdatableEntity - * @package FiveamCode\LaravelNotionApi\Traits + * Trait UpdatableEntity. */ trait TimestampableEntity { - /** * @var array */ From 2b84fce1646c1dcae8fc718d097112ae0a82208d Mon Sep 17 00:00:00 2001 From: johguentner Date: Sun, 5 Feb 2023 11:14:21 +0100 Subject: [PATCH 03/21] refactor title and add description for db entity - refactor rawTitle into richTitle - add description and rawDescription (with according fill and getter) --- src/Entities/Database.php | 48 ++++++++++++++++++++++++++++++++++----- 1 file changed, 42 insertions(+), 6 deletions(-) diff --git a/src/Entities/Database.php b/src/Entities/Database.php index e447107..d7e64f7 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -3,6 +3,7 @@ namespace FiveamCode\LaravelNotionApi\Entities; use FiveamCode\LaravelNotionApi\Entities\Properties\Property; +use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use FiveamCode\LaravelNotionApi\Traits\TimestampableEntity; use Illuminate\Support\Arr; @@ -20,6 +21,11 @@ class Database extends Entity */ protected string $title = ''; + /** + * @var string + */ + protected string $description = ''; + /** * @var string */ @@ -51,9 +57,14 @@ class Database extends Entity protected string $objectType = ''; /** - * @var array + * @var ?RichText + */ + protected ?RichText $richTitle = null; + + /** + * @var ?RichText */ - protected array $rawTitle = []; + protected ?RichText $richDescription = null; /** * @var array @@ -90,6 +101,7 @@ private function fillFromRaw() $this->fillIcon(); $this->fillCover(); $this->fillTitle(); + $this->fillDescription(); $this->fillObjectType(); $this->fillProperties(); $this->fillDatabaseUrl(); @@ -100,7 +112,15 @@ private function fillTitle(): void { if (Arr::exists($this->responseData, 'title') && is_array($this->responseData['title'])) { $this->title = Arr::first($this->responseData['title'], null, ['plain_text' => ''])['plain_text']; - $this->rawTitle = $this->responseData['title']; + $this->richTitle = new RichText($this->responseData['title']); + } + } + + private function fillDescription(): void + { + if (Arr::exists($this->responseData, 'description') && is_array($this->responseData['description'])) { + $this->description = Arr::first($this->responseData['description'], null, ['plain_text' => ''])['plain_text']; + $this->richDescription = new RichText($this->responseData['description']); } } @@ -188,6 +208,14 @@ public function getTitle(): string return $this->title; } + /** + * @return string + */ + public function getDescription(): string + { + return $this->description; + } + /** * @return string */ @@ -237,11 +265,19 @@ public function getProperties(): Collection } /** - * @return array + * @return ?RichText + */ + public function getRichTitle(): ?RichText + { + return $this->richTitle; + } + + /** + * @return ?RichText */ - public function getRawTitle(): array + public function getRichDescription(): ?RichText { - return $this->rawTitle; + return $this->richDescription; } /** From fcb68e9c98244a049b86373af6f0b4f082d8d4e4 Mon Sep 17 00:00:00 2001 From: johguentner Date: Sun, 5 Feb 2023 11:14:56 +0100 Subject: [PATCH 04/21] rewrite tests for title and add for description - within the database entity - add description example to stub --- tests/EndpointDatabasesTest.php | 7 ++++++- .../databases/response_specific_200.json | 19 +++++++++++++++++++ 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/tests/EndpointDatabasesTest.php b/tests/EndpointDatabasesTest.php index 12502e0..730d73c 100644 --- a/tests/EndpointDatabasesTest.php +++ b/tests/EndpointDatabasesTest.php @@ -4,6 +4,7 @@ use Carbon\Carbon; use FiveamCode\LaravelNotionApi\Entities\Database; +use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; use FiveamCode\LaravelNotionApi\Exceptions\NotionException; use Illuminate\Support\Facades\Http; use Notion; @@ -90,9 +91,13 @@ public function it_returns_database_entity_with_filled_properties() // check properties $this->assertSame('Grocery List', $databaseResult->getTitle()); + $this->assertSame('Grocery List Description', $databaseResult->getDescription()); $this->assertSame('database', $databaseResult->getObjectType()); - $this->assertCount(1, $databaseResult->getRawTitle()); + $this->assertInstanceOf(RichText::class, $databaseResult->getRichTitle()); + $this->assertInstanceOf(RichText::class, $databaseResult->getRichDescription()); + $this->assertCount(1, $databaseResult->getRichTitle()->getRawResponse()); + $this->assertCount(1, $databaseResult->getRichDescription()->getRawResponse()); $this->assertCount(12, $databaseResult->getRawProperties()); $this->assertInstanceOf(Carbon::class, $databaseResult->getCreatedTime()); diff --git a/tests/stubs/endpoints/databases/response_specific_200.json b/tests/stubs/endpoints/databases/response_specific_200.json index a900324..c0e50ad 100644 --- a/tests/stubs/endpoints/databases/response_specific_200.json +++ b/tests/stubs/endpoints/databases/response_specific_200.json @@ -3,6 +3,25 @@ "id": "668d797c-76fa-4934-9b05-ad288df2d136", "created_time": "2020-03-17T19:10:04.968Z", "last_edited_time": "2020-03-17T21:49:37.913Z", + "description": [ + { + "type": "text", + "text": { + "content": "Grocery List Description", + "link": null + }, + "annotations": { + "bold": false, + "italic": false, + "strikethrough": false, + "underline": false, + "code": false, + "color": "default" + }, + "plain_text": "Grocery List Description", + "href": null + } + ], "title": [ { "type": "text", From 558844e937c8e0b4b4eca43922ce5d22e4d26bf8 Mon Sep 17 00:00:00 2001 From: johguentner Date: Sun, 5 Feb 2023 11:18:10 +0100 Subject: [PATCH 05/21] rename ``TimestampableEntity`` - to ``HasTimestamps`` - for improved readability --- src/Entities/Blocks/Block.php | 4 ++-- src/Entities/Database.php | 4 ++-- src/Entities/Page.php | 4 ++-- src/Traits/{TimestampableEntity.php => HasTimestamps.php} | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) rename src/Traits/{TimestampableEntity.php => HasTimestamps.php} (98%) diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index d15538f..8958ab0 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -4,7 +4,7 @@ use FiveamCode\LaravelNotionApi\Entities\Entity; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; -use FiveamCode\LaravelNotionApi\Traits\TimestampableEntity; +use FiveamCode\LaravelNotionApi\Traits\HasTimestamps; use Illuminate\Support\Arr; /** @@ -12,7 +12,7 @@ */ class Block extends Entity { - use TimestampableEntity; + use HasTimestamps; /** * @var string diff --git a/src/Entities/Database.php b/src/Entities/Database.php index e447107..8070df2 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -4,7 +4,7 @@ use FiveamCode\LaravelNotionApi\Entities\Properties\Property; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; -use FiveamCode\LaravelNotionApi\Traits\TimestampableEntity; +use FiveamCode\LaravelNotionApi\Traits\HasTimestamps; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -13,7 +13,7 @@ */ class Database extends Entity { - use TimestampableEntity; + use HasTimestamps; /** * @var string diff --git a/src/Entities/Page.php b/src/Entities/Page.php index eebc602..20b2a32 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -17,7 +17,7 @@ use FiveamCode\LaravelNotionApi\Entities\Properties\Title; use FiveamCode\LaravelNotionApi\Entities\Properties\Url; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; -use FiveamCode\LaravelNotionApi\Traits\TimestampableEntity; +use FiveamCode\LaravelNotionApi\Traits\HasTimestamps; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -26,7 +26,7 @@ */ class Page extends Entity { - use TimestampableEntity; + use HasTimestamps; /** * @var string diff --git a/src/Traits/TimestampableEntity.php b/src/Traits/HasTimestamps.php similarity index 98% rename from src/Traits/TimestampableEntity.php rename to src/Traits/HasTimestamps.php index b148c70..054a839 100644 --- a/src/Traits/TimestampableEntity.php +++ b/src/Traits/HasTimestamps.php @@ -10,7 +10,7 @@ /** * Trait UpdatableEntity. */ -trait TimestampableEntity +trait HasTimestamps { /** * @var array From 192ae82d903d37578012de29a01ad337a12f3718 Mon Sep 17 00:00:00 2001 From: johguentner Date: Sun, 5 Feb 2023 11:50:32 +0100 Subject: [PATCH 06/21] introduce ``HasParent`` as Trait for entities - move page logic of ``HasParent`` to its on trait and did minor refactoring - add ``HasParent`` to Database and Block entity --- src/Entities/Blocks/Block.php | 4 ++- src/Entities/Database.php | 4 ++- src/Entities/Page.php | 44 ++----------------------- src/Traits/HasParent.php | 61 +++++++++++++++++++++++++++++++++++ 4 files changed, 70 insertions(+), 43 deletions(-) create mode 100644 src/Traits/HasParent.php diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index 8958ab0..19b91c8 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -4,6 +4,7 @@ use FiveamCode\LaravelNotionApi\Entities\Entity; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Traits\HasParent; use FiveamCode\LaravelNotionApi\Traits\HasTimestamps; use Illuminate\Support\Arr; @@ -12,7 +13,7 @@ */ class Block extends Entity { - use HasTimestamps; + use HasTimestamps, HasParent; /** * @var string @@ -61,6 +62,7 @@ protected function fillFromRaw(): void $this->fillType(); $this->fillRawContent(); $this->fillHasChildren(); + $this->fillParentProperties(); $this->fillTimestampableProperties(); } diff --git a/src/Entities/Database.php b/src/Entities/Database.php index 87e3a50..99539d6 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -5,6 +5,7 @@ use FiveamCode\LaravelNotionApi\Entities\Properties\Property; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Traits\HasParent; use FiveamCode\LaravelNotionApi\Traits\HasTimestamps; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -14,7 +15,7 @@ */ class Database extends Entity { - use HasTimestamps; + use HasTimestamps, HasParent; /** * @var string @@ -105,6 +106,7 @@ private function fillFromRaw() $this->fillObjectType(); $this->fillProperties(); $this->fillDatabaseUrl(); + $this->fillParentProperties(); $this->fillTimestampableProperties(); } diff --git a/src/Entities/Page.php b/src/Entities/Page.php index 0d13691..a6f7822 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -17,6 +17,7 @@ use FiveamCode\LaravelNotionApi\Entities\Properties\Title; use FiveamCode\LaravelNotionApi\Entities\Properties\Url; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Traits\HasParent; use FiveamCode\LaravelNotionApi\Traits\HasTimestamps; use Illuminate\Support\Arr; use Illuminate\Support\Collection; @@ -26,7 +27,7 @@ */ class Page extends Entity { - use HasTimestamps; + use HasTimestamps, HasParent; /** * @var string @@ -58,16 +59,6 @@ class Page extends Entity */ private string $coverType = ''; - /** - * @var string - */ - private string $parentId = ''; - - /** - * @var string - */ - private string $parentType = ''; - /** * @var string */ @@ -125,13 +116,13 @@ protected function setResponseData(array $responseData): void private function fillFromRaw(): void { $this->fillId(); - $this->fillParent(); $this->fillObjectType(); $this->fillProperties(); $this->fillTitle(); // This has to be called after fillProperties(), since title is provided by properties $this->fillPageUrl(); $this->fillIcon(); $this->fillCover(); + $this->fillParentProperties(); $this->fillTimestampableProperties(); } @@ -208,19 +199,6 @@ private function fillPageUrl(): void } } - private function fillParent(): void - { - if (Arr::exists($this->responseData, 'parent')) { - $this->parentType = $this->responseData['parent']['type']; - if (Arr::exists($this->responseData['parent'], 'database_id')) { - $this->parentId = $this->responseData['parent']['database_id']; - } elseif (Arr::exists($this->responseData['parent'], 'page_id')) { - $this->parentId = $this->responseData['parent']['page_id']; - } elseif (Arr::exists($this->responseData['parent'], 'workspace')) { - $this->parentId = $this->responseData['parent']['workspace']; - } - } - } /** * @param $propertyTitle @@ -475,22 +453,6 @@ public function getObjectType(): string return $this->objectType; } - /** - * @return string - */ - public function getParentId(): string - { - return $this->parentId; - } - - /** - * @return string - */ - public function getParentType(): string - { - return $this->parentType; - } - /** * @return array */ diff --git a/src/Traits/HasParent.php b/src/Traits/HasParent.php new file mode 100644 index 0000000..2bb7d41 --- /dev/null +++ b/src/Traits/HasParent.php @@ -0,0 +1,61 @@ +fillParent(); + } + + private function fillParent(): void + { + if (Arr::exists($this->responseData, 'parent') && Arr::exists($this->responseData['parent'], 'type')) { + $this->parentType = $this->responseData['parent']['type']; + if (Arr::exists($this->responseData['parent'], $this->parentType)) { + $this->parentId = $this->responseData['parent'][$this->parentType]; + } + } + } + + /** + * @return string + */ + public function getParentId(): string + { + return $this->parentId; + } + + /** + * @return string + */ + public function getParentType(): string + { + return $this->parentType; + } +} From 0cccb2eb9f14f1c3c7e084173b1dd73c0d97fbc7 Mon Sep 17 00:00:00 2001 From: johguentner Date: Sun, 5 Feb 2023 11:51:38 +0100 Subject: [PATCH 07/21] add tests for entities which use ``HasParent`` - tests already exist for page entity - add to database and block tests - add raw parent property to stubs within block and database --- tests/EndpointBlocksTest.php | 6 +++ tests/EndpointDatabasesTest.php | 3 ++ .../blocks/response_specific_block_200.json | 8 ++- .../databases/response_specific_200.json | 49 ++++++++++--------- 4 files changed, 41 insertions(+), 25 deletions(-) diff --git a/tests/EndpointBlocksTest.php b/tests/EndpointBlocksTest.php index f26ea0b..0964493 100644 --- a/tests/EndpointBlocksTest.php +++ b/tests/EndpointBlocksTest.php @@ -340,5 +340,11 @@ public function it_retrieves_a_single_block() $this->assertInstanceOf(Block::class, $block); $this->assertInstanceOf(Paragraph::class, $block); + $this->assertEquals('a6f8ebe8-d5df-4ffa-b543-bcd54d1c3bad', $block->getId()); + $this->assertEquals('paragraph', $block->getType()); + $this->assertEquals('This is a paragraph test', $block->getContent()->getPlainText()); + + $this->assertEquals('page_id', $block->getParentType()); + $this->assertEquals('f2939732-f694-4ce2-b613-f28db6ded673', $block->getParentId()); } } diff --git a/tests/EndpointDatabasesTest.php b/tests/EndpointDatabasesTest.php index 730d73c..7dc2d7a 100644 --- a/tests/EndpointDatabasesTest.php +++ b/tests/EndpointDatabasesTest.php @@ -102,6 +102,9 @@ public function it_returns_database_entity_with_filled_properties() $this->assertInstanceOf(Carbon::class, $databaseResult->getCreatedTime()); $this->assertInstanceOf(Carbon::class, $databaseResult->getLastEditedTime()); + + $this->assertEquals('page_id', $databaseResult->getParentType()); + $this->assertEquals('f2939732-f694-4ce2-b613-f28db6ded673', $databaseResult->getParentId()); } /** @test */ diff --git a/tests/stubs/endpoints/blocks/response_specific_block_200.json b/tests/stubs/endpoints/blocks/response_specific_block_200.json index 1b2a15f..1de5b9f 100644 --- a/tests/stubs/endpoints/blocks/response_specific_block_200.json +++ b/tests/stubs/endpoints/blocks/response_specific_block_200.json @@ -6,12 +6,16 @@ "has_children": false, "archived": false, "type": "paragraph", + "parent": { + "type": "page_id", + "page_id": "f2939732-f694-4ce2-b613-f28db6ded673" + }, "paragraph": { "text": [ { "type": "text", "text": { - "content": "C:\\xampp\\php", + "content": "This is a paragraph test", "link": null }, "annotations": { @@ -22,7 +26,7 @@ "code": false, "color": "default" }, - "plain_text": "C:\\xampp\\php", + "plain_text": "This is a paragraph test", "href": null } ] diff --git a/tests/stubs/endpoints/databases/response_specific_200.json b/tests/stubs/endpoints/databases/response_specific_200.json index c0e50ad..bbe9fcf 100644 --- a/tests/stubs/endpoints/databases/response_specific_200.json +++ b/tests/stubs/endpoints/databases/response_specific_200.json @@ -3,6 +3,10 @@ "id": "668d797c-76fa-4934-9b05-ad288df2d136", "created_time": "2020-03-17T19:10:04.968Z", "last_edited_time": "2020-03-17T21:49:37.913Z", + "parent": { + "type": "page_id", + "page_id": "f2939732-f694-4ce2-b613-f28db6ded673" + }, "description": [ { "type": "text", @@ -122,29 +126,28 @@ "id": "Z\\Eh", "type": "multi_select", "multi_select": { - "options": - [ - { - "id": "d209b920-212c-4040-9d4a-bdf349dd8b2a", - "name": "Duc Loi Market", - "color": "blue" - }, - { - "id": "70104074-0f91-467b-9787-00d59e6e1e41", - "name": "Rainbow Grocery", - "color": "gray" - }, - { - "id": "e6fd4f04-894d-4fa7-8d8b-e92d08ebb604", - "name": "Nijiya Market", - "color": "purple" - }, - { - "id": "6c3867c5-d542-4f84-b6e9-a420c43094e7", - "name": "Gus's Community Market", - "color": "yellow" - } - ] + "options": [ + { + "id": "d209b920-212c-4040-9d4a-bdf349dd8b2a", + "name": "Duc Loi Market", + "color": "blue" + }, + { + "id": "70104074-0f91-467b-9787-00d59e6e1e41", + "name": "Rainbow Grocery", + "color": "gray" + }, + { + "id": "e6fd4f04-894d-4fa7-8d8b-e92d08ebb604", + "name": "Nijiya Market", + "color": "purple" + }, + { + "id": "6c3867c5-d542-4f84-b6e9-a420c43094e7", + "name": "Gus's Community Market", + "color": "yellow" + } + ] } }, "+1": { From f2c0702046e884c9aeb264f08093e2eddf6f578b Mon Sep 17 00:00:00 2001 From: Di Date: Sun, 5 Feb 2023 11:51:54 +0100 Subject: [PATCH 08/21] Apply fixes from StyleCI (#106) --- src/Entities/Page.php | 1 - src/Traits/HasParent.php | 6 +----- 2 files changed, 1 insertion(+), 6 deletions(-) diff --git a/src/Entities/Page.php b/src/Entities/Page.php index a6f7822..1fd26ba 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -199,7 +199,6 @@ private function fillPageUrl(): void } } - /** * @param $propertyTitle * @param $property diff --git a/src/Traits/HasParent.php b/src/Traits/HasParent.php index 2bb7d41..f9cef22 100644 --- a/src/Traits/HasParent.php +++ b/src/Traits/HasParent.php @@ -2,13 +2,10 @@ namespace FiveamCode\LaravelNotionApi\Traits; -use Carbon\Carbon; -use DateTime; -use FiveamCode\LaravelNotionApi\Entities\User; use Illuminate\Support\Arr; /** - * Trait HasParent + * Trait HasParent. */ trait HasParent { @@ -27,7 +24,6 @@ trait HasParent */ private string $parentType = ''; - protected function fillParentProperties(): void { $this->fillParent(); From b9797bf46ca58205194d4a328bc130045a0cce93 Mon Sep 17 00:00:00 2001 From: johguentner Date: Sun, 5 Feb 2023 12:04:05 +0100 Subject: [PATCH 09/21] add ``is_inline`` property to database entity - add missing prop to stub - add check for property within database test --- src/Entities/Database.php | 21 +++++++++++++++++++ tests/EndpointDatabasesTest.php | 2 ++ .../databases/response_specific_200.json | 1 + 3 files changed, 24 insertions(+) diff --git a/src/Entities/Database.php b/src/Entities/Database.php index 99539d6..719410f 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -67,6 +67,11 @@ class Database extends Entity */ protected ?RichText $richDescription = null; + /** + * @var bool + */ + protected bool $isInline = false; + /** * @var array */ @@ -102,6 +107,7 @@ private function fillFromRaw() $this->fillIcon(); $this->fillCover(); $this->fillTitle(); + $this->fillIsInline(); $this->fillDescription(); $this->fillObjectType(); $this->fillProperties(); @@ -118,6 +124,13 @@ private function fillTitle(): void } } + private function fillIsInline(): void + { + if (Arr::exists($this->responseData, 'is_inline')) { + $this->isInline = $this->responseData['is_inline']; + } + } + private function fillDescription(): void { if (Arr::exists($this->responseData, 'description') && is_array($this->responseData['description'])) { @@ -210,6 +223,14 @@ public function getTitle(): string return $this->title; } + /** + * @return bool + */ + public function getIsInline(): bool + { + return $this->isInline; + } + /** * @return string */ diff --git a/tests/EndpointDatabasesTest.php b/tests/EndpointDatabasesTest.php index 7dc2d7a..c0203b0 100644 --- a/tests/EndpointDatabasesTest.php +++ b/tests/EndpointDatabasesTest.php @@ -93,6 +93,8 @@ public function it_returns_database_entity_with_filled_properties() $this->assertSame('Grocery List', $databaseResult->getTitle()); $this->assertSame('Grocery List Description', $databaseResult->getDescription()); $this->assertSame('database', $databaseResult->getObjectType()); + $this->assertSame('668d797c-76fa-4934-9b05-ad288df2d136', $databaseResult->getId()); + $this->assertTrue($databaseResult->getIsInline()); $this->assertInstanceOf(RichText::class, $databaseResult->getRichTitle()); $this->assertInstanceOf(RichText::class, $databaseResult->getRichDescription()); diff --git a/tests/stubs/endpoints/databases/response_specific_200.json b/tests/stubs/endpoints/databases/response_specific_200.json index bbe9fcf..3bf2da7 100644 --- a/tests/stubs/endpoints/databases/response_specific_200.json +++ b/tests/stubs/endpoints/databases/response_specific_200.json @@ -7,6 +7,7 @@ "type": "page_id", "page_id": "f2939732-f694-4ce2-b613-f28db6ded673" }, + "is_inline": true, "description": [ { "type": "text", From 60bcca4a7a0282551717574feead3b6ad1dd4aea Mon Sep 17 00:00:00 2001 From: johguentner Date: Sun, 5 Feb 2023 12:24:42 +0100 Subject: [PATCH 10/21] introduce ``HasArchive`` trait for entities - the handling of archived-flags can be added to entities by this trait - add trait to database, page and block --- src/Entities/Blocks/Block.php | 4 +++- src/Entities/Database.php | 6 +++-- src/Entities/Page.php | 4 +++- src/Traits/HasArchive.php | 42 +++++++++++++++++++++++++++++++++++ 4 files changed, 52 insertions(+), 4 deletions(-) create mode 100644 src/Traits/HasArchive.php diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index 19b91c8..68957e1 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -4,6 +4,7 @@ use FiveamCode\LaravelNotionApi\Entities\Entity; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Traits\HasArchive; use FiveamCode\LaravelNotionApi\Traits\HasParent; use FiveamCode\LaravelNotionApi\Traits\HasTimestamps; use Illuminate\Support\Arr; @@ -13,7 +14,7 @@ */ class Block extends Entity { - use HasTimestamps, HasParent; + use HasTimestamps, HasArchive, HasParent; /** * @var string @@ -63,6 +64,7 @@ protected function fillFromRaw(): void $this->fillRawContent(); $this->fillHasChildren(); $this->fillParentProperties(); + $this->fillArchivedProperties(); $this->fillTimestampableProperties(); } diff --git a/src/Entities/Database.php b/src/Entities/Database.php index 719410f..2a77a93 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -5,6 +5,7 @@ use FiveamCode\LaravelNotionApi\Entities\Properties\Property; use FiveamCode\LaravelNotionApi\Entities\PropertyItems\RichText; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Traits\HasArchive; use FiveamCode\LaravelNotionApi\Traits\HasParent; use FiveamCode\LaravelNotionApi\Traits\HasTimestamps; use Illuminate\Support\Arr; @@ -15,7 +16,7 @@ */ class Database extends Entity { - use HasTimestamps, HasParent; + use HasTimestamps, HasArchive, HasParent; /** * @var string @@ -113,6 +114,7 @@ private function fillFromRaw() $this->fillProperties(); $this->fillDatabaseUrl(); $this->fillParentProperties(); + $this->fillArchivedProperties(); $this->fillTimestampableProperties(); } @@ -226,7 +228,7 @@ public function getTitle(): string /** * @return bool */ - public function getIsInline(): bool + public function isInline(): bool { return $this->isInline; } diff --git a/src/Entities/Page.php b/src/Entities/Page.php index 1fd26ba..b697fa2 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -17,6 +17,7 @@ use FiveamCode\LaravelNotionApi\Entities\Properties\Title; use FiveamCode\LaravelNotionApi\Entities\Properties\Url; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; +use FiveamCode\LaravelNotionApi\Traits\HasArchive; use FiveamCode\LaravelNotionApi\Traits\HasParent; use FiveamCode\LaravelNotionApi\Traits\HasTimestamps; use Illuminate\Support\Arr; @@ -27,7 +28,7 @@ */ class Page extends Entity { - use HasTimestamps, HasParent; + use HasTimestamps, HasArchive, HasParent; /** * @var string @@ -123,6 +124,7 @@ private function fillFromRaw(): void $this->fillIcon(); $this->fillCover(); $this->fillParentProperties(); + $this->fillArchivedProperties(); $this->fillTimestampableProperties(); } diff --git a/src/Traits/HasArchive.php b/src/Traits/HasArchive.php new file mode 100644 index 0000000..dd807c5 --- /dev/null +++ b/src/Traits/HasArchive.php @@ -0,0 +1,42 @@ +fillArchived(); + } + + private function fillArchived(): void + { + if (Arr::exists($this->responseData, 'archived')) { + $this->archived = $this->responseData['archived']; + } + } + + /** + * @return bool + */ + public function isArchived(): bool + { + return $this->archived; + } +} From 81b2b102b54fa3031471866ab5e6cd8c1f8b2b6d Mon Sep 17 00:00:00 2001 From: johguentner Date: Sun, 5 Feb 2023 12:25:52 +0100 Subject: [PATCH 11/21] add tests for the ``HasArchive`` trait - to database, block and pages - change stubs regarding the archived-flag, to force testing to check for ``true`` as archived --- tests/EndpointBlocksTest.php | 1 + tests/EndpointDatabasesTest.php | 3 ++- tests/EndpointPagesTest.php | 1 + tests/stubs/endpoints/blocks/response_specific_block_200.json | 2 +- tests/stubs/endpoints/databases/response_specific_200.json | 1 + tests/stubs/endpoints/pages/response_specific_200.json | 2 +- 6 files changed, 7 insertions(+), 3 deletions(-) diff --git a/tests/EndpointBlocksTest.php b/tests/EndpointBlocksTest.php index 0964493..ffb1943 100644 --- a/tests/EndpointBlocksTest.php +++ b/tests/EndpointBlocksTest.php @@ -346,5 +346,6 @@ public function it_retrieves_a_single_block() $this->assertEquals('page_id', $block->getParentType()); $this->assertEquals('f2939732-f694-4ce2-b613-f28db6ded673', $block->getParentId()); + $this->assertTrue($block->isArchived()); } } diff --git a/tests/EndpointDatabasesTest.php b/tests/EndpointDatabasesTest.php index c0203b0..42f3891 100644 --- a/tests/EndpointDatabasesTest.php +++ b/tests/EndpointDatabasesTest.php @@ -94,7 +94,8 @@ public function it_returns_database_entity_with_filled_properties() $this->assertSame('Grocery List Description', $databaseResult->getDescription()); $this->assertSame('database', $databaseResult->getObjectType()); $this->assertSame('668d797c-76fa-4934-9b05-ad288df2d136', $databaseResult->getId()); - $this->assertTrue($databaseResult->getIsInline()); + $this->assertTrue($databaseResult->isInline()); + $this->assertTrue($databaseResult->isArchived()); $this->assertInstanceOf(RichText::class, $databaseResult->getRichTitle()); $this->assertInstanceOf(RichText::class, $databaseResult->getRichDescription()); diff --git a/tests/EndpointPagesTest.php b/tests/EndpointPagesTest.php index d0c3323..f8608b2 100644 --- a/tests/EndpointPagesTest.php +++ b/tests/EndpointPagesTest.php @@ -75,6 +75,7 @@ public function it_returns_page_entity_with_filled_properties() $this->assertCount(9, $pageResult->getPropertyKeys()); $this->assertSame('database_id', $pageResult->getParentType()); $this->assertSame('f2939732-f694-4ce2-b613-f28db6ded673', $pageResult->getParentId()); + $this->assertTrue($pageResult->isArchived()); // check date and datetime properties $this->assertTrue($pageResult->getProperty('DateWithTime')->hasTime()); diff --git a/tests/stubs/endpoints/blocks/response_specific_block_200.json b/tests/stubs/endpoints/blocks/response_specific_block_200.json index 1de5b9f..de5336b 100644 --- a/tests/stubs/endpoints/blocks/response_specific_block_200.json +++ b/tests/stubs/endpoints/blocks/response_specific_block_200.json @@ -4,7 +4,7 @@ "created_time": "2021-05-17T13:51:00.000Z", "last_edited_time": "2021-06-10T17:40:00.000Z", "has_children": false, - "archived": false, + "archived": true, "type": "paragraph", "parent": { "type": "page_id", diff --git a/tests/stubs/endpoints/databases/response_specific_200.json b/tests/stubs/endpoints/databases/response_specific_200.json index 3bf2da7..af8ae96 100644 --- a/tests/stubs/endpoints/databases/response_specific_200.json +++ b/tests/stubs/endpoints/databases/response_specific_200.json @@ -8,6 +8,7 @@ "page_id": "f2939732-f694-4ce2-b613-f28db6ded673" }, "is_inline": true, + "archived": true, "description": [ { "type": "text", diff --git a/tests/stubs/endpoints/pages/response_specific_200.json b/tests/stubs/endpoints/pages/response_specific_200.json index 268e117..b34e012 100644 --- a/tests/stubs/endpoints/pages/response_specific_200.json +++ b/tests/stubs/endpoints/pages/response_specific_200.json @@ -7,7 +7,7 @@ "type": "database_id", "database_id": "f2939732-f694-4ce2-b613-f28db6ded673" }, - "archived": false, + "archived": true, "properties": { "NumberProp": { "id": ">d{N", From dedf7c31423481511ea51d9816328ab4c48329f9 Mon Sep 17 00:00:00 2001 From: Di Date: Sun, 5 Feb 2023 12:26:09 +0100 Subject: [PATCH 12/21] Apply fixes from StyleCI (#107) --- src/Traits/HasArchive.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Traits/HasArchive.php b/src/Traits/HasArchive.php index dd807c5..bba5a49 100644 --- a/src/Traits/HasArchive.php +++ b/src/Traits/HasArchive.php @@ -5,7 +5,7 @@ use Illuminate\Support\Arr; /** - * Trait HasArchive + * Trait HasArchive. */ trait HasArchive { @@ -19,7 +19,6 @@ trait HasArchive */ private bool $archived = false; - protected function fillArchivedProperties(): void { $this->fillArchived(); From c5b482ec434d794d00913ab2bec5467b79063eba Mon Sep 17 00:00:00 2001 From: johguentner Date: Sun, 5 Feb 2023 12:42:43 +0100 Subject: [PATCH 13/21] move ``object`` to entity base class - and do filling of ``id`` and ``object`` within a entity base class method - call ``::fillEntityBase`` form the ``fillFromRaw`` within all according entitites - add test-cases for checking ``getObjectType`` --- src/Entities/Blocks/Block.php | 2 +- src/Entities/Database.php | 23 +----------------- src/Entities/Entity.php | 29 ++++++++++++++++++++++- src/Entities/Page.php | 23 +----------------- src/Entities/Properties/Property.php | 2 +- src/Entities/PropertyItems/SelectItem.php | 2 +- src/Entities/User.php | 2 +- 7 files changed, 34 insertions(+), 49 deletions(-) diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index 68957e1..2524a88 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -59,7 +59,7 @@ protected function setResponseData(array $responseData): void protected function fillFromRaw(): void { - $this->fillId(); + parent::fillEntityBase(); $this->fillType(); $this->fillRawContent(); $this->fillHasChildren(); diff --git a/src/Entities/Database.php b/src/Entities/Database.php index 2a77a93..28acc25 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -53,11 +53,6 @@ class Database extends Entity */ private string $url; - /** - * @var string - */ - protected string $objectType = ''; - /** * @var ?RichText */ @@ -104,13 +99,12 @@ protected function setResponseData(array $responseData): void private function fillFromRaw() { - $this->fillId(); + parent::fillEntityBase(); $this->fillIcon(); $this->fillCover(); $this->fillTitle(); $this->fillIsInline(); $this->fillDescription(); - $this->fillObjectType(); $this->fillProperties(); $this->fillDatabaseUrl(); $this->fillParentProperties(); @@ -174,13 +168,6 @@ private function fillCover(): void } } - private function fillObjectType(): void - { - if (Arr::exists($this->responseData, 'object')) { - $this->objectType = $this->responseData['object']; - } - } - private function fillProperties(): void { if (Arr::exists($this->responseData, 'properties')) { @@ -209,14 +196,6 @@ public function getProperty(string $propertyKey): ?Property return $this->propertyMap[$propertyKey]; } - /** - * @return string - */ - public function getObjectType(): string - { - return $this->objectType; - } - /** * @return string */ diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 5b02440..ea6488d 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -17,6 +17,11 @@ class Entity implements JsonSerializable */ private string $id; + /** + * @var string + */ + protected string $objectType = ''; + /** * @var array */ @@ -67,11 +72,24 @@ protected function setResponseData(array $responseData): void $this->responseData = $responseData; } - protected function fillId() + protected function fillEntityBase(): void + { + $this->fillId(); + $this->fillObjectType(); + } + + private function fillId() { $this->id = $this->responseData['id']; } + private function fillObjectType(): void + { + if (Arr::exists($this->responseData, 'object')) { + $this->objectType = $this->responseData['object']; + } + } + /** * @return string */ @@ -80,11 +98,20 @@ public function getId(): string return $this->id; } + public function setId($id): void { $this->id = $id; } + /** + * @return string + */ + public function getObjectType(): string + { + return $this->objectType; + } + /** * @return array */ diff --git a/src/Entities/Page.php b/src/Entities/Page.php index b697fa2..caf0971 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -60,11 +60,6 @@ class Page extends Entity */ private string $coverType = ''; - /** - * @var string - */ - protected string $objectType = ''; - /** * @var array */ @@ -116,8 +111,7 @@ protected function setResponseData(array $responseData): void private function fillFromRaw(): void { - $this->fillId(); - $this->fillObjectType(); + parent::fillEntityBase(); $this->fillProperties(); $this->fillTitle(); // This has to be called after fillProperties(), since title is provided by properties $this->fillPageUrl(); @@ -128,13 +122,6 @@ private function fillFromRaw(): void $this->fillTimestampableProperties(); } - private function fillObjectType(): void - { - if (Arr::exists($this->responseData, 'object')) { - $this->objectType = $this->responseData['object']; - } - } - /** * @throws HandlingException */ @@ -446,14 +433,6 @@ public function getProperty(string $propertyKey): ?Property return $this->propertyMap[$propertyKey]; } - /** - * @return string - */ - public function getObjectType(): string - { - return $this->objectType; - } - /** * @return array */ diff --git a/src/Entities/Properties/Property.php b/src/Entities/Properties/Property.php index 2ceb17c..32f17cd 100644 --- a/src/Entities/Properties/Property.php +++ b/src/Entities/Properties/Property.php @@ -62,7 +62,7 @@ protected function setResponseData(array $responseData): void protected function fillFromRaw(): void { - $this->fillId(); + parent::fillEntityBase(); $this->fillType(); $this->fillContent(); } diff --git a/src/Entities/PropertyItems/SelectItem.php b/src/Entities/PropertyItems/SelectItem.php index 42e8437..87f1228 100644 --- a/src/Entities/PropertyItems/SelectItem.php +++ b/src/Entities/PropertyItems/SelectItem.php @@ -36,7 +36,7 @@ protected function setResponseData(array $responseData): void protected function fillFromRaw(): void { - $this->fillId(); + parent::fillEntityBase(); $this->fillName(); $this->fillColor(); } diff --git a/src/Entities/User.php b/src/Entities/User.php index e337bf3..9c201fb 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -37,7 +37,7 @@ protected function setResponseData(array $responseData): void private function fillFromRaw(): void { - $this->fillId(); + parent::fillEntityBase(); $this->fillName(); $this->fillAvatarUrl(); } From 4d36500fcdf7c717f11ffccdb5f921fba8e047fe Mon Sep 17 00:00:00 2001 From: johguentner Date: Sun, 5 Feb 2023 12:42:52 +0100 Subject: [PATCH 14/21] add test-cases (previous commit) --- tests/EndpointBlocksTest.php | 1 + tests/EndpointUsersTest.php | 1 + 2 files changed, 2 insertions(+) diff --git a/tests/EndpointBlocksTest.php b/tests/EndpointBlocksTest.php index ffb1943..089814f 100644 --- a/tests/EndpointBlocksTest.php +++ b/tests/EndpointBlocksTest.php @@ -343,6 +343,7 @@ public function it_retrieves_a_single_block() $this->assertEquals('a6f8ebe8-d5df-4ffa-b543-bcd54d1c3bad', $block->getId()); $this->assertEquals('paragraph', $block->getType()); $this->assertEquals('This is a paragraph test', $block->getContent()->getPlainText()); + $this->assertEquals('block', $block->getObjectType()); $this->assertEquals('page_id', $block->getParentType()); $this->assertEquals('f2939732-f694-4ce2-b613-f28db6ded673', $block->getParentId()); diff --git a/tests/EndpointUsersTest.php b/tests/EndpointUsersTest.php index e9b17af..25e56c1 100644 --- a/tests/EndpointUsersTest.php +++ b/tests/EndpointUsersTest.php @@ -80,6 +80,7 @@ public function it_returns_a_specific_user_as_entity_object() $this->assertInstanceOf(User::class, $user); $this->assertEquals('Avocado Lovelace', $user->getName()); + $this->assertEquals('user', $user->getObjectType()); $this->assertEquals('https://secure.notion-static.com/e6a352a8-8381-44d0-a1dc-9ed80e62b53d.jpg', $user->getAvatarUrl()); } From d59324c0e9228e685fa7eb46008c56793aad3fa4 Mon Sep 17 00:00:00 2001 From: Di Date: Sun, 5 Feb 2023 12:43:09 +0100 Subject: [PATCH 15/21] Apply fixes from StyleCI (#108) --- src/Entities/Entity.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index ea6488d..b3e45fc 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -98,7 +98,6 @@ public function getId(): string return $this->id; } - public function setId($id): void { $this->id = $id; From b32890ea3e1054252cb2c2e78cae4166561217d2 Mon Sep 17 00:00:00 2001 From: johguentner Date: Mon, 6 Feb 2023 09:19:04 +0100 Subject: [PATCH 16/21] change name ``->fillTimestampableProperties`` - to ``->fillTimestampableAttributes`` --- src/Entities/Blocks/Block.php | 2 +- src/Entities/Database.php | 2 +- src/Entities/Page.php | 2 +- src/Traits/HasTimestamps.php | 10 +++++----- 4 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index 8958ab0..cb5e64e 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -61,7 +61,7 @@ protected function fillFromRaw(): void $this->fillType(); $this->fillRawContent(); $this->fillHasChildren(); - $this->fillTimestampableProperties(); + $this->fillTimestampableAttributes(); } private function fillType(): void diff --git a/src/Entities/Database.php b/src/Entities/Database.php index 8070df2..37352f2 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -93,7 +93,7 @@ private function fillFromRaw() $this->fillObjectType(); $this->fillProperties(); $this->fillDatabaseUrl(); - $this->fillTimestampableProperties(); + $this->fillTimestampableAttributes(); } private function fillTitle(): void diff --git a/src/Entities/Page.php b/src/Entities/Page.php index 20b2a32..3c8be33 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -121,7 +121,7 @@ private function fillFromRaw(): void $this->fillPageUrl(); $this->fillIcon(); $this->fillCover(); - $this->fillTimestampableProperties(); + $this->fillTimestampableAttributes(); } private function fillObjectType(): void diff --git a/src/Traits/HasTimestamps.php b/src/Traits/HasTimestamps.php index 054a839..55a1f15 100644 --- a/src/Traits/HasTimestamps.php +++ b/src/Traits/HasTimestamps.php @@ -37,7 +37,7 @@ trait HasTimestamps */ protected User $lastEditedBy; - protected function fillTimestampableProperties(): void + protected function fillTimestampableAttributes(): void { $this->fillCreatedTime(); $this->fillLastEditedTime(); @@ -45,28 +45,28 @@ protected function fillTimestampableProperties(): void $this->fillLastEditedBy(); } - protected function fillCreatedTime(): void + private function fillCreatedTime(): void { if (Arr::exists($this->responseData, 'created_time')) { $this->createdTime = new Carbon($this->responseData['created_time']); } } - protected function fillLastEditedTime(): void + private function fillLastEditedTime(): void { if (Arr::exists($this->responseData, 'last_edited_time')) { $this->lastEditedTime = new Carbon($this->responseData['last_edited_time']); } } - protected function fillCreatedBy(): void + private function fillCreatedBy(): void { if (Arr::exists($this->responseData, 'created_by')) { $this->createdBy = new User($this->responseData['created_by']); } } - protected function fillLastEditedBy(): void + private function fillLastEditedBy(): void { if (Arr::exists($this->responseData, 'last_edited_by')) { $this->lastEditedBy = new User($this->responseData['last_edited_by']); From 78d93e2fca51526bd7ff791b29008beabd00fc23 Mon Sep 17 00:00:00 2001 From: johguentner Date: Mon, 6 Feb 2023 09:22:32 +0100 Subject: [PATCH 17/21] change name of fill-method of traits - ``...properties`` to ``...attributes`` --- src/Entities/Blocks/Block.php | 4 ++-- src/Entities/Database.php | 4 ++-- src/Entities/Page.php | 4 ++-- src/Traits/HasArchive.php | 2 +- src/Traits/HasParent.php | 2 +- 5 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index c3ebcd6..0718171 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -63,8 +63,8 @@ protected function fillFromRaw(): void $this->fillType(); $this->fillRawContent(); $this->fillHasChildren(); - $this->fillParentProperties(); - $this->fillArchivedProperties(); + $this->fillParentAttributes(); + $this->fillArchivedAttributes(); $this->fillTimestampableAttributes(); } diff --git a/src/Entities/Database.php b/src/Entities/Database.php index 201474e..8020918 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -107,8 +107,8 @@ private function fillFromRaw() $this->fillDescription(); $this->fillProperties(); $this->fillDatabaseUrl(); - $this->fillParentProperties(); - $this->fillArchivedProperties(); + $this->fillParentAttributes(); + $this->fillArchivedAttributes(); $this->fillTimestampableAttributes(); } diff --git a/src/Entities/Page.php b/src/Entities/Page.php index 7c01574..43888dd 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -117,8 +117,8 @@ private function fillFromRaw(): void $this->fillPageUrl(); $this->fillIcon(); $this->fillCover(); - $this->fillParentProperties(); - $this->fillArchivedProperties(); + $this->fillParentAttributes(); + $this->fillArchivedAttributes(); $this->fillTimestampableAttributes(); } diff --git a/src/Traits/HasArchive.php b/src/Traits/HasArchive.php index bba5a49..1235ea7 100644 --- a/src/Traits/HasArchive.php +++ b/src/Traits/HasArchive.php @@ -19,7 +19,7 @@ trait HasArchive */ private bool $archived = false; - protected function fillArchivedProperties(): void + protected function fillArchivedAttributes(): void { $this->fillArchived(); } diff --git a/src/Traits/HasParent.php b/src/Traits/HasParent.php index f9cef22..8356ae6 100644 --- a/src/Traits/HasParent.php +++ b/src/Traits/HasParent.php @@ -24,7 +24,7 @@ trait HasParent */ private string $parentType = ''; - protected function fillParentProperties(): void + protected function fillParentAttributes(): void { $this->fillParent(); } From c47dfbcfdf9fa6ad3b2bb0036c50bf8e489bbff2 Mon Sep 17 00:00:00 2001 From: johguentner Date: Mon, 6 Feb 2023 13:14:50 +0100 Subject: [PATCH 18/21] allow ``null`` for attrs in HasTimestamps trait --- src/Traits/HasTimestamps.php | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/Traits/HasTimestamps.php b/src/Traits/HasTimestamps.php index 55a1f15..4cd99e4 100644 --- a/src/Traits/HasTimestamps.php +++ b/src/Traits/HasTimestamps.php @@ -20,22 +20,22 @@ trait HasTimestamps /** * @var DateTime */ - protected DateTime $createdTime; + protected ?DateTime $createdTime = null; /** * @var DateTime */ - protected DateTime $lastEditedTime; + protected ?DateTime $lastEditedTime = null; /** * @var User */ - protected User $createdBy; + protected ?User $createdBy = null; /** * @var User */ - protected User $lastEditedBy; + protected ?User $lastEditedBy = null; protected function fillTimestampableAttributes(): void { @@ -74,33 +74,33 @@ private function fillLastEditedBy(): void } /** - * @return DateTime + * @return ?DateTime */ - public function getCreatedTime(): DateTime + public function getCreatedTime(): ?DateTime { return $this->createdTime; } /** - * @return DateTime + * @return ?DateTime */ - public function getLastEditedTime(): DateTime + public function getLastEditedTime(): ?DateTime { return $this->lastEditedTime; } /** - * @return User + * @return ?User */ - public function getCreatedBy(): User + public function getCreatedBy(): ?User { return $this->createdBy; } /** - * @return User + * @return ?User */ - public function getLastEditedBy(): User + public function getLastEditedBy(): ?User { return $this->lastEditedBy; } From be1efeb113fa6a46f4b15110f52e164ef2265b9a Mon Sep 17 00:00:00 2001 From: johguentner Date: Mon, 6 Feb 2023 13:15:07 +0100 Subject: [PATCH 19/21] rename trait comment --- src/Traits/HasTimestamps.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Traits/HasTimestamps.php b/src/Traits/HasTimestamps.php index 4cd99e4..2e769c4 100644 --- a/src/Traits/HasTimestamps.php +++ b/src/Traits/HasTimestamps.php @@ -8,7 +8,7 @@ use Illuminate\Support\Arr; /** - * Trait UpdatableEntity. + * Trait HasTimestamps. */ trait HasTimestamps { From f7c097d2ddc418c792382001c3918893564feef5 Mon Sep 17 00:00:00 2001 From: johguentner Date: Mon, 6 Feb 2023 20:08:47 +0100 Subject: [PATCH 20/21] rename `fillEntityBase()` to `fillEssentials` and - move filling of trait related attributes to the context of `fillEssentials()` --- src/Entities/Blocks/Block.php | 5 +-- src/Entities/Database.php | 5 +-- src/Entities/Entity.php | 44 +++++++++++++++++++++-- src/Entities/Page.php | 5 +-- src/Entities/Properties/Property.php | 2 +- src/Entities/PropertyItems/SelectItem.php | 2 +- src/Entities/User.php | 2 +- 7 files changed, 47 insertions(+), 18 deletions(-) diff --git a/src/Entities/Blocks/Block.php b/src/Entities/Blocks/Block.php index 0718171..507ef60 100644 --- a/src/Entities/Blocks/Block.php +++ b/src/Entities/Blocks/Block.php @@ -59,13 +59,10 @@ protected function setResponseData(array $responseData): void protected function fillFromRaw(): void { - parent::fillEntityBase(); + parent::fillEssentials(); $this->fillType(); $this->fillRawContent(); $this->fillHasChildren(); - $this->fillParentAttributes(); - $this->fillArchivedAttributes(); - $this->fillTimestampableAttributes(); } private function fillType(): void diff --git a/src/Entities/Database.php b/src/Entities/Database.php index 8020918..9df0eee 100644 --- a/src/Entities/Database.php +++ b/src/Entities/Database.php @@ -99,7 +99,7 @@ protected function setResponseData(array $responseData): void private function fillFromRaw() { - parent::fillEntityBase(); + parent::fillEssentials(); $this->fillIcon(); $this->fillCover(); $this->fillTitle(); @@ -107,9 +107,6 @@ private function fillFromRaw() $this->fillDescription(); $this->fillProperties(); $this->fillDatabaseUrl(); - $this->fillParentAttributes(); - $this->fillArchivedAttributes(); - $this->fillTimestampableAttributes(); } private function fillTitle(): void diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index b3e45fc..19ec2df 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -50,7 +50,7 @@ public function __construct(array $responseData = null) */ protected function setResponseData(array $responseData): void { - if (! Arr::exists($responseData, 'object')) { + if (!Arr::exists($responseData, 'object')) { throw new HandlingException('invalid json-array: no object given'); } @@ -65,17 +65,55 @@ protected function setResponseData(array $responseData): void throw NotionException::instance('Not found', compact('responseData')); } - if (! Arr::exists($responseData, 'id')) { + if (!Arr::exists($responseData, 'id')) { throw HandlingException::instance('invalid json-array: no id provided'); } $this->responseData = $responseData; } - protected function fillEntityBase(): void + protected function fillEssentials(): void { $this->fillId(); $this->fillObjectType(); + $this->fillTraitAttributes(); + } + + private function fillTraitAttributes(): void + { + $traitMapping = [ + 'FiveamCode\LaravelNotionApi\Traits\HasTimestamps' => function ($entity) { + $entity->fillTimestampableAttributes(); + }, + 'FiveamCode\LaravelNotionApi\Traits\HasParent' => function ($entity) { + $entity->fillParentAttributes(); + }, + 'FiveamCode\LaravelNotionApi\Traits\HasArchive' => function ($entity) { + $entity->fillArchivedAttributes(); + }, + ]; + + $traits = $this->class_uses_deep($this); + foreach ($traits as $trait) { + if (Arr::exists($traitMapping, $trait)) { + $traitMapping[$trait]($this); + } + } + } + + private function class_uses_deep($class, $autoload = true) + { + $traits = []; + + do { + $traits = array_merge(class_uses($class, $autoload), $traits); + } while ($class = get_parent_class($class)); + + foreach ($traits as $trait => $same) { + $traits = array_merge(class_uses($trait, $autoload), $traits); + } + + return array_unique($traits); } private function fillId() diff --git a/src/Entities/Page.php b/src/Entities/Page.php index 43888dd..c857162 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -111,15 +111,12 @@ protected function setResponseData(array $responseData): void private function fillFromRaw(): void { - parent::fillEntityBase(); + parent::fillEssentials(); $this->fillProperties(); $this->fillTitle(); // This has to be called after fillProperties(), since title is provided by properties $this->fillPageUrl(); $this->fillIcon(); $this->fillCover(); - $this->fillParentAttributes(); - $this->fillArchivedAttributes(); - $this->fillTimestampableAttributes(); } /** diff --git a/src/Entities/Properties/Property.php b/src/Entities/Properties/Property.php index 32f17cd..e647f31 100644 --- a/src/Entities/Properties/Property.php +++ b/src/Entities/Properties/Property.php @@ -62,7 +62,7 @@ protected function setResponseData(array $responseData): void protected function fillFromRaw(): void { - parent::fillEntityBase(); + parent::fillEssentials(); $this->fillType(); $this->fillContent(); } diff --git a/src/Entities/PropertyItems/SelectItem.php b/src/Entities/PropertyItems/SelectItem.php index 87f1228..1cf7cb3 100644 --- a/src/Entities/PropertyItems/SelectItem.php +++ b/src/Entities/PropertyItems/SelectItem.php @@ -36,7 +36,7 @@ protected function setResponseData(array $responseData): void protected function fillFromRaw(): void { - parent::fillEntityBase(); + parent::fillEssentials(); $this->fillName(); $this->fillColor(); } diff --git a/src/Entities/User.php b/src/Entities/User.php index 9c201fb..87ae619 100644 --- a/src/Entities/User.php +++ b/src/Entities/User.php @@ -37,7 +37,7 @@ protected function setResponseData(array $responseData): void private function fillFromRaw(): void { - parent::fillEntityBase(); + parent::fillEssentials(); $this->fillName(); $this->fillAvatarUrl(); } From 25c97d452172011d4e9ad1b77e765fedc0a6284f Mon Sep 17 00:00:00 2001 From: Di Date: Mon, 6 Feb 2023 20:09:05 +0100 Subject: [PATCH 21/21] Apply fixes from StyleCI (#117) --- src/Entities/Entity.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Entities/Entity.php b/src/Entities/Entity.php index 19ec2df..b23671f 100644 --- a/src/Entities/Entity.php +++ b/src/Entities/Entity.php @@ -50,7 +50,7 @@ public function __construct(array $responseData = null) */ protected function setResponseData(array $responseData): void { - if (!Arr::exists($responseData, 'object')) { + if (! Arr::exists($responseData, 'object')) { throw new HandlingException('invalid json-array: no object given'); } @@ -65,7 +65,7 @@ protected function setResponseData(array $responseData): void throw NotionException::instance('Not found', compact('responseData')); } - if (!Arr::exists($responseData, 'id')) { + if (! Arr::exists($responseData, 'id')) { throw HandlingException::instance('invalid json-array: no id provided'); }