Passed
Push — main ( c994c9...3e431a )
by Michael
03:19
created

Email::validationParameters()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 3
ccs 2
cts 2
cp 1
crap 1
rs 10
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 16
    public function __construct(string|Stringable $value)
46
    {
47 16
        parent::__construct($value);
48
49 11
        $this->split();
50
    }
51
52
    /**
53
     * @return string
54
     */
55 2
    public function username(): string
56
    {
57 2
        return $this->split->first();
58
    }
59
60
    /**
61
     * @return string
62
     */
63 2
    public function domain(): string
64
    {
65 2
        return $this->split->last();
66
    }
67
68
    /**
69
     * Get an array representation of the value object.
70
     *
71
     * @return array
72
     */
73 1
    public function toArray(): array
74
    {
75
        return [
76 1
            'email'    => $this->value(),
77 1
            'username' => $this->username(),
78 1
            'domain'   => $this->domain(),
79
        ];
80
    }
81
82
    /**
83
     * Validate the email.
84
     *
85
     * @return void
86
     */
87 17
    protected function validate(): void
88
    {
89 17
        $validator = Validator::make(
90 17
            ['email' => $this->value()],
91 17
            ['email' => $this->validationRules()],
92
        );
93
94 17
        if ($validator->fails()) {
95 5
            throw ValidationException::withMessages([__('Your email is invalid.')]);
96
        }
97
    }
98
99
    /**
100
     * Define the rules for email validator.
101
     *
102
     * @return array
103
     */
104 17
    protected function validationRules(): array
105
    {
106 17
        return ['required', 'email:filter,spoof'];
107
    }
108
109
    /**
110
     * Split the value by at symbol.
111
     *
112
     * @return void
113
     */
114 12
    protected function split(): void
115
    {
116 12
        $this->split = str($this->value())->split('/@/');
117
    }
118
}
119