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

FullName::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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|null $value)
29
 * @method static static from(string|null $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 13
    public function __construct(protected string $value)
46
    {
47 13
        $this->validate();
48
49 12
        $this->value = $this->format();
50 12
        $this->split = $this->split();
51
    }
52
53
    /**
54
     * Get the full name.
55
     *
56
     * @return string
57
     */
58 13
    public function fullName(): string
59
    {
60 13
        return $this->value;
61
    }
62
63
    /**
64
     * Get the first name.
65
     *
66
     * @return string|null
67
     */
68 6
    public function firstName(): ?string
69
    {
70 6
        return $this->split->first();
71
    }
72
73
    /**
74
     * Get the last name.
75
     *
76
     * @return string|null
77
     */
78 5
    public function lastName(): ?string
79
    {
80 5
        return $this->split->last();
81
    }
82
83
    /**
84
     * Get the object value.
85
     *
86
     * @return string
87
     */
88 13
    public function value(): string
89
    {
90 13
        return $this->fullName();
91
    }
92
93
    /**
94
     * Get an array representation of the value object.
95
     *
96
     * @return array<string, string|null>
97
     */
98 1
    public function toArray(): array
99
    {
100
        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...
101 1
            'fullName'  => $this->fullName(),
102 1
            'firstName' => $this->firstName(),
103 1
            'lastName'  => $this->lastName(),
104
        ];
105
    }
106
107
    /**
108
     * Validate the value object data.
109
     *
110
     * @return void
111
     */
112 13
    protected function validate(): void
113
    {
114 13
        if (empty($this->value())) {
115 1
            throw new InvalidArgumentException('Full name cannot be empty.');
116
        }
117
    }
118
119
    /**
120
     * Format the value.
121
     *
122
     * @return string
123
     */
124 12
    protected function format(): string
125
    {
126 12
        return format(FullNameFormatter::class, $this->value());
127
    }
128
129
    /**
130
     * Split the value.
131
     *
132
     * @return Collection<int, string>
133
     */
134 12
    protected function split(): Collection
135
    {
136 12
        return str($this->value())->split('/\s/');
137
    }
138
}
139