From 3083365690b2710213309b60f598b93cb5fcfb6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mario=20Sim=C3=A3o?= Date: Wed, 27 Oct 2021 15:09:12 -0300 Subject: [PATCH] Add multi select page property --- src/Pages/Properties/Factory.php | 1 + src/Pages/Properties/MultiSelect.php | 82 +++++++++++++++++++ src/Pages/Properties/Property.php | 8 +- .../Unit/Pages/Properties/MultiSelectTest.php | 61 ++++++++++++++ 4 files changed, 148 insertions(+), 4 deletions(-) create mode 100644 src/Pages/Properties/MultiSelect.php create mode 100644 tests/Unit/Pages/Properties/MultiSelectTest.php diff --git a/src/Pages/Properties/Factory.php b/src/Pages/Properties/Factory.php index 183a2c6b..a0977f9e 100644 --- a/src/Pages/Properties/Factory.php +++ b/src/Pages/Properties/Factory.php @@ -18,6 +18,7 @@ public static function fromArray(array $array): PropertyInterface Property::TYPE_RICH_TEXT => RichTextProperty::fromArray($array), Property::TYPE_NUMBER => Number::fromArray($array), Property::TYPE_SELECT => Select::fromArray($array), + Property::TYPE_MULTI_SELECT => MultiSelect::fromArray($array), default => throw new Exception("Invalid property type: '{$type}'"), }; } diff --git a/src/Pages/Properties/MultiSelect.php b/src/Pages/Properties/MultiSelect.php new file mode 100644 index 00000000..3cd3dd74 --- /dev/null +++ b/src/Pages/Properties/MultiSelect.php @@ -0,0 +1,82 @@ +property = $property; + $this->options = $options; + } + + public static function fromIds(string ...$ids): self + { + $property = Property::create("", self::TYPE); + $options = array_map(fn(string $id) => Option::fromId($id), $ids); + + return new self($property, $options); + } + + public static function fromNames(string ...$names): self + { + $property = Property::create("", self::TYPE); + $options = array_map(fn(string $name) => Option::fromName($name), $names); + + return new self($property, $options); + } + + public static function fromArray(array $array): self + { + /** @psalm-var MultiSelectJson $array */ + $property = Property::fromArray($array); + + $options = array_map(fn(array $option) => Option::fromArray($option), $array[self::TYPE]); + + return new self($property, $options); + } + + public function toArray(): array + { + $array = $this->property->toArray(); + $array[self::TYPE] = array_map(fn (Option $option) => $option->toArray(), $this->options); + + return $array; + } + + public function property(): Property + { + return $this->property; + } + + /** @return Option[] */ + public function options(): array + { + return $this->options; + } + + public function addOption(Option $option): self + { + $options = $this->options; + $options[] = $option; + + return new self($this->property, $options); + } +} diff --git a/src/Pages/Properties/Property.php b/src/Pages/Properties/Property.php index 5dbb28aa..673e4de6 100644 --- a/src/Pages/Properties/Property.php +++ b/src/Pages/Properties/Property.php @@ -86,10 +86,10 @@ public function isSelect(): bool return $this->type === self::TYPE_SELECT; } - // public function isMultiSelect(): bool - // { - // return $this->type === self::TYPE_MULTI_SELECT; - // } + public function isMultiSelect(): bool + { + return $this->type === self::TYPE_MULTI_SELECT; + } // public function isDate(): bool // { diff --git a/tests/Unit/Pages/Properties/MultiSelectTest.php b/tests/Unit/Pages/Properties/MultiSelectTest.php new file mode 100644 index 00000000..d1146fd6 --- /dev/null +++ b/tests/Unit/Pages/Properties/MultiSelectTest.php @@ -0,0 +1,61 @@ +assertEquals($id1, $multiSelect->options()[0]->id()); + $this->assertEquals($id2, $multiSelect->options()[1]->id()); + + $this->assertTrue($multiSelect->property()->isMultiSelect()); + } + + public function test_create_from_names(): void + { + $optionA = "Option A"; + $optionC = "Option C"; + + $multiSelect = MultiSelect::fromNames($optionA, $optionC); + + $this->assertEquals($optionA, $multiSelect->options()[0]->name()); + $this->assertEquals($optionC, $multiSelect->options()[1]->name()); + } + + public function test_array_conversion(): void + { + $array = [ + "id" => "931db25b-f8af-4fc0-b7bf-eb9c29de6b87", + "type" => "multi_select", + "multi_select" => [ + [ "name" => "Option A", "color" => "red" ], + [ "name" => "Option C", "color" => "blue" ], + ], + ]; + + $multiSelect = MultiSelect::fromArray($array); + $fromFactory = Factory::fromArray($array); + + $this->assertEquals($array, $multiSelect->toArray()); + $this->assertEquals($array, $fromFactory->toArray()); + } + + public function test_add_option(): void + { + $multiSelect = MultiSelect::fromNames("Option A", "Option B") + ->addOption(Option::fromName("Option C")); + + $this->assertCount(3, $multiSelect->options()); + } +}