|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects) |
|
5
|
|
|
* |
|
6
|
|
|
* @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository |
|
7
|
|
|
* @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/) |
|
8
|
|
|
* @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace MichaelRubel\ValueObjects\Collection\Complex; |
|
12
|
|
|
|
|
13
|
|
|
use Illuminate\Support\Stringable; |
|
14
|
|
|
use Illuminate\Validation\Concerns\ValidatesAttributes; |
|
15
|
|
|
use Illuminate\Validation\ValidationException; |
|
16
|
|
|
use MichaelRubel\ValueObjects\Collection\Primitive\Text; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* "Email" object presenting a generic name. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Michael Rubél <[email protected]> |
|
22
|
|
|
* |
|
23
|
|
|
* @template TKey of array-key |
|
24
|
|
|
* @template TValue |
|
25
|
|
|
* |
|
26
|
|
|
* @method static static make(string|Stringable $value) |
|
27
|
|
|
* @method static static from(string|Stringable $value) |
|
28
|
|
|
* @method static static makeOrNull(string|Stringable|null $value) |
|
29
|
|
|
* |
|
30
|
|
|
* @extends Text<TKey, TValue> |
|
31
|
|
|
*/ |
|
32
|
|
|
class Email extends Text |
|
33
|
|
|
{ |
|
34
|
|
|
use ValidatesAttributes; |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* Create a new instance of the value object. |
|
38
|
|
|
* |
|
39
|
|
|
* @param string|Stringable $value |
|
40
|
|
|
*/ |
|
41
|
12 |
|
public function __construct(protected string|Stringable $value) |
|
42
|
|
|
{ |
|
43
|
12 |
|
parent::__construct($this->value); |
|
44
|
|
|
|
|
45
|
7 |
|
$this->validate(); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Validate the email. |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
12 |
|
protected function validate(): void |
|
54
|
|
|
{ |
|
55
|
12 |
|
$toValidate = ['email', $this->value(), $this->validationParameters()]; |
|
56
|
|
|
|
|
57
|
12 |
|
if (! $this->validateEmail(...$toValidate)) { |
|
|
|
|
|
|
58
|
5 |
|
throw ValidationException::withMessages(['Your email is invalid.']); |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
/** |
|
63
|
|
|
* Define how you want to validate the email. |
|
64
|
|
|
* |
|
65
|
|
|
* @return array |
|
66
|
|
|
*/ |
|
67
|
12 |
|
protected function validationParameters(): array |
|
68
|
|
|
{ |
|
69
|
12 |
|
return ['filter', 'spoof', 'dns']; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|