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