FullName::validate()   A
last analyzed

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 Illuminate\Support\Stringable;
17
use Illuminate\Validation\ValidationException;
18
use MichaelRubel\Formatters\Collection\FullNameFormatter;
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|Stringable $value, int $limit = -1)
29
 * @method static static from(string|Stringable $value, int $limit = -1)
30
 * @method static static makeOrNull(string|Stringable|null $value, int $limit = -1)
31
 *
32
 * @extends Name<TKey, TValue>
33
 */
34
class FullName extends Name
35
{
36
    /**
37
     * @var Collection<int, string>
38
     */
39
    protected Collection $split;
40
41
    /**
42
     * Create a new instance of the value object.
43
     *
44
     * @param  string|Stringable  $value
45
     * @param  int  $limit
46
     */
47 26
    public function __construct(string|Stringable $value, protected int $limit = -1)
48
    {
49 26
        static::beforeParentCalls(fn () => $this->split());
50
51 26
        parent::__construct($value);
52
    }
53
54
    /**
55
     * Get the full name.
56
     *
57
     * @return string
58
     */
59 11
    public function fullName(): string
60
    {
61 11
        return $this->value();
62
    }
63
64
    /**
65
     * Get the first name.
66
     *
67
     * @return string
68
     */
69 9
    public function firstName(): string
70
    {
71 9
        return $this->split->first();
72
    }
73
74
    /**
75
     * Get the last name.
76
     *
77
     * @return string
78
     */
79 8
    public function lastName(): string
80
    {
81 8
        return $this->split->last();
82
    }
83
84
    /**
85
     * Get an array representation of the value object.
86
     *
87
     * @return array<string, string|null>
88
     */
89 2
    public function toArray(): array
90
    {
91 2
        return [
92 2
            'fullName'  => $this->fullName(),
93 2
            'firstName' => $this->firstName(),
94 2
            'lastName'  => $this->lastName(),
95 2
        ];
96
    }
97
98
    /**
99
     * Validate the value object data.
100
     *
101
     * @return void
102
     */
103 26
    protected function validate(): void
104
    {
105 26
        if ($this->value() === '') {
106 2
            throw ValidationException::withMessages(['Full name cannot be empty.']);
107
        }
108
109 25
        if (count($this->split) < 2) {
110 2
            throw ValidationException::withMessages(['Full name should have a first name and last name.']);
111
        }
112
    }
113
114
    /**
115
     * Sanitize the value.
116
     *
117
     * @return void
118
     */
119 23
    protected function sanitize(): void
120
    {
121 23
        $this->value = format(FullNameFormatter::class, $this->value());
122
    }
123
124
    /**
125
     * Split the value.
126
     *
127
     * @return void
128
     */
129 27
    protected function split(): void
130
    {
131 27
        $this->split = str($this->value())->split('/\s/', $this->limit);
132
    }
133
}
134