Skip to content

Commit

Permalink
Add select page property
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 27, 2021
1 parent 7303d0f commit 496f198
Show file tree
Hide file tree
Showing 4 changed files with 176 additions and 5 deletions.
4 changes: 3 additions & 1 deletion src/Pages/Properties/Factory.php
Expand Up @@ -14,8 +14,10 @@ public static function fromArray(array $array): PropertyInterface
$type = $array["type"];

return match($type) {
Property::TYPE_TITLE => Title::fromArray($array),
Property::TYPE_TITLE => Title::fromArray($array),
Property::TYPE_RICH_TEXT => RichTextProperty::fromArray($array),
Property::TYPE_NUMBER => Number::fromArray($array),
Property::TYPE_SELECT => Select::fromArray($array),
default => throw new Exception("Invalid property type: '{$type}'"),
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/Pages/Properties/Property.php
Expand Up @@ -81,10 +81,10 @@ public function isNumber(): bool
return $this->type === self::TYPE_NUMBER;
}

// public function isSelect(): bool
// {
// return $this->type === self::TYPE_SELECT;
// }
public function isSelect(): bool
{
return $this->type === self::TYPE_SELECT;
}

// public function isMultiSelect(): bool
// {
Expand Down
105 changes: 105 additions & 0 deletions src/Pages/Properties/Select.php
@@ -0,0 +1,105 @@
<?php

namespace Notion\Pages\Properties;

/**
* @psalm-type SelectJson = array{
* id: string,
* type: "select",
* select: array{ id: string, name: string, color: string }
* }
*/
class Select implements PropertyInterface
{
public const COLOR_DEFAULT = "default";
public const COLOR_GRAY = "gray";
public const COLOR_BROWN = "brown";
public const COLOR_RED = "red";
public const COLOR_ORANGE = "orange";
public const COLOR_YELLOW = "yellow";
public const COLOR_GREEN = "green";
public const COLOR_BLUE = "blue";
public const COLOR_PURPLE = "purple";
public const COLOR_PINK = "pink";

private const TYPE = Property::TYPE_SELECT;

private Property $property;

private string|null $id;
private string|null $name;

private function __construct(Property $property, string|null $id, string|null $name)
{
$this->property = $property;
$this->id = $id;
$this->name = $name;
}

public static function fromId(string $id): self
{
$property = Property::create("", self::TYPE);

return new self($property, $id, null);
}

public static function fromName(string $name): self
{
$property = Property::create("", self::TYPE);

return new self($property, null, $name);
}

public static function fromArray(array $array): self
{
/** @psalm-var SelectJson $array */

$property = Property::fromArray($array);

$id = $array[self::TYPE]["id"] ?? null;
$name = $array[self::TYPE]["name"] ?? null;

return new self($property, $id, $name);
}

public function toArray(): array
{
$array = $this->property->toArray();

$select = [];
if ($this->name !== null) {
$select["name"] = $this->name;
}
if ($this->id !== null) {
$select["id"] = $this->id;
}
$array[self::TYPE] = $select;

return $array;
}

public function property(): Property
{
return $this->property;
}

public function id(): string|null
{
return $this->id;
}

public function withId(string $id): self
{
return new self($this->property, $id, $this->name);
}

public function name(): string|null
{
return $this->name;
}

public function withName(string $name): self
{
return new self($this->property, $this->id, $name);
}
}
64 changes: 64 additions & 0 deletions tests/Unit/Pages/Properties/SelectTest.php
@@ -0,0 +1,64 @@
<?php

namespace Notion\Test\Unit\Pages\Properties;

use Notion\Pages\Properties\Factory;
use Notion\Pages\Properties\Select;
use PHPUnit\Framework\TestCase;

class SelectTest extends TestCase
{
public function test_create_from_option_id(): void
{
$select = Select::fromId("e69017d3-9027-46c4-9b6f-490d243e459b");

$this->assertEquals("e69017d3-9027-46c4-9b6f-490d243e459b", $select->id());
$this->assertNull($select->name());
$this->assertEquals("", $select->property()->id());
$this->assertEquals("select", $select->property()->type());
$this->assertTrue($select->property()->isSelect());
}

public function test_create_from_option_name(): void
{
$select = Select::fromName("Option A");

$this->assertEquals("Option A", $select->name());
$this->assertNull($select->id());
$this->assertEquals("", $select->property()->id());
$this->assertEquals("select", $select->property()->type());
$this->assertTrue($select->property()->isSelect());
}

public function test_array_conversion(): void
{
$array = [
"id" => "a7ede3b7-c7ae-4eb8-b415-a7f80ac4dfe5",
"type" => "select",
"select" => [
"name" => "Option A",
"id" => "ad762674-9280-444b-96a7-3a0fb0aefff9",
],
];

$select = Select::fromArray($array);
$fromFactory = Factory::fromArray($array);

$this->assertEquals($array, $select->toArray());
$this->assertEquals($array, $fromFactory->toArray());
}

public function test_change_option_name(): void
{
$select = Select::fromName("Option A")->withName("Option B");
$this->assertEquals("Option B", $select->name());
}

public function test_change_option_id(): void
{
$select = Select::fromId("ad3d06a7-4245-4c71-9bf4-f285b251f92e")
->withId("ad762674-9280-444b-96a7-3a0fb0aefff9");

$this->assertEquals("ad762674-9280-444b-96a7-3a0fb0aefff9", $select->id());
}
}

0 comments on commit 496f198

Please sign in to comment.