|
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\Traits\Conditionable; |
|
9
|
|
|
use Illuminate\Support\Traits\Macroable; |
|
10
|
|
|
use Illuminate\Support\Traits\Tappable; |
|
11
|
|
|
use MichaelRubel\ValueObjects\ValueObject; |
|
12
|
|
|
|
|
13
|
|
|
class Uuid implements ValueObject, Arrayable |
|
14
|
|
|
{ |
|
15
|
|
|
use Macroable, Conditionable, Tappable; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Create a new value object instance. |
|
19
|
|
|
* |
|
20
|
|
|
* @param string|null $uuid |
|
21
|
|
|
* @param string|null $name |
|
22
|
|
|
*/ |
|
23
|
8 |
|
final public function __construct( |
|
24
|
|
|
public ?string $uuid, |
|
25
|
|
|
public ?string $name = null, |
|
26
|
|
|
) { |
|
27
|
8 |
|
if (! str($this->uuid)->isUuid()) { |
|
28
|
1 |
|
throw new \InvalidArgumentException('Your UUID is invalid.'); |
|
29
|
|
|
} |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* Return a new instance of value object. |
|
34
|
|
|
* |
|
35
|
|
|
* @param string|null $uuid |
|
36
|
|
|
* @param string|null $name |
|
37
|
|
|
* |
|
38
|
|
|
* @return static |
|
39
|
|
|
*/ |
|
40
|
3 |
|
public static function make( |
|
41
|
|
|
?string $uuid, |
|
42
|
|
|
?string $name = null, |
|
43
|
|
|
): static { |
|
44
|
3 |
|
return new static($uuid, $name); |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function name(): string |
|
51
|
|
|
{ |
|
52
|
1 |
|
return str($this->name)->value(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function value(): string |
|
59
|
|
|
{ |
|
60
|
3 |
|
return $this->uuid; |
|
|
|
|
|
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return array |
|
65
|
|
|
*/ |
|
66
|
1 |
|
public function toArray(): array |
|
67
|
|
|
{ |
|
68
|
|
|
return [ |
|
|
|
|
|
|
69
|
1 |
|
'name' => $this->name, |
|
70
|
1 |
|
'value' => $this->uuid, |
|
71
|
|
|
]; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* Return the first UUID if cast to string. |
|
76
|
|
|
* |
|
77
|
|
|
* @return string |
|
78
|
|
|
*/ |
|
79
|
1 |
|
public function __toString(): string |
|
80
|
|
|
{ |
|
81
|
1 |
|
return str($this->uuid)->value(); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|