Skip to content

Commit

Permalink
Add checkbox page property
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 31, 2021
1 parent bbed82c commit 555fdb8
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 4 deletions.
72 changes: 72 additions & 0 deletions src/Pages/Properties/Checkbox.php
@@ -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);
}
}
1 change: 1 addition & 0 deletions src/Pages/Properties/Factory.php
Expand Up @@ -22,6 +22,7 @@ public static function fromArray(array $array): PropertyInterface
Property::TYPE_FORMULA => Formula::fromArray($array),
Property::TYPE_RELATION => Relation::fromArray($array),
Property::TYPE_PEOPLE => People::fromArray($array),
Property::TYPE_CHECKBOX => Checkbox::fromArray($array),
default => throw new Exception("Invalid property type: '{$type}'"),
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/Pages/Properties/Property.php
Expand Up @@ -126,10 +126,10 @@ public function isPeople(): bool
// return $this->type === self::TYPE_FILES;
// }

// public function isCheckbox(): bool
// {
// return $this->type === self::TYPE_CHECKBOX;
// }
public function isCheckbox(): bool
{
return $this->type === self::TYPE_CHECKBOX;
}

// public function isUrl(): bool
// {
Expand Down
47 changes: 47 additions & 0 deletions tests/Unit/Pages/Properties/CheckboxTest.php
@@ -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());
}
}

0 comments on commit 555fdb8

Please sign in to comment.