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 [ |
|
|
|
|
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
|
|
|
|