-
-
Notifications
You must be signed in to change notification settings - Fork 23
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
ab3c86d
commit 0ec3b39
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
<?php | ||
|
||
namespace Notion\Pages\Properties; | ||
|
||
/** | ||
* @psalm-type EmailJson = array{ | ||
* id: string, | ||
* type: "email", | ||
* email: string, | ||
* } | ||
*/ | ||
class Email implements PropertyInterface | ||
{ | ||
private const TYPE = Property::TYPE_EMAIL; | ||
|
||
private Property $property; | ||
|
||
private string $email; | ||
|
||
private function __construct(Property $property, string $email) | ||
{ | ||
$this->property = $property; | ||
$this->email = $email; | ||
} | ||
|
||
public static function create(string $email): self | ||
{ | ||
$property = Property::create("", self::TYPE); | ||
|
||
return new self($property, $email); | ||
} | ||
|
||
public static function fromArray(array $array): self | ||
{ | ||
/** @psalm-var EmailJson $array */ | ||
|
||
$property = Property::fromArray($array); | ||
|
||
$email = $array[self::TYPE]; | ||
|
||
return new self($property, $email); | ||
} | ||
|
||
public function toArray(): array | ||
{ | ||
$array = $this->property->toArray(); | ||
|
||
$array[self::TYPE] = $this->email; | ||
|
||
return $array; | ||
} | ||
|
||
public function property(): Property | ||
{ | ||
return $this->property; | ||
} | ||
|
||
public function email(): string | ||
{ | ||
return $this->email; | ||
} | ||
|
||
public function withEmail(string $email): self | ||
{ | ||
return new self($this->property, $email); | ||
} | ||
} |
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
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\Email; | ||
use Notion\Pages\Properties\Factory; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
class EmailTest extends TestCase | ||
{ | ||
public function test_create(): void | ||
{ | ||
$email = Email::create("mario@domain.com"); | ||
|
||
$this->assertTrue($email->property()->isEmail()); | ||
$this->assertEquals("mario@domain.com", $email->email()); | ||
} | ||
|
||
public function test_change_email(): void | ||
{ | ||
$email = Email::create("mario@domain.com")->withEmail("luigi@domain.com"); | ||
|
||
$this->assertEquals("luigi@domain.com", $email->email()); | ||
} | ||
|
||
public function test_array_conversion(): void | ||
{ | ||
$array = [ | ||
"id" => "abc", | ||
"type" => "email", | ||
"email" => "mario@domain.com", | ||
]; | ||
|
||
$email = Email::fromArray($array); | ||
$fromFactory = Factory::fromArray($array); | ||
|
||
$this->assertEquals($array, $email->toArray()); | ||
$this->assertEquals($array, $fromFactory->toArray()); | ||
} | ||
} |