diff --git a/src/Databases/Properties/Factory.php b/src/Databases/Properties/Factory.php index da478fc6..a84abfe9 100644 --- a/src/Databases/Properties/Factory.php +++ b/src/Databases/Properties/Factory.php @@ -15,6 +15,7 @@ public static function fromArray(array $array): PropertyInterface return match($type) { Property::TYPE_TITLE => Title::fromArray($array), + Property::TYPE_RICH_TEXT => RichText::fromArray($array), default => throw new Exception("Invalid property type: '{$type}'"), }; } diff --git a/src/Databases/Properties/Property.php b/src/Databases/Properties/Property.php index 45a92628..e41fd493 100644 --- a/src/Databases/Properties/Property.php +++ b/src/Databases/Properties/Property.php @@ -80,10 +80,10 @@ public function type(): string return $this->type; } - // public function isRichText(): bool - // { - // return $this->type === self::TYPE_RICH_TEXT; - // } + public function isRichText(): bool + { + return $this->type === self::TYPE_RICH_TEXT; + } // public function isNumber(): bool // { diff --git a/src/Databases/Properties/RichText.php b/src/Databases/Properties/RichText.php new file mode 100644 index 00000000..d080a5aa --- /dev/null +++ b/src/Databases/Properties/RichText.php @@ -0,0 +1,51 @@ +, + * } + */ +class RichText implements PropertyInterface +{ + private const TYPE = Property::TYPE_RICH_TEXT; + + private Property $property; + + private function __construct(Property $property) + { + $this->property = $property; + } + + public static function create(string $propertyName): self + { + $property = Property::create("", $propertyName, self::TYPE); + + return new self($property); + } + + public function property(): Property + { + return $this->property; + } + + public static function fromArray(array $array): self + { + /** @psalm-var RichTextJson $array */ + $property = Property::fromArray($array); + + return new self($property); + } + + public function toArray(): array + { + $array = $this->property->toArray(); + $array[self::TYPE] = []; + + return $array; + } +} diff --git a/tests/Unit/Databases/Properties/RichTextTest.php b/tests/Unit/Databases/Properties/RichTextTest.php new file mode 100644 index 00000000..be945498 --- /dev/null +++ b/tests/Unit/Databases/Properties/RichTextTest.php @@ -0,0 +1,33 @@ +assertEquals("Dummy prop name", $text->property()->name()); + $this->assertTrue($text->property()->isRichText()); + } + + public function test_array_conversion(): void + { + $array = [ + "id" => "abc", + "name" => "dummy", + "type" => "rich_text", + "rich_text" => [], + ]; + $text = RichText::fromArray($array); + $fromFactory = Factory::fromArray($array); + + $this->assertEquals($array, $text->toArray()); + $this->assertEquals($array, $fromFactory->toArray()); + } +}