Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7303d0f
commit 496f198
Showing
4 changed files
with
176 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()); | ||
} | ||
} |