Skip to content

#1 - added email VO #11

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 83 additions & 0 deletions src/PhpValueObject/User/Email.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace PhpValueObject\User;

use PhpValueObject\User\Exception\InvalidEmailException;
use PhpValueObject\ValueObject;

final class Email implements ValueObject
{
/**
* @var string
*/
private $email;

/**
* Email constructor.
* @param string $email
* @throws \PhpValueObject\User\Exception\InvalidEmailException
*/
private function __construct(string $email)
{
$this->changeEmail($email);
}

/**
* @param string $email
* @return static
* @throws \PhpValueObject\User\Exception\InvalidEmailException
*/
public static function build(string $email)
{
return new static($email);
}

/**
* @return string
*/
public function email(): string
{
return $this->email;
}

/**
* Compare a value object with another one.
*
* @param static|ValueObject $valueObjectToCompare
* @return bool
*/
public function equals(ValueObject $valueObjectToCompare): bool
{
return ($this->email() === $valueObjectToCompare->email());
}

/**
* Change email
*
* @param string $email
* @throws InvalidEmailException
*/
private function changeEmail(string $email)
{
$this->checkIsValidEmail($email);
$this->email = $email;
}

/**
* Check if email is valid
*
* @param string $email
* @throws InvalidEmailException
*/
private function checkIsValidEmail(string $email): void
{
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
throw new InvalidEmailException('Email ' . $email . ' is not valid');
}
}
}
18 changes: 18 additions & 0 deletions src/PhpValueObject/User/Exception/InvalidEmailException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace PhpValueObject\User\Exception;

use Throwable;

class InvalidEmailException extends \Exception
{
public function __construct($message = "", $code = 0, Throwable $previous = null)
{
parent::__construct($message, $code, $previous);
}
}
50 changes: 50 additions & 0 deletions tests/User/EmailTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php
/**
* This software was built by:
* Daniel Tomé Fernández <danieltomefer@gmail.com>
* GitHub: danitome24
*/

namespace Test\User;

use PHPUnit\Framework\TestCase;
use PhpValueObject\User\Email;
use PhpValueObject\User\Exception\InvalidEmailException;

class EmailTest extends TestCase
{
public function testEmailIsNotValidException()
{
$this->expectException(InvalidEmailException::class);
Email::build('aninvalidEmail.com');
}

public function testEmailIsValidInstance()
{
$emailText = 'thebestproject@email.com';
$email = Email::build($emailText);
$this->assertEquals($emailText, $email->email());
}

/**
* @dataProvider emails
* @param Email $email
* @param Email $emailToCompare
* @param bool $isEquals
*/
public function testEmailIsEquals(Email $email, Email $emailToCompare, bool $isEquals)
{
$this->assertEquals($email->equals($emailToCompare), $isEquals);
}

/**
* @return array
*/
public function emails(): array
{
return [
[Email::build('thebestproject@email.com'), Email::build('thebestproject@email.com'), true],
[Email::build('thebestproject@email.com'), Email::build('thebestproject2@email.com'), false]
];
}
}