Skip to content

Commit

Permalink
Add email page property
Browse files Browse the repository at this point in the history
  • Loading branch information
mariosimao committed Oct 31, 2021
1 parent ab3c86d commit 0ec3b39
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 4 deletions.
67 changes: 67 additions & 0 deletions src/Pages/Properties/Email.php
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);
}
}
1 change: 1 addition & 0 deletions src/Pages/Properties/Factory.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public static function fromArray(array $array): PropertyInterface
Property::TYPE_PEOPLE => People::fromArray($array),
Property::TYPE_CHECKBOX => Checkbox::fromArray($array),
Property::TYPE_URL => Url::fromArray($array),
Property::TYPE_EMAIL => Email::fromArray($array),
default => throw new Exception("Invalid property type: '{$type}'"),
};
}
Expand Down
8 changes: 4 additions & 4 deletions src/Pages/Properties/Property.php
Original file line number Diff line number Diff line change
Expand Up @@ -136,10 +136,10 @@ public function isUrl(): bool
return $this->type === self::TYPE_URL;
}

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

// public function isPhoneNumber(): bool
// {
Expand Down
40 changes: 40 additions & 0 deletions tests/Unit/Pages/Properties/EmailTest.php
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());
}
}

0 comments on commit 0ec3b39

Please sign in to comment.