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
0ec3b39
commit bfcedac
Showing
4 changed files
with
112 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
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,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); | ||
} | ||
} |
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,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()); | ||
} | ||
} |