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 InvalidArgumentException; |
18
|
|
|
use MichaelRubel\Formatters\Collection\FullNameFormatter; |
19
|
|
|
use MichaelRubel\ValueObjects\ValueObject; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* "FullName" object presenting a full name. |
23
|
|
|
* |
24
|
|
|
* @author Michael Rubél <[email protected]> |
25
|
|
|
* |
26
|
|
|
* @template TKey of array-key |
27
|
|
|
* @template TValue |
28
|
|
|
* |
29
|
|
|
* @method static static make(string|Stringable $value) |
30
|
|
|
* @method static static from(string|Stringable $value) |
31
|
|
|
* @method static static makeOrNull(string|Stringable $value) |
32
|
|
|
* |
33
|
|
|
* @extends ValueObject<TKey, TValue> |
34
|
|
|
*/ |
35
|
|
|
class FullName extends ValueObject |
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(protected string|Stringable $value) |
48
|
|
|
{ |
49
|
15 |
|
$this->split(); |
50
|
15 |
|
$this->sanitize(); |
51
|
15 |
|
$this->validate(); |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* Get the full name. |
56
|
|
|
* |
57
|
|
|
* @return string |
58
|
|
|
*/ |
59
|
3 |
|
public function fullName(): string |
60
|
|
|
{ |
61
|
3 |
|
return $this->value(); |
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* Get the first name. |
66
|
|
|
* |
67
|
|
|
* @return string |
68
|
|
|
*/ |
69
|
6 |
|
public function firstName(): string |
70
|
|
|
{ |
71
|
6 |
|
return (string) $this->split->first(); |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get the last name. |
76
|
|
|
* |
77
|
|
|
* @return string |
78
|
|
|
*/ |
79
|
5 |
|
public function lastName(): string |
80
|
|
|
{ |
81
|
5 |
|
return (string) $this->split->last(); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* Get the object value. |
86
|
|
|
* |
87
|
|
|
* @return string |
88
|
|
|
*/ |
89
|
15 |
|
public function value(): string |
90
|
|
|
{ |
91
|
15 |
|
return (string) $this->value; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Get an array representation of the value object. |
96
|
|
|
* |
97
|
|
|
* @return array<string, string|null> |
98
|
|
|
*/ |
99
|
1 |
|
public function toArray(): array |
100
|
|
|
{ |
101
|
|
|
return [ |
102
|
1 |
|
'fullName' => $this->fullName(), |
103
|
1 |
|
'firstName' => $this->firstName(), |
104
|
1 |
|
'lastName' => $this->lastName(), |
105
|
|
|
]; |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Validate the value object data. |
110
|
|
|
* |
111
|
|
|
* @return void |
112
|
|
|
*/ |
113
|
15 |
|
protected function validate(): void |
114
|
|
|
{ |
115
|
15 |
|
if (empty($this->value())) { |
116
|
1 |
|
throw new InvalidArgumentException('Full name cannot be empty.'); |
117
|
|
|
} |
118
|
|
|
|
119
|
14 |
|
if (count($this->split) < 2) { |
120
|
2 |
|
throw new InvalidArgumentException('Full name should have a first name and last name.'); |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* Sanitize the value. |
126
|
|
|
* |
127
|
|
|
* @return void |
128
|
|
|
*/ |
129
|
15 |
|
protected function sanitize(): void |
130
|
|
|
{ |
131
|
15 |
|
$this->value = format(FullNameFormatter::class, $this->value()); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
/** |
135
|
|
|
* Split the value. |
136
|
|
|
* |
137
|
|
|
* @return void |
138
|
|
|
*/ |
139
|
15 |
|
protected function split(): void |
140
|
|
|
{ |
141
|
15 |
|
$this->split = str($this->value())->split('/\s/'); |
142
|
|
|
} |
143
|
|
|
} |
144
|
|
|
|