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) |
29
|
|
|
* @method static static from(string|Stringable $value) |
30
|
|
|
* @method static static makeOrNull(string|Stringable|null $value) |
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
|
|
|
*/ |
46
|
16 |
|
public function __construct(string|Stringable $value) |
47
|
|
|
{ |
48
|
16 |
|
static::beforeParentCalls(fn () => $this->split()); |
49
|
|
|
|
50
|
16 |
|
parent::__construct($value); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* Get the full name. |
55
|
|
|
* |
56
|
|
|
* @return string |
57
|
|
|
*/ |
58
|
3 |
|
public function fullName(): string |
59
|
|
|
{ |
60
|
3 |
|
return $this->value(); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Get the first name. |
65
|
|
|
* |
66
|
|
|
* @return string |
67
|
|
|
*/ |
68
|
6 |
|
public function firstName(): string |
69
|
|
|
{ |
70
|
6 |
|
return (string) $this->split->first(); |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Get the last name. |
75
|
|
|
* |
76
|
|
|
* @return string |
77
|
|
|
*/ |
78
|
5 |
|
public function lastName(): string |
79
|
|
|
{ |
80
|
5 |
|
return (string) $this->split->last(); |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Get an array representation of the value object. |
85
|
|
|
* |
86
|
|
|
* @return array<string, string|null> |
87
|
|
|
*/ |
88
|
1 |
|
public function toArray(): array |
89
|
|
|
{ |
90
|
|
|
return [ |
91
|
1 |
|
'fullName' => $this->fullName(), |
92
|
1 |
|
'firstName' => $this->firstName(), |
93
|
1 |
|
'lastName' => $this->lastName(), |
94
|
|
|
]; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Validate the value object data. |
99
|
|
|
* |
100
|
|
|
* @return void |
101
|
|
|
*/ |
102
|
16 |
|
protected function validate(): void |
103
|
|
|
{ |
104
|
16 |
|
if (empty($this->value())) { |
105
|
1 |
|
throw ValidationException::withMessages([__('Full name cannot be empty.')]); |
106
|
|
|
} |
107
|
|
|
|
108
|
15 |
|
if (count($this->split) < 2) { |
109
|
1 |
|
throw ValidationException::withMessages([__('Full name should have a first name and last name.')]); |
110
|
|
|
} |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
/** |
114
|
|
|
* Sanitize the value. |
115
|
|
|
* |
116
|
|
|
* @return void |
117
|
|
|
*/ |
118
|
14 |
|
protected function sanitize(): void |
119
|
|
|
{ |
120
|
14 |
|
$this->value = format(FullNameFormatter::class, $this->value()); |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Split the value. |
125
|
|
|
* |
126
|
|
|
* @return void |
127
|
|
|
*/ |
128
|
16 |
|
protected function split(): void |
129
|
|
|
{ |
130
|
16 |
|
$this->split = str($this->value())->split('/\s/'); |
131
|
|
|
} |
132
|
|
|
} |
133
|
|
|
|