generated from michael-rubel/laravel-package-template
-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathFullName.php
133 lines (118 loc) · 3.12 KB
/
FullName.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
<?php
declare(strict_types=1);
/**
* This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects)
*
* @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository
* @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/)
* @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT
*/
namespace MichaelRubel\ValueObjects\Collection\Complex;
use Illuminate\Support\Collection;
use Illuminate\Support\Stringable;
use Illuminate\Validation\ValidationException;
use MichaelRubel\Formatters\Collection\FullNameFormatter;
/**
* "FullName" object presenting a full name.
*
* @author Michael Rubél <michael@laravel.software>
*
* @template TKey of array-key
* @template TValue
*
* @method static static make(string|Stringable $value, int $limit = -1)
* @method static static from(string|Stringable $value, int $limit = -1)
* @method static static makeOrNull(string|Stringable|null $value, int $limit = -1)
*
* @extends Name<TKey, TValue>
*/
class FullName extends Name
{
/**
* @var Collection<int, string>
*/
protected Collection $split;
/**
* Create a new instance of the value object.
*
* @param string|Stringable $value
* @param int $limit
*/
public function __construct(string|Stringable $value, protected int $limit = -1)
{
static::beforeParentCalls(fn () => $this->split());
parent::__construct($value);
}
/**
* Get the full name.
*
* @return string
*/
public function fullName(): string
{
return $this->value();
}
/**
* Get the first name.
*
* @return string
*/
public function firstName(): string
{
return $this->split->first();
}
/**
* Get the last name.
*
* @return string
*/
public function lastName(): string
{
return $this->split->last();
}
/**
* Get an array representation of the value object.
*
* @return array<string, string|null>
*/
public function toArray(): array
{
return [
'fullName' => $this->fullName(),
'firstName' => $this->firstName(),
'lastName' => $this->lastName(),
];
}
/**
* Validate the value object data.
*
* @return void
*/
protected function validate(): void
{
if ($this->value() === '') {
throw ValidationException::withMessages(['Full name cannot be empty.']);
}
if (count($this->split) < 2) {
throw ValidationException::withMessages(['Full name should have a first name and last name.']);
}
}
/**
* Sanitize the value.
*
* @return void
*/
protected function sanitize(): void
{
$this->value = format(FullNameFormatter::class, $this->value());
}
/**
* Split the value.
*
* @return void
*/
protected function split(): void
{
$this->split = str($this->value())->split('/\s/', $this->limit);
}
}