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
bbed82c
commit 555fdb8
Showing
4 changed files
with
124 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
<?php | ||
|
||
namespace Notion\Pages\Properties; | ||
|
||
/** | ||
* @psalm-type CheckboxJson = array{ | ||
* id: string, | ||
* type: "checkbox", | ||
* checkbox: bool, | ||
* } | ||
*/ | ||
class Checkbox implements PropertyInterface | ||
{ | ||
private const TYPE = Property::TYPE_CHECKBOX; | ||
|
||
private Property $property; | ||
|
||
private bool $checked; | ||
|
||
private function __construct(Property $property, bool $checked) | ||
{ | ||
$this->property = $property; | ||
$this->checked = $checked; | ||
} | ||
|
||
public static function create(bool $checked = false): self | ||
{ | ||
$property = Property::create("", self::TYPE); | ||
|
||
return new self($property, $checked); | ||
} | ||
|
||
public static function fromArray(array $array): self | ||
{ | ||
/** @psalm-var CheckboxJson $array */ | ||
|
||
$property = Property::fromArray($array); | ||
|
||
$checked = $array[self::TYPE]; | ||
|
||
return new self($property, $checked); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$array = $this->property->toArray(); | ||
|
||
$array[self::TYPE] = $this->checked; | ||
|
||
return $array; | ||
} | ||
|
||
public function property(): Property | ||
{ | ||
return $this->property; | ||
} | ||
|
||
public function isChecked(): bool | ||
{ | ||
return $this->checked; | ||
} | ||
|
||
public function check(): self | ||
{ | ||
return new self($this->property, true); | ||
} | ||
|
||
public function uncheck(): self | ||
{ | ||
return new self($this->property, false); | ||
} | ||
} |
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,47 @@ | ||
<?php | ||
|
||
namespace Notion\Test\Unit\Pages\Properties; | ||
|
||
use Notion\Pages\Properties\Checkbox; | ||
use Notion\Pages\Properties\Factory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class CheckboxTest extends TestCase | ||
{ | ||
public function test_create(): void | ||
{ | ||
$checkbox = Checkbox::create(true); | ||
|
||
$this->assertTrue($checkbox->property()->isCheckbox()); | ||
$this->assertTrue($checkbox->isChecked()); | ||
} | ||
|
||
public function test_check(): void | ||
{ | ||
$checkbox = Checkbox::create()->check(); | ||
|
||
$this->assertTrue($checkbox->isChecked()); | ||
} | ||
|
||
public function test_uncheck(): void | ||
{ | ||
$checkbox = Checkbox::create(true)->uncheck(); | ||
|
||
$this->assertFalse($checkbox->isChecked()); | ||
} | ||
|
||
public function test_array_conversion(): void | ||
{ | ||
$array = [ | ||
"id" => "abc", | ||
"type" => "checkbox", | ||
"checkbox" => true, | ||
]; | ||
|
||
$checkbox = Checkbox::fromArray($array); | ||
$fromFactory = Factory::fromArray($array); | ||
|
||
$this->assertEquals($array, $checkbox->toArray()); | ||
$this->assertEquals($array, $fromFactory->toArray()); | ||
} | ||
} |