diff --git a/src/Pages/Properties/Factory.php b/src/Pages/Properties/Factory.php index c628db1e..53f85f38 100644 --- a/src/Pages/Properties/Factory.php +++ b/src/Pages/Properties/Factory.php @@ -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}'"), }; } diff --git a/src/Pages/Properties/PhoneNumber.php b/src/Pages/Properties/PhoneNumber.php new file mode 100644 index 00000000..afef2e1e --- /dev/null +++ b/src/Pages/Properties/PhoneNumber.php @@ -0,0 +1,67 @@ +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); + } +} diff --git a/src/Pages/Properties/Property.php b/src/Pages/Properties/Property.php index 91bef4a6..54d1c6f5 100644 --- a/src/Pages/Properties/Property.php +++ b/src/Pages/Properties/Property.php @@ -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 // { diff --git a/tests/Unit/Pages/Properties/PhoneNumberTest.php b/tests/Unit/Pages/Properties/PhoneNumberTest.php new file mode 100644 index 00000000..393d40d2 --- /dev/null +++ b/tests/Unit/Pages/Properties/PhoneNumberTest.php @@ -0,0 +1,40 @@ +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()); + } +}