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\Facades\Validator; |
14
|
|
|
use Illuminate\Support\Stringable; |
15
|
|
|
use Illuminate\Validation\ValidationException; |
16
|
|
|
use MichaelRubel\ValueObjects\Collection\Primitive\Text; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* "Email" object that represents the email address. |
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
|
|
|
/** |
35
|
|
|
* @var array<int, string> |
36
|
|
|
*/ |
37
|
|
|
protected array $split; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Create a new instance of the value object. |
41
|
|
|
* |
42
|
|
|
* @param string|Stringable $value |
43
|
|
|
*/ |
44
|
16 |
|
public function __construct(string|Stringable $value) |
45
|
|
|
{ |
46
|
16 |
|
parent::__construct($value); |
47
|
|
|
|
48
|
11 |
|
$this->split(); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
2 |
|
public function username(): string |
55
|
|
|
{ |
56
|
2 |
|
return $this->split[0]; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
*/ |
62
|
2 |
|
public function domain(): string |
63
|
|
|
{ |
64
|
2 |
|
return $this->split[1]; |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
/** |
68
|
|
|
* Get an array representation of the value object. |
69
|
|
|
* |
70
|
|
|
* @return array |
71
|
|
|
*/ |
72
|
1 |
|
public function toArray(): array |
73
|
|
|
{ |
74
|
1 |
|
return [ |
75
|
1 |
|
'email' => $this->value(), |
76
|
1 |
|
'username' => $this->username(), |
77
|
1 |
|
'domain' => $this->domain(), |
78
|
1 |
|
]; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Validate the email. |
83
|
|
|
* |
84
|
|
|
* @return void |
85
|
|
|
*/ |
86
|
17 |
|
protected function validate(): void |
87
|
|
|
{ |
88
|
17 |
|
$validator = Validator::make( |
89
|
17 |
|
['email' => $this->value()], |
90
|
17 |
|
['email' => $this->validationRules()], |
91
|
17 |
|
); |
92
|
|
|
|
93
|
17 |
|
if ($validator->fails()) { |
94
|
5 |
|
throw ValidationException::withMessages(['Your email is invalid.']); |
95
|
|
|
} |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
/** |
99
|
|
|
* Define the rules for email validator. |
100
|
|
|
* |
101
|
|
|
* @return array |
102
|
|
|
*/ |
103
|
17 |
|
protected function validationRules(): array |
104
|
|
|
{ |
105
|
17 |
|
return ['required', 'email:filter,spoof']; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Split the value by at symbol. |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
12 |
|
protected function split(): void |
114
|
|
|
{ |
115
|
12 |
|
$this->split = explode('@', $this->value()); |
116
|
|
|
} |
117
|
|
|
} |
118
|
|
|
|