1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
namespace MichaelRubel\ValueObjects\Complex; |
6
|
|
|
|
7
|
|
|
use Illuminate\Contracts\Support\Arrayable; |
8
|
|
|
use Illuminate\Support\Collection; |
9
|
|
|
use Illuminate\Support\Traits\Conditionable; |
10
|
|
|
use Illuminate\Support\Traits\Macroable; |
11
|
|
|
use Illuminate\Support\Traits\Tappable; |
12
|
|
|
use MichaelRubel\Formatters\Collection\FullNameFormatter; |
13
|
|
|
use MichaelRubel\ValueObjects\ValueObject; |
14
|
|
|
|
15
|
|
|
class FullName implements ValueObject, Arrayable |
16
|
|
|
{ |
17
|
|
|
use Macroable, Conditionable, Tappable; |
|
|
|
|
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* @var Collection |
21
|
|
|
*/ |
22
|
|
|
protected Collection $split; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* Create a new value object instance. |
26
|
|
|
* |
27
|
|
|
* @param string|null $full_name |
28
|
|
|
*/ |
29
|
10 |
|
final public function __construct(public ?string $full_name) |
30
|
|
|
{ |
31
|
10 |
|
$this->full_name = format(FullNameFormatter::class, $this->full_name); |
32
|
|
|
|
33
|
10 |
|
$this->split = str($this->full_name)->split('/\s/'); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Return a new instance of TaxNumber. |
38
|
|
|
* |
39
|
|
|
* @param string|null $name |
40
|
|
|
* |
41
|
|
|
* @return static |
42
|
|
|
*/ |
43
|
5 |
|
public static function make(?string $name): static |
44
|
|
|
{ |
45
|
5 |
|
return new static($name); |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Get the first name. |
50
|
|
|
* |
51
|
|
|
* @return string |
52
|
|
|
*/ |
53
|
3 |
|
public function firstName(): string |
54
|
|
|
{ |
55
|
3 |
|
return $this->split->first(); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Get the last name. |
60
|
|
|
* |
61
|
|
|
* @return string |
62
|
|
|
*/ |
63
|
2 |
|
public function lastName(): string |
64
|
|
|
{ |
65
|
2 |
|
return $this->split->last(); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* Get the last name. |
70
|
|
|
* |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
6 |
|
public function fullName(): string |
74
|
|
|
{ |
75
|
6 |
|
return (string) $this->full_name; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
1 |
|
public function toArray(): array |
82
|
|
|
{ |
83
|
|
|
return [ |
|
|
|
|
84
|
1 |
|
'full_name' => $this->fullName(), |
85
|
1 |
|
'first_name' => $this->firstName(), |
86
|
1 |
|
'last_name' => $this->lastName(), |
87
|
|
|
]; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Return the first UUID if cast to string. |
92
|
|
|
* |
93
|
|
|
* @return string |
94
|
|
|
*/ |
95
|
1 |
|
public function __toString(): string |
96
|
|
|
{ |
97
|
1 |
|
return $this->fullName(); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|