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