Skip to content

Commit a1bb2ef

Browse files
authored
Merge pull request #11 from danitome24/1-email-vo
#1 - added email VO
2 parents 5dcdb20 + 675992d commit a1bb2ef

File tree

3 files changed

+151
-0
lines changed

3 files changed

+151
-0
lines changed

src/PhpValueObject/User/Email.php

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <danieltomefer@gmail.com>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace PhpValueObject\User;
9+
10+
use PhpValueObject\User\Exception\InvalidEmailException;
11+
use PhpValueObject\ValueObject;
12+
13+
final class Email implements ValueObject
14+
{
15+
/**
16+
* @var string
17+
*/
18+
private $email;
19+
20+
/**
21+
* Email constructor.
22+
* @param string $email
23+
* @throws \PhpValueObject\User\Exception\InvalidEmailException
24+
*/
25+
private function __construct(string $email)
26+
{
27+
$this->changeEmail($email);
28+
}
29+
30+
/**
31+
* @param string $email
32+
* @return static
33+
* @throws \PhpValueObject\User\Exception\InvalidEmailException
34+
*/
35+
public static function build(string $email)
36+
{
37+
return new static($email);
38+
}
39+
40+
/**
41+
* @return string
42+
*/
43+
public function email(): string
44+
{
45+
return $this->email;
46+
}
47+
48+
/**
49+
* Compare a value object with another one.
50+
*
51+
* @param static|ValueObject $valueObjectToCompare
52+
* @return bool
53+
*/
54+
public function equals(ValueObject $valueObjectToCompare): bool
55+
{
56+
return ($this->email() === $valueObjectToCompare->email());
57+
}
58+
59+
/**
60+
* Change email
61+
*
62+
* @param string $email
63+
* @throws InvalidEmailException
64+
*/
65+
private function changeEmail(string $email)
66+
{
67+
$this->checkIsValidEmail($email);
68+
$this->email = $email;
69+
}
70+
71+
/**
72+
* Check if email is valid
73+
*
74+
* @param string $email
75+
* @throws InvalidEmailException
76+
*/
77+
private function checkIsValidEmail(string $email): void
78+
{
79+
if (!filter_var($email, FILTER_VALIDATE_EMAIL)) {
80+
throw new InvalidEmailException('Email ' . $email . ' is not valid');
81+
}
82+
}
83+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <danieltomefer@gmail.com>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace PhpValueObject\User\Exception;
9+
10+
use Throwable;
11+
12+
class InvalidEmailException extends \Exception
13+
{
14+
public function __construct($message = "", $code = 0, Throwable $previous = null)
15+
{
16+
parent::__construct($message, $code, $previous);
17+
}
18+
}

tests/User/EmailTest.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
/**
3+
* This software was built by:
4+
* Daniel Tomé Fernández <danieltomefer@gmail.com>
5+
* GitHub: danitome24
6+
*/
7+
8+
namespace Test\User;
9+
10+
use PHPUnit\Framework\TestCase;
11+
use PhpValueObject\User\Email;
12+
use PhpValueObject\User\Exception\InvalidEmailException;
13+
14+
class EmailTest extends TestCase
15+
{
16+
public function testEmailIsNotValidException()
17+
{
18+
$this->expectException(InvalidEmailException::class);
19+
Email::build('aninvalidEmail.com');
20+
}
21+
22+
public function testEmailIsValidInstance()
23+
{
24+
$emailText = 'thebestproject@email.com';
25+
$email = Email::build($emailText);
26+
$this->assertEquals($emailText, $email->email());
27+
}
28+
29+
/**
30+
* @dataProvider emails
31+
* @param Email $email
32+
* @param Email $emailToCompare
33+
* @param bool $isEquals
34+
*/
35+
public function testEmailIsEquals(Email $email, Email $emailToCompare, bool $isEquals)
36+
{
37+
$this->assertEquals($email->equals($emailToCompare), $isEquals);
38+
}
39+
40+
/**
41+
* @return array
42+
*/
43+
public function emails(): array
44+
{
45+
return [
46+
[Email::build('thebestproject@email.com'), Email::build('thebestproject@email.com'), true],
47+
[Email::build('thebestproject@email.com'), Email::build('thebestproject2@email.com'), false]
48+
];
49+
}
50+
}

0 commit comments

Comments
 (0)