Passed
Pull Request — main (#2)
by Michael
03:29
created

FullName::validate()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 3

Importance

Changes 0
Metric Value
cc 3
eloc 4
c 0
b 0
f 0
nc 3
nop 0
dl 0
loc 8
ccs 5
cts 5
cp 1
crap 3
rs 10
1
<?php
2
3
declare(strict_types=1);
4
5
/**
6
 * This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects)
7
 *
8
 * @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository
9
 * @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/)
10
 * @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT
11
 */
12
13
namespace MichaelRubel\ValueObjects\Collection\Complex;
14
15
use Illuminate\Support\Collection;
16
use InvalidArgumentException;
17
use MichaelRubel\Formatters\Collection\FullNameFormatter;
18
use MichaelRubel\ValueObjects\ValueObject;
19
20
/**
21
 * "FullName" object presenting a full name.
22
 *
23
 * @author Michael Rubél <[email protected]>
24
 *
25
 * @template TKey of array-key
26
 * @template TValue
27
 *
28
 * @method static static make(string $value)
29
 * @method static static from(string $value)
30
 *
31
 * @extends ValueObject<TKey, TValue>
32
 */
33
class FullName extends ValueObject
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  $value
44
     */
45 14
    public function __construct(protected string $value)
46
    {
47 14
        $this->split();
48 14
        $this->format();
49 14
        $this->validate();
50
    }
51
52
    /**
53
     * Get the full name.
54
     *
55
     * @return string
56
     */
57 14
    public function fullName(): string
58
    {
59 14
        return $this->value;
60
    }
61
62
    /**
63
     * Get the first name.
64
     *
65
     * @return string|null
66
     */
67 6
    public function firstName(): ?string
68
    {
69 6
        return $this->split->first();
70
    }
71
72
    /**
73
     * Get the last name.
74
     *
75
     * @return string|null
76
     */
77 5
    public function lastName(): ?string
78
    {
79 5
        return $this->split->last();
80
    }
81
82
    /**
83
     * Get the object value.
84
     *
85
     * @return string
86
     */
87 14
    public function value(): string
88
    {
89 14
        return $this->fullName();
90
    }
91
92
    /**
93
     * Get an array representation of the value object.
94
     *
95
     * @return array<string, string|null>
96
     */
97 1
    public function toArray(): array
98
    {
99
        return [
0 ignored issues
show
introduced by
The expression return array('fullName' ...' => $this->lastName()) returns an array which contains values of type string which are incompatible with the return type Illuminate\Contracts\Support\TValue mandated by Illuminate\Contracts\Support\Arrayable::toArray().
Loading history...
100 1
            'fullName'  => $this->fullName(),
101 1
            'firstName' => $this->firstName(),
102 1
            'lastName'  => $this->lastName(),
103
        ];
104
    }
105
106
    /**
107
     * Validate the value object data.
108
     *
109
     * @return void
110
     */
111 14
    protected function validate(): void
112
    {
113 14
        if (empty($this->value())) {
114 1
            throw new InvalidArgumentException('Full name cannot be empty.');
115
        }
116
117 13
        if (count($this->split) < 2) {
118 2
            throw new InvalidArgumentException('Full name should have a first name and last name.');
119
        }
120
    }
121
122
    /**
123
     * Format the value.
124
     *
125
     * @return void
126
     */
127 14
    protected function format(): void
128
    {
129 14
        $this->value = format(FullNameFormatter::class, $this->value());
130
    }
131
132
    /**
133
     * Split the value.
134
     *
135
     * @return void
136
     */
137 14
    protected function split(): void
138
    {
139 14
        $this->split = str($this->value())->split('/\s/');
140
    }
141
}
142