Skip to content

Commit

Permalink
Add phone number page property
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 31, 2021
1 parent 0ec3b39 commit bfcedac
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Pages/Properties/Factory.php
Expand Up @@ -25,6 +25,7 @@ public static function fromArray(array $array): PropertyInterface
Property::TYPE_CHECKBOX => Checkbox::fromArray($array),
Property::TYPE_URL => Url::fromArray($array),
Property::TYPE_EMAIL => Email::fromArray($array),
Property::TYPE_PHONE_NUMBER => PhoneNumber::fromArray($array),
default => throw new Exception("Invalid property type: '{$type}'"),
};
}
Expand Down
67 changes: 67 additions & 0 deletions src/Pages/Properties/PhoneNumber.php
@@ -0,0 +1,67 @@
<?php

namespace Notion\Pages\Properties;

/**
* @psalm-type PhoneNumberJson = array{
* id: string,
* type: "phone_number",
* phone_number: string,
* }
*/
class PhoneNumber implements PropertyInterface
{
private const TYPE = Property::TYPE_PHONE_NUMBER;

private Property $property;

private string $phone;

private function __construct(Property $property, string $phone)
{
$this->property = $property;
$this->phone = $phone;
}

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

return new self($property, $phone);
}

public static function fromArray(array $array): self
{
/** @psalm-var PhoneNumberJson $array */

$property = Property::fromArray($array);

$phone = $array[self::TYPE];

return new self($property, $phone);
}

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

$array[self::TYPE] = $this->phone;

return $array;
}

public function property(): Property
{
return $this->property;
}

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

public function withPhone(string $phone): self
{
return new self($this->property, $phone);
}
}
8 changes: 4 additions & 4 deletions src/Pages/Properties/Property.php
Expand Up @@ -141,10 +141,10 @@ public function isEmail(): bool
return $this->type === self::TYPE_EMAIL;
}

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

// public function isCreatedTime(): bool
// {
Expand Down
40 changes: 40 additions & 0 deletions tests/Unit/Pages/Properties/PhoneNumberTest.php
@@ -0,0 +1,40 @@
<?php

namespace Notion\Test\Unit\Pages\Properties;

use Notion\Pages\Properties\PhoneNumber;
use Notion\Pages\Properties\Factory;
use PHPUnit\Framework\TestCase;

class PhoneNumberTest extends TestCase
{
public function test_create(): void
{
$phone = PhoneNumber::create("415-000-1111");

$this->assertTrue($phone->property()->isPhoneNumber());
$this->assertEquals("415-000-1111", $phone->phone());
}

public function test_change_phone(): void
{
$phone = PhoneNumber::create("415-000-1111")->withPhone("415-000-2222");

$this->assertEquals("415-000-2222", $phone->phone());
}

public function test_array_conversion(): void
{
$array = [
"id" => "abc",
"type" => "phone_number",
"phone_number" => "415-000-1111",
];

$phone = PhoneNumber::fromArray($array);
$fromFactory = Factory::fromArray($array);

$this->assertEquals($array, $phone->toArray());
$this->assertEquals($array, $fromFactory->toArray());
}
}

0 comments on commit bfcedac

Please sign in to comment.