From ec76cfdb030ef0303086d8e57978352bb3ec25e6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Sim=C3=A3o?= Date: Tue, 27 Jun 2023 20:01:21 -0300 Subject: [PATCH 1/3] feat(page): properties with empty values --- src/Pages/Properties/Date.php | 7 +++++++ src/Pages/Properties/Email.php | 7 +++++++ src/Pages/Properties/Number.php | 14 ++++++++++++-- src/Pages/Properties/PhoneNumber.php | 7 +++++++ src/Pages/Properties/PropertyCollection.php | 6 ++++-- src/Pages/Properties/RichTextProperty.php | 17 +++++++++++++++++ src/Pages/Properties/Select.php | 7 +++++++ src/Pages/Properties/Url.php | 12 ++++++++++++ 8 files changed, 73 insertions(+), 4 deletions(-) diff --git a/src/Pages/Properties/Date.php b/src/Pages/Properties/Date.php index 2575fb6c..48184ad8 100644 --- a/src/Pages/Properties/Date.php +++ b/src/Pages/Properties/Date.php @@ -39,6 +39,13 @@ public static function createRange(DateTimeImmutable $start, DateTimeImmutable $ return new self($property, CommonDate::createRange($start, $end)); } + public static function createEmpty(): self + { + $metadata = PropertyMetadata::create("", PropertyType::Date); + + return new self($metadata, null); + } + public static function fromArray(array $array): self { /** @psalm-var DateJson $array */ diff --git a/src/Pages/Properties/Email.php b/src/Pages/Properties/Email.php index 936da145..441d3150 100644 --- a/src/Pages/Properties/Email.php +++ b/src/Pages/Properties/Email.php @@ -26,6 +26,13 @@ public static function create(string $email): self return new self($property, $email); } + public static function createEmpty(): self + { + $property = PropertyMetadata::create("", PropertyType::Email); + + return new self($property, null); + } + public static function fromArray(array $array): self { /** @psalm-var EmailJson $array */ diff --git a/src/Pages/Properties/Number.php b/src/Pages/Properties/Number.php index c8f851e6..21e9d4cc 100644 --- a/src/Pages/Properties/Number.php +++ b/src/Pages/Properties/Number.php @@ -2,8 +2,6 @@ namespace Notion\Pages\Properties; -use Notion\Common\RichText; - /** * @psalm-type NumberJson = array{ * id: string, @@ -28,6 +26,13 @@ public static function create(int|float $number): self return new self($property, $number); } + public static function createEmpty(): self + { + $property = PropertyMetadata::create("", PropertyType::Number); + + return new self($property, null); + } + public static function fromArray(array $array): self { /** @psalm-var NumberJson $array */ @@ -58,6 +63,11 @@ public function changeNumber(int|float $number): self return new self($this->metadata, $number); } + public function clear(): self + { + return new self($this->metadata, null); + } + public function isEmpty(): bool { return $this->number === null; diff --git a/src/Pages/Properties/PhoneNumber.php b/src/Pages/Properties/PhoneNumber.php index 86e5e3df..85623caa 100644 --- a/src/Pages/Properties/PhoneNumber.php +++ b/src/Pages/Properties/PhoneNumber.php @@ -26,6 +26,13 @@ public static function create(string $phone): self return new self($property, $phone); } + public static function createEmpty(): self + { + $property = PropertyMetadata::create("", PropertyType::PhoneNumber); + + return new self($property, null); + } + public static function fromArray(array $array): self { /** @psalm-var PhoneNumberJson $array */ diff --git a/src/Pages/Properties/PropertyCollection.php b/src/Pages/Properties/PropertyCollection.php index 1c7bd9c8..4e31aaa1 100644 --- a/src/Pages/Properties/PropertyCollection.php +++ b/src/Pages/Properties/PropertyCollection.php @@ -5,9 +5,11 @@ /** @psalm-immutable */ final class PropertyCollection { - /** @param array $properties */ + /** + * @param array $properties + */ private function __construct( - private readonly array $properties + private readonly array $properties, ) { } diff --git a/src/Pages/Properties/RichTextProperty.php b/src/Pages/Properties/RichTextProperty.php index 21a5b771..b102f62c 100644 --- a/src/Pages/Properties/RichTextProperty.php +++ b/src/Pages/Properties/RichTextProperty.php @@ -39,6 +39,13 @@ public static function fromString(string $text): self return new self($metadata, $texts); } + public static function createEmpty(): self + { + $metadata = PropertyMetadata::create("", PropertyType::RichText); + + return new self($metadata, []); + } + public static function fromArray(array $array): self { /** @psalm-var RichTextPropertyMetadataJson $array */ @@ -74,6 +81,16 @@ public function changeText(RichText ...$texts): self return new self($this->metadata, $texts); } + public function clear(): self + { + return new self($this->metadata, []); + } + + public function isEmpty(): bool + { + return count($this->text) === 0; + } + public function toString(): string { $string = ""; diff --git a/src/Pages/Properties/Select.php b/src/Pages/Properties/Select.php index 6ce89778..ad3d3e26 100644 --- a/src/Pages/Properties/Select.php +++ b/src/Pages/Properties/Select.php @@ -44,6 +44,13 @@ public static function fromOption(SelectOption $option): self return new self($metadata, $option); } + public static function createEmpty(): self + { + $metadata = PropertyMetadata::create("", PropertyType::Select); + + return new self($metadata, null); + } + public static function fromArray(array $array): self { /** @psalm-var SelectJson $array */ diff --git a/src/Pages/Properties/Url.php b/src/Pages/Properties/Url.php index 3dc502a2..f0926407 100644 --- a/src/Pages/Properties/Url.php +++ b/src/Pages/Properties/Url.php @@ -26,6 +26,13 @@ public static function create(string $url): self return new self($metadata, $url); } + public static function createEmpty(): self + { + $metadata = PropertyMetadata::create("", PropertyType::Url); + + return new self($metadata, null); + } + public static function fromArray(array $array): self { /** @psalm-var UrlJson $array */ @@ -56,6 +63,11 @@ public function changeUrl(string $url): self return new self($this->metadata, $url); } + public function clear(): self + { + return new self($this->metadata, null); + } + public function isEmpty(): bool { return $this->url === null; From f38ed86130f47d2b9e015e6d273fd5f6882197ef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Sim=C3=A3o?= Date: Tue, 27 Jun 2023 20:14:06 -0300 Subject: [PATCH 2/3] Add tests --- src/Pages/Properties/Date.php | 5 +++ src/Pages/Properties/Email.php | 5 +++ src/Pages/Properties/PhoneNumber.php | 5 +++ src/Pages/Properties/Select.php | 5 +++ tests/Unit/Pages/Properties/DateTest.php | 16 +++++++++ tests/Unit/Pages/Properties/EmailTest.php | 14 ++++++++ tests/Unit/Pages/Properties/NumberTest.php | 34 +++++++++++++------ .../Unit/Pages/Properties/PhoneNumberTest.php | 14 ++++++++ .../Pages/Properties/RichTextPropertyTest.php | 14 ++++++++ tests/Unit/Pages/Properties/SelectTest.php | 16 +++++++++ tests/Unit/Pages/Properties/UrlTest.php | 14 ++++++++ 11 files changed, 132 insertions(+), 10 deletions(-) diff --git a/src/Pages/Properties/Date.php b/src/Pages/Properties/Date.php index 48184ad8..b66cfff2 100644 --- a/src/Pages/Properties/Date.php +++ b/src/Pages/Properties/Date.php @@ -86,6 +86,11 @@ public function removeEnd(): self return new self($this->metadata, $this->date?->removeEnd()); } + public function clear(): self + { + return new self($this->metadata, null); + } + public function start(): DateTimeImmutable|null { return $this->date?->start; diff --git a/src/Pages/Properties/Email.php b/src/Pages/Properties/Email.php index 441d3150..1be81c36 100644 --- a/src/Pages/Properties/Email.php +++ b/src/Pages/Properties/Email.php @@ -63,6 +63,11 @@ public function changeEmail(string $email): self return new self($this->metadata, $email); } + public function clear(): self + { + return new self($this->metadata, null); + } + public function isEmpty(): bool { return $this->email === null; diff --git a/src/Pages/Properties/PhoneNumber.php b/src/Pages/Properties/PhoneNumber.php index 85623caa..9065f51a 100644 --- a/src/Pages/Properties/PhoneNumber.php +++ b/src/Pages/Properties/PhoneNumber.php @@ -63,6 +63,11 @@ public function changePhone(string $phone): self return new self($this->metadata, $phone); } + public function clear(): self + { + return new self($this->metadata, null); + } + public function isEmpty(): bool { return $this->phone === null; diff --git a/src/Pages/Properties/Select.php b/src/Pages/Properties/Select.php index ad3d3e26..4a95e4b8 100644 --- a/src/Pages/Properties/Select.php +++ b/src/Pages/Properties/Select.php @@ -79,6 +79,11 @@ public function changeOption(SelectOption $option): self return new self($this->metadata, $option); } + public function clear(): self + { + return new self($this->metadata, null); + } + public function isEmpty(): bool { return $this->option === null; diff --git a/tests/Unit/Pages/Properties/DateTest.php b/tests/Unit/Pages/Properties/DateTest.php index 11ff343e..23ce4c31 100644 --- a/tests/Unit/Pages/Properties/DateTest.php +++ b/tests/Unit/Pages/Properties/DateTest.php @@ -34,6 +34,13 @@ public function test_create_range(): void $this->assertEquals($end, $date->end()); } + public function test_create_empty(): void + { + $date = Date::createEmpty(); + + $this->assertTrue($date->isEmpty()); + } + public function test_change_start(): void { $newStart = new DateTimeImmutable("2021-01-01"); @@ -65,6 +72,15 @@ public function test_remove_end(): void $this->assertFalse($date->isRange()); } + public function test_clear(): void + { + $someday = new DateTimeImmutable("2021-01-01"); + + $date = Date::create($someday)->clear(); + + $this->assertTrue($date->isEmpty()); + } + public function test_array_conversion(): void { $array = [ diff --git a/tests/Unit/Pages/Properties/EmailTest.php b/tests/Unit/Pages/Properties/EmailTest.php index 8bbf40d0..5c7562e4 100644 --- a/tests/Unit/Pages/Properties/EmailTest.php +++ b/tests/Unit/Pages/Properties/EmailTest.php @@ -17,6 +17,13 @@ public function test_create(): void $this->assertEquals("mario@domain.com", $email->email); } + public function test_create_empty(): void + { + $email = Email::createEmpty(); + + $this->assertTrue($email->isEmpty()); + } + public function test_change_email(): void { $email = Email::create("mario@domain.com")->changeEmail("luigi@domain.com"); @@ -24,6 +31,13 @@ public function test_change_email(): void $this->assertEquals("luigi@domain.com", $email->email); } + public function test_clear(): void + { + $email = Email::create("mario@domain.com")->clear(); + + $this->assertTrue($email->isEmpty()); + } + public function test_array_conversion(): void { $array = [ diff --git a/tests/Unit/Pages/Properties/NumberTest.php b/tests/Unit/Pages/Properties/NumberTest.php index c8982418..e7da22ba 100644 --- a/tests/Unit/Pages/Properties/NumberTest.php +++ b/tests/Unit/Pages/Properties/NumberTest.php @@ -12,11 +12,18 @@ class NumberTest extends TestCase { public function test_create(): void { - $text = Number::create(123); + $number = Number::create(123); - $this->assertEquals(123, $text->number); - $this->assertEquals("", $text->metadata()->id); - $this->assertEquals(PropertyType::Number, $text->metadata()->type); + $this->assertEquals(123, $number->number); + $this->assertEquals("", $number->metadata()->id); + $this->assertEquals(PropertyType::Number, $number->metadata()->type); + } + + public function test_create_empty(): void + { + $number = Number::createEmpty(); + + $this->assertTrue($number->isEmpty()); } public function test_array_conversion(): void @@ -27,17 +34,24 @@ public function test_array_conversion(): void "number" => 123, ]; - $text = Number::fromArray($array); + $number = Number::fromArray($array); $fromFactory = PropertyFactory::fromArray($array); - $this->assertEquals($array, $text->toArray()); + $this->assertEquals($array, $number->toArray()); $this->assertEquals($array, $fromFactory->toArray()); } public function test_change_value(): void { - $text = Number::create(123)->changeNumber(0.25); - $this->assertEquals(0.25, $text->number); + $number = Number::create(123)->changeNumber(0.25); + $this->assertEquals(0.25, $number->number); + } + + public function test_clear(): void + { + $number = Number::create(123)->clear(); + + $this->assertTrue($number->isEmpty()); } public function test_is_empty(): void @@ -48,8 +62,8 @@ public function test_is_empty(): void "number" => null, ]; - $text = Number::fromArray($array); + $number = Number::fromArray($array); - $this->assertTrue($text->isEmpty()); + $this->assertTrue($number->isEmpty()); } } diff --git a/tests/Unit/Pages/Properties/PhoneNumberTest.php b/tests/Unit/Pages/Properties/PhoneNumberTest.php index e9f9ad9a..b3d31dcd 100644 --- a/tests/Unit/Pages/Properties/PhoneNumberTest.php +++ b/tests/Unit/Pages/Properties/PhoneNumberTest.php @@ -17,6 +17,13 @@ public function test_create(): void $this->assertEquals("415-000-1111", $phone->phone); } + public function test_create_empty(): void + { + $phone = PhoneNumber::createEmpty(); + + $this->assertTrue($phone->isEmpty()); + } + public function test_change_phone(): void { $phone = PhoneNumber::create("415-000-1111")->changePhone("415-000-2222"); @@ -24,6 +31,13 @@ public function test_change_phone(): void $this->assertEquals("415-000-2222", $phone->phone); } + public function test_clear(): void + { + $phone = PhoneNumber::create("415-000-1111")->clear(); + + $this->assertTrue($phone->isEmpty()); + } + public function test_array_conversion(): void { $array = [ diff --git a/tests/Unit/Pages/Properties/RichTextPropertyTest.php b/tests/Unit/Pages/Properties/RichTextPropertyTest.php index f08bc0ce..f647e508 100644 --- a/tests/Unit/Pages/Properties/RichTextPropertyTest.php +++ b/tests/Unit/Pages/Properties/RichTextPropertyTest.php @@ -19,6 +19,13 @@ public function test_create(): void $this->assertTrue($text->metadata()->type === PropertyType::RichText); } + public function test_create_empty(): void + { + $text = RichTextProperty::createEmpty(); + + $this->assertTrue($text->isEmpty()); + } + public function test_array_conversion(): void { $array = [ @@ -62,4 +69,11 @@ public function test_change_text(): void ); $this->assertEquals("Dummy text", $text->toString()); } + + public function test_clear(): void + { + $text = RichTextProperty::fromString("Dummy text")->clear(); + + $this->assertTrue($text->isEmpty()); + } } diff --git a/tests/Unit/Pages/Properties/SelectTest.php b/tests/Unit/Pages/Properties/SelectTest.php index a81a9bc3..acd34989 100644 --- a/tests/Unit/Pages/Properties/SelectTest.php +++ b/tests/Unit/Pages/Properties/SelectTest.php @@ -40,6 +40,13 @@ public function test_create_from_option(): void $this->assertEquals("abc", $select->option?->id); } + public function test_create_empty(): void + { + $select = Select::createEmpty(); + + $this->assertTrue($select->isEmpty()); + } + public function test_change_option(): void { $optionA = SelectOption::fromId("abc"); @@ -51,6 +58,15 @@ public function test_change_option(): void $this->assertEquals($optionB, $select->option); } + public function test_clear(): void + { + $option = SelectOption::fromId("abc"); + + $select = Select::fromOption($option)->clear(); + + $this->assertTrue($select->isEmpty()); + } + public function test_array_conversion(): void { $array = [ diff --git a/tests/Unit/Pages/Properties/UrlTest.php b/tests/Unit/Pages/Properties/UrlTest.php index b9e598fe..fda1d807 100644 --- a/tests/Unit/Pages/Properties/UrlTest.php +++ b/tests/Unit/Pages/Properties/UrlTest.php @@ -17,6 +17,13 @@ public function test_create(): void $this->assertEquals("https://notion.so", $url->url); } + public function test_create_empty(): void + { + $url = Url::createEmpty(); + + $this->assertTrue($url->isEmpty()); + } + public function test_change_url(): void { $url = Url::create("https://notion.so")->changeUrl("https://google.com"); @@ -24,6 +31,13 @@ public function test_change_url(): void $this->assertEquals("https://google.com", $url->url); } + public function test_clear(): void + { + $url = Url::create("https://notion.so")->clear(); + + $this->assertTrue($url->isEmpty()); + } + public function test_array_conversion(): void { $array = [ From 2ea93b2941d2eeda03ad2ed9a948c51afb75d2ae Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Sim=C3=A3o?= Date: Tue, 27 Jun 2023 20:15:17 -0300 Subject: [PATCH 3/3] Changelog --- CHANGELOG.md | 15 +++++++++------ 1 file changed, 9 insertions(+), 6 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2ea6265c..a4efd6d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,23 +6,26 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Added +- Page properties with empty values (#260) + ## [v1.9.0] 2023-06-15 -## Added +### Added - Caption on image block and file objects (#250) -## Fixed +### Fixed - Do not update CreatedBy prop on pages (#251) ## [v1.8.1] 2023-06-12 -## Fixed +### Fixed - Rich Text mention creation (#241) - Possible null pointer on RichText (#246) - Change rich text URL - Only send to API file name when set (#234) -## Build +### Build - Bump phpunit/phpunit from 10.1.2 to 10.1.3 (#227) - Bump guzzlehttp/guzzle from 7.5.1 to 7.6.1 (#226) - Bump php-http/discovery from 1.18.0 to 1.18.1 (#228) @@ -32,10 +35,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Bump vite from 3.1.3 to 3.2.7 in /docs (#236) - Bump phpunit/phpunit from 10.1.3 to 10.2.2 (#244) -## Test +### Test - Fix file-related tests (#245) -## Chore +### Chore - Add sponsors to README (#237) ## [v1.8.0] 2023-05-09