Skip to content

Commit

Permalink
Add phone number database property
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Nov 1, 2021
1 parent fd46ec0 commit 8e725ff
Show file tree
Hide file tree
Showing 4 changed files with 89 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/Databases/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
51 changes: 51 additions & 0 deletions src/Databases/Properties/PhoneNumber.php
@@ -0,0 +1,51 @@
<?php

namespace Notion\Databases\Properties;

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

private Property $property;

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

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

return new self($property);
}

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

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

return new self($property);
}

public function toArray(): array
{
$array = $this->property->toArray();
$array[self::TYPE] = [];

return $array;
}
}
8 changes: 4 additions & 4 deletions src/Databases/Properties/Property.php
Expand Up @@ -150,10 +150,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
33 changes: 33 additions & 0 deletions tests/Unit/Databases/Properties/PhoneNumberTest.php
@@ -0,0 +1,33 @@
<?php

namespace Notion\Test\Unit\Databases\Properties;

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

class PhoneNumberTest extends TestCase
{
public function test_create(): void
{
$phoneNumber = PhoneNumber::create("Dummy prop name");

$this->assertEquals("Dummy prop name", $phoneNumber->property()->name());
$this->assertTrue($phoneNumber->property()->isPhoneNumber());
}

public function test_array_conversion(): void
{
$array = [
"id" => "abc",
"name" => "dummy",
"type" => "phone_number",
"phone_number" => [],
];
$phoneNumber = PhoneNumber::fromArray($array);
$fromFactory = Factory::fromArray($array);

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

0 comments on commit 8e725ff

Please sign in to comment.