Skip to content

Commit

Permalink
Add color to select property
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 27, 2021
1 parent 496f198 commit 0a5f2f2
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 12 deletions.
36 changes: 26 additions & 10 deletions src/Pages/Properties/Select.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,45 +28,51 @@ class Select implements PropertyInterface

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

private function __construct(Property $property, string|null $id, string|null $name)
{
private string $color;

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

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

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

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

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

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;
$color = $array[self::TYPE]["color"];

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

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

$select = [];
$select = [ "color" => $this->color ];
if ($this->name !== null) {
$select["name"] = $this->name;
}
Expand All @@ -90,7 +96,7 @@ public function id(): string|null

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

public function name(): string|null
Expand All @@ -100,6 +106,16 @@ public function name(): string|null

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

public function color(): string
{
return $this->color;
}

public function withColor(string $color): self
{
return new self($this->property, $this->id, $this->name, $color);
}
}
12 changes: 10 additions & 2 deletions tests/Unit/Pages/Properties/SelectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ public function test_array_conversion(): void
"id" => "a7ede3b7-c7ae-4eb8-b415-a7f80ac4dfe5",
"type" => "select",
"select" => [
"name" => "Option A",
"id" => "ad762674-9280-444b-96a7-3a0fb0aefff9",
"name" => "Option A",
"id" => "ad762674-9280-444b-96a7-3a0fb0aefff9",
"color" => "default",
],
];

Expand All @@ -61,4 +62,11 @@ public function test_change_option_id(): void

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

public function test_change_option_color(): void
{
$select = Select::fromName("Option A")->withColor("red");

$this->assertEquals("red", $select->color());
}
}

0 comments on commit 0a5f2f2

Please sign in to comment.