Total Complexity | 5 |
Total Lines | 46 |
Duplicated Lines | 0 % |
Changes | 1 | ||
Bugs | 0 | Features | 0 |
1 | <?php declare(strict_types=1); |
||
10 | class Email implements Type |
||
11 | { |
||
12 | /** |
||
13 | * @var string $email |
||
14 | */ |
||
15 | private $email; |
||
16 | |||
17 | /** |
||
18 | * Create an email object if data is valid. |
||
19 | * @param string $email |
||
20 | * @param Validator $validator |
||
21 | * @throws InvalidValue |
||
22 | */ |
||
23 | public function __construct(string $email, Validator $validator = null) |
||
24 | { |
||
25 | if ($validator == null) { |
||
26 | // use the default validator |
||
27 | $validator = new EmailValidator(); |
||
28 | } |
||
29 | $result = $validator->validate($email); |
||
30 | |||
31 | if (!$result->isValid()) { |
||
32 | /** |
||
33 | * @var Failure $result |
||
34 | */ |
||
35 | throw new InvalidValue($result->getFirstError()->getMessage(), $result->getErrors()); |
||
36 | } |
||
37 | $this->email = $email; |
||
38 | } |
||
39 | |||
40 | /** |
||
41 | * Get string representation of the email object. |
||
42 | * @return string |
||
43 | */ |
||
44 | public function __toString(): string |
||
45 | { |
||
46 | return $this->email; |
||
47 | } |
||
48 | |||
49 | /** |
||
50 | * Get real value of the email object. |
||
51 | * @return string |
||
52 | */ |
||
53 | public function getValue() : string |
||
56 | } |
||
57 | } |