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