Passed
Push — main ( 7f2400...dee5f3 )
by Michael
59s queued 12s
created

Email::toArray()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 0
Metric Value
eloc 4
c 0
b 0
f 0
dl 0
loc 6
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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\Stringable;
15
use Illuminate\Validation\Concerns\ValidatesAttributes;
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
    use ValidatesAttributes;
36
37
    /**
38
     * @var Collection<int, string>
39
     */
40
    protected Collection $split;
41
42
    /**
43
     * Create a new instance of the value object.
44
     *
45
     * @param  string|Stringable  $value
46
     */
47 15
    public function __construct(string|Stringable $value)
48
    {
49 15
        parent::__construct($value);
50
51 11
        $this->validate();
52 11
        $this->split();
53
    }
54
55
    /**
56
     * @return string
57
     */
58 2
    public function username(): string
59
    {
60 2
        return (string) $this->split->first();
61
    }
62
63
    /**
64
     * @return string
65
     */
66 2
    public function domain(): string
67
    {
68 2
        return (string) $this->split->last();
69
    }
70
71
    /**
72
     * Get an array representation of the value object.
73
     *
74
     * @return array
75
     */
76 1
    public function toArray(): array
77
    {
78
        return [
79 1
            'email'    => $this->value(),
80 1
            'username' => $this->username(),
81 1
            'domain'   => $this->domain(),
82
        ];
83
    }
84
85
    /**
86
     * Validate the email.
87
     *
88
     * @return void
89
     */
90 15
    protected function validate(): void
91
    {
92 15
        $toValidate = ['email', $this->value(), $this->validationParameters()];
93
94 15
        if (! $this->validateEmail(...$toValidate)) {
0 ignored issues
show
Bug introduced by
It seems like $toValidate can also be of type array<integer,string>; however, parameter $attribute of MichaelRubel\ValueObject...\Email::validateEmail() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

94
        if (! $this->validateEmail(/** @scrutinizer ignore-type */ ...$toValidate)) {
Loading history...
95 4
            throw ValidationException::withMessages([__('Your email is invalid.')]);
96
        }
97
    }
98
99
    /**
100
     * Define how you want to validate the email.
101
     *
102
     * @return array
103
     */
104 15
    protected function validationParameters(): array
105
    {
106 15
        return ['filter', 'spoof'];
107
    }
108
109
    /**
110
     * Split the value by at symbol.
111
     *
112
     * @return void
113
     */
114 11
    protected function split(): void
115
    {
116 11
        $this->split = str($this->value())->split('/@/');
117
    }
118
}
119