From de705ae78a006c81a485276111cea929990e3c10 Mon Sep 17 00:00:00 2001 From: johguentner Date: Thu, 2 Feb 2023 15:00:35 +0100 Subject: [PATCH 1/3] polish page and properties - add parent information to page - check end-date of Date property for null - add "asText" method to Text and Title property --- src/Entities/Page.php | 44 ++++++++++++++++++++++++++++++- src/Entities/Properties/Date.php | 2 +- src/Entities/Properties/Text.php | 10 ++++++- src/Entities/Properties/Title.php | 8 ++++++ 4 files changed, 61 insertions(+), 3 deletions(-) diff --git a/src/Entities/Page.php b/src/Entities/Page.php index 77e6609..53747eb 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -18,6 +18,7 @@ use FiveamCode\LaravelNotionApi\Entities\Properties\Url; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use Illuminate\Support\Arr; +use Illuminate\Support\Str; use Illuminate\Support\Collection; /** @@ -55,6 +56,16 @@ class Page extends Entity */ private string $coverType = ''; + /** + * @var string + */ + private string $parentId = ''; + + /** + * @var string + */ + private string $parentType = ''; + /** * @var string */ @@ -122,6 +133,7 @@ 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 @@ -205,6 +217,20 @@ 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 * @param $property @@ -443,7 +469,7 @@ public function getProperties(): Collection */ public function getProperty(string $propertyKey): ?Property { - if (! isset($this->propertyMap[$propertyKey])) { + if (!isset($this->propertyMap[$propertyKey])) { return null; } @@ -458,6 +484,22 @@ 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/Entities/Properties/Date.php b/src/Entities/Properties/Date.php index 5a7fc1f..332d1f2 100644 --- a/src/Entities/Properties/Date.php +++ b/src/Entities/Properties/Date.php @@ -97,7 +97,7 @@ protected function fillDate(): void $richDate->setHasTime($this->isIsoTimeString($startAsIsoString)); } - if (Arr::exists($this->rawContent, 'end')) { + if (Arr::exists($this->rawContent, 'end') && $this->rawContent['end'] !== null) { $endAsIsoString = $this->rawContent['end']; $richDate->setEnd(new DateTime($endAsIsoString)); } diff --git a/src/Entities/Properties/Text.php b/src/Entities/Properties/Text.php index 7fad01c..3d96cbb 100644 --- a/src/Entities/Properties/Text.php +++ b/src/Entities/Properties/Text.php @@ -56,7 +56,7 @@ public static function value($text): Text protected function fillFromRaw(): void { parent::fillFromRaw(); - if (! is_array($this->rawContent)) { + if (!is_array($this->rawContent)) { throw HandlingException::instance('The property-type is text, however the raw data-structure does not represent this type (= array of items). Please check the raw response-data.'); } @@ -77,6 +77,14 @@ public function getContent(): RichText return $this->getRichText(); } + /** + * @return string + */ + public function asText(): string + { + return $this->getPlainText(); + } + /** * @return RichText */ diff --git a/src/Entities/Properties/Title.php b/src/Entities/Properties/Title.php index 4962a06..5d7e502 100644 --- a/src/Entities/Properties/Title.php +++ b/src/Entities/Properties/Title.php @@ -76,6 +76,14 @@ public function getContent(): RichText return $this->getRichText(); } + /** + * @return string + */ + public function asText(): string + { + return $this->getPlainText(); + } + /** * @return RichText */ From 8542f79fc83ff4221d3a0ff06686ef521c0cc027 Mon Sep 17 00:00:00 2001 From: Di Date: Thu, 2 Feb 2023 15:01:00 +0100 Subject: [PATCH 2/3] Apply fixes from StyleCI (#102) --- src/Entities/Page.php | 3 +-- src/Entities/Properties/Text.php | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Entities/Page.php b/src/Entities/Page.php index 53747eb..08d03d1 100644 --- a/src/Entities/Page.php +++ b/src/Entities/Page.php @@ -18,7 +18,6 @@ use FiveamCode\LaravelNotionApi\Entities\Properties\Url; use FiveamCode\LaravelNotionApi\Exceptions\HandlingException; use Illuminate\Support\Arr; -use Illuminate\Support\Str; use Illuminate\Support\Collection; /** @@ -469,7 +468,7 @@ public function getProperties(): Collection */ public function getProperty(string $propertyKey): ?Property { - if (!isset($this->propertyMap[$propertyKey])) { + if (! isset($this->propertyMap[$propertyKey])) { return null; } diff --git a/src/Entities/Properties/Text.php b/src/Entities/Properties/Text.php index 3d96cbb..84df2e4 100644 --- a/src/Entities/Properties/Text.php +++ b/src/Entities/Properties/Text.php @@ -56,7 +56,7 @@ public static function value($text): Text protected function fillFromRaw(): void { parent::fillFromRaw(); - if (!is_array($this->rawContent)) { + if (! is_array($this->rawContent)) { throw HandlingException::instance('The property-type is text, however the raw data-structure does not represent this type (= array of items). Please check the raw response-data.'); } From ef0daae54b02bfc12b389860a22d57b998c813b1 Mon Sep 17 00:00:00 2001 From: johguentner Date: Thu, 2 Feb 2023 15:34:04 +0100 Subject: [PATCH 3/3] add tests which check properties/methods - of page parent information - of Title and Text property (added: asText()) --- tests/EndpointPagesTest.php | 2 ++ tests/PagePropertyTest.php | 2 ++ 2 files changed, 4 insertions(+) diff --git a/tests/EndpointPagesTest.php b/tests/EndpointPagesTest.php index cd33a19..770e62c 100644 --- a/tests/EndpointPagesTest.php +++ b/tests/EndpointPagesTest.php @@ -72,6 +72,8 @@ public function it_returns_page_entity_with_filled_properties() $this->assertCount(9, $pageResult->getRawProperties()); $this->assertCount(9, $pageResult->getProperties()); $this->assertCount(9, $pageResult->getPropertyKeys()); + $this->assertSame('database_id', $pageResult->getParentType()); + $this->assertSame('f2939732-f694-4ce2-b613-f28db6ded673', $pageResult->getParentId()); // check date and datetime properties $this->assertTrue($pageResult->getProperty('DateWithTime')->hasTime()); diff --git a/tests/PagePropertyTest.php b/tests/PagePropertyTest.php index bf02ace..739dbf2 100644 --- a/tests/PagePropertyTest.php +++ b/tests/PagePropertyTest.php @@ -99,6 +99,7 @@ public function it_checks_if_specific_page_property_is_a_valid_text_property() $this->assertSame('text', $text->getType()); $this->assertSame('|Zt@', $text->getId()); $this->assertSame('this is a nice Text :-)', $text->getPlainText()); + $this->assertSame('this is a nice Text :-)', $text->asText()); $this->assertInstanceOf(RichText::class, $text->getRichText()); $this->assertInstanceOf(RichText::class, $text->getContent()); $this->assertSame('this is a nice Text :-)', $text->getRichText()->getPlainText()); @@ -149,6 +150,7 @@ public function it_checks_if_specific_page_property_is_a_valid_title_property() $this->assertSame('title', $title->getType()); $this->assertSame('title', $title->getId()); $this->assertSame('Notion Is Awesome', $title->getPlainText()); + $this->assertSame('Notion Is Awesome', $title->asText()); $this->assertInstanceOf(RichText::class, $title->getContent()); $this->assertSame('Notion Is Awesome', $title->getContent()->getPlainText()); }