Passed
Pull Request — main (#2)
by Michael
03:17
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 MichaelRubel\Formatters\Collection\FullNameFormatter;
17
use MichaelRubel\ValueObjects\ValueObject;
18
19
/**
20
 * "FullName" object presenting a full name.
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|null $value)
28
 * @method static static from(string|null $value)
29
 *
30
 * @extends ValueObject<TKey, TValue>
31
 */
32
class FullName extends ValueObject
33
{
34
    /**
35
     * @var Collection<int, string>
36
     */
37
    protected Collection $split;
38
39
    /**
40
     * Create a new instance of the value object.
41
     *
42
     * @param  string  $value
43
     */
44 13
    public function __construct(protected string $value)
45
    {
46 13
        $this->validate();
47
48 12
        $this->value = $this->format();
49 12
        $this->split = $this->split();
50
    }
51
52
    /**
53
     * Get the full name.
54
     *
55
     * @return string
56
     */
57 12
    public function fullName(): string
58
    {
59 12
        return (string) $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 12
    public function value(): string
88
    {
89 12
        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 13
    protected function validate(): void
112
    {
113 13
        if (blank($this->value)) {
114 1
            throw new \InvalidArgumentException('Full name cannot be blank.');
115
        }
116
    }
117
118
    /**
119
     * Format the value.
120
     *
121
     * @return string
122
     */
123 12
    protected function format(): string
124
    {
125 12
        return format(FullNameFormatter::class, $this->value());
126
    }
127
128
    /**
129
     * Split the value.
130
     *
131
     * @return Collection<int, string>
132
     */
133 12
    protected function split(): Collection
134
    {
135 12
        return str($this->value())->split('/\s/');
136
    }
137
}
138