From 496f1984756c67474ade3bdae08753c2918dcd90 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Sim=C3=A3o?= Date: Wed, 27 Oct 2021 14:11:55 -0300 Subject: [PATCH] Add select page property --- src/Pages/Properties/Factory.php | 4 +- src/Pages/Properties/Property.php | 8 +- src/Pages/Properties/Select.php | 105 +++++++++++++++++++++ tests/Unit/Pages/Properties/SelectTest.php | 64 +++++++++++++ 4 files changed, 176 insertions(+), 5 deletions(-) create mode 100644 src/Pages/Properties/Select.php create mode 100644 tests/Unit/Pages/Properties/SelectTest.php diff --git a/src/Pages/Properties/Factory.php b/src/Pages/Properties/Factory.php index 4e62c571..183a2c6b 100644 --- a/src/Pages/Properties/Factory.php +++ b/src/Pages/Properties/Factory.php @@ -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}'"), }; } diff --git a/src/Pages/Properties/Property.php b/src/Pages/Properties/Property.php index dc718177..5dbb28aa 100644 --- a/src/Pages/Properties/Property.php +++ b/src/Pages/Properties/Property.php @@ -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 // { diff --git a/src/Pages/Properties/Select.php b/src/Pages/Properties/Select.php new file mode 100644 index 00000000..1b9a4490 --- /dev/null +++ b/src/Pages/Properties/Select.php @@ -0,0 +1,105 @@ +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); + } +} diff --git a/tests/Unit/Pages/Properties/SelectTest.php b/tests/Unit/Pages/Properties/SelectTest.php new file mode 100644 index 00000000..022a4a8c --- /dev/null +++ b/tests/Unit/Pages/Properties/SelectTest.php @@ -0,0 +1,64 @@ +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()); + } +}