Skip to content

Commit

Permalink
Add rich text database property
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 31, 2021
1 parent b122ef4 commit 507e4ef
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Databases/Properties/Factory.php
Expand Up @@ -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}'"),
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/Databases/Properties/Property.php
Expand Up @@ -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
// {
Expand Down
51 changes: 51 additions & 0 deletions src/Databases/Properties/RichText.php
@@ -0,0 +1,51 @@
<?php

namespace Notion\Databases\Properties;

/**
* @psalm-type RichTextJson = array{
* id: string,
* name: string,
* type: "rich_text",
* rich_text: array<empty, empty>,
* }
*/
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;
}
}
33 changes: 33 additions & 0 deletions tests/Unit/Databases/Properties/RichTextTest.php
@@ -0,0 +1,33 @@
<?php

namespace Notion\Test\Unit\Databases\Properties;

use Notion\Databases\Properties\Factory;
use Notion\Databases\Properties\RichText;
use PHPUnit\Framework\TestCase;

class RichTextTest extends TestCase
{
public function test_create(): void
{
$text = RichText::create("Dummy prop name");

$this->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());
}
}

0 comments on commit 507e4ef

Please sign in to comment.