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\Collection; |
14
|
|
|
use Illuminate\Support\Facades\Validator; |
15
|
|
|
use Illuminate\Support\Stringable; |
16
|
|
|
use Illuminate\Validation\ValidationException; |
17
|
|
|
use MichaelRubel\ValueObjects\Collection\Primitive\Text; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* "Email" object that represents the email address. |
21
|
|
|
* |
22
|
|
|
* @author Michael Rubél <[email protected]> |
23
|
|
|
* |
24
|
|
|
* @template TKey of array-key |
25
|
|
|
* @template TValue |
26
|
|
|
* |
27
|
|
|
* @method static static make(string|Stringable $value) |
28
|
|
|
* @method static static from(string|Stringable $value) |
29
|
|
|
* @method static static makeOrNull(string|Stringable|null $value) |
30
|
|
|
* |
31
|
|
|
* @extends Text<TKey, TValue> |
32
|
|
|
*/ |
33
|
|
|
class Email extends Text |
34
|
|
|
{ |
35
|
|
|
/** |
36
|
|
|
* @var Collection<int, string> |
37
|
|
|
*/ |
38
|
|
|
protected Collection $split; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Create a new instance of the value object. |
42
|
|
|
* |
43
|
|
|
* @param string|Stringable $value |
44
|
|
|
*/ |
45
|
15 |
|
public function __construct(string|Stringable $value) |
46
|
|
|
{ |
47
|
15 |
|
parent::__construct($value); |
48
|
|
|
|
49
|
11 |
|
$this->validate(); |
50
|
11 |
|
$this->split(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @return string |
55
|
|
|
*/ |
56
|
2 |
|
public function username(): string |
57
|
|
|
{ |
58
|
2 |
|
return (string) $this->split->first(); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
2 |
|
public function domain(): string |
65
|
|
|
{ |
66
|
2 |
|
return (string) $this->split->last(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Get an array representation of the value object. |
71
|
|
|
* |
72
|
|
|
* @return array |
73
|
|
|
*/ |
74
|
1 |
|
public function toArray(): array |
75
|
|
|
{ |
76
|
|
|
return [ |
77
|
1 |
|
'email' => $this->value(), |
78
|
1 |
|
'username' => $this->username(), |
79
|
1 |
|
'domain' => $this->domain(), |
80
|
|
|
]; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Validate the email. |
85
|
|
|
* |
86
|
|
|
* @return void |
87
|
|
|
*/ |
88
|
15 |
|
protected function validate(): void |
89
|
|
|
{ |
90
|
15 |
|
$validator = Validator::make( |
91
|
15 |
|
['email' => $this->value()], |
92
|
15 |
|
['email' => $this->validationRules()], |
93
|
|
|
); |
94
|
|
|
|
95
|
15 |
|
if ($validator->fails()) { |
96
|
4 |
|
throw ValidationException::withMessages([__('Your email is invalid.')]); |
97
|
|
|
} |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Define the rules for email validator. |
102
|
|
|
* |
103
|
|
|
* @return array |
104
|
|
|
*/ |
105
|
15 |
|
protected function validationRules(): array |
106
|
|
|
{ |
107
|
15 |
|
return ['required', 'email:' . implode(',', $this->validationParameters())]; |
|
|
|
|
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
/** |
111
|
|
|
* Define how you want to validate the email. |
112
|
|
|
* |
113
|
|
|
* @deprecated |
114
|
|
|
* |
115
|
|
|
* @return array |
116
|
|
|
*/ |
117
|
15 |
|
protected function validationParameters(): array |
118
|
|
|
{ |
119
|
15 |
|
return ['filter', 'spoof']; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Split the value by at symbol. |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
11 |
|
protected function split(): void |
128
|
|
|
{ |
129
|
11 |
|
$this->split = str($this->value())->split('/@/'); |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|