|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/** |
|
6
|
|
|
* This file is part of michael-rubel/laravel-value-objects. (https://github.com/michael-rubel/laravel-value-objects) |
|
7
|
|
|
* |
|
8
|
|
|
* @link https://github.com/michael-rubel/laravel-value-objects for the canonical source repository |
|
9
|
|
|
* @copyright Copyright (c) 2022 Michael Rubél. (https://github.com/michael-rubel/) |
|
10
|
|
|
* @license https://raw.githubusercontent.com/michael-rubel/laravel-value-objects/main/LICENSE.md MIT |
|
11
|
|
|
*/ |
|
12
|
|
|
|
|
13
|
|
|
namespace MichaelRubel\ValueObjects\Collection\Complex; |
|
14
|
|
|
|
|
15
|
|
|
use Illuminate\Support\Str; |
|
16
|
|
|
use MichaelRubel\ValueObjects\ValueObject; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* "Uuid" object presenting unique ID. |
|
20
|
|
|
* |
|
21
|
|
|
* @author Michael Rubél <[email protected]> |
|
22
|
|
|
* |
|
23
|
|
|
* @template TKey of array-key |
|
24
|
|
|
* @template TValue |
|
25
|
|
|
* |
|
26
|
|
|
* @method static static make(string $value, string|null $name = null) |
|
27
|
|
|
* @method static static from(string $value, string|null $name = null) |
|
28
|
|
|
* |
|
29
|
|
|
* @extends ValueObject<TKey, TValue> |
|
30
|
|
|
*/ |
|
31
|
|
|
class Uuid extends ValueObject |
|
32
|
|
|
{ |
|
33
|
|
|
/** |
|
34
|
|
|
* Create a new instance of the value object. |
|
35
|
|
|
* |
|
36
|
|
|
* @param string $value |
|
37
|
|
|
* @param string|null $name |
|
38
|
|
|
*/ |
|
39
|
11 |
|
public function __construct( |
|
40
|
|
|
protected string $value, |
|
41
|
|
|
protected ?string $name = null, |
|
42
|
|
|
) { |
|
43
|
11 |
|
$this->validate(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* Get the UUID value. |
|
48
|
|
|
* |
|
49
|
|
|
* @return string |
|
50
|
|
|
*/ |
|
51
|
1 |
|
public function uuid(): string |
|
52
|
|
|
{ |
|
53
|
1 |
|
return $this->value(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* Get the UUID name if present. |
|
58
|
|
|
* |
|
59
|
|
|
* @return string|null |
|
60
|
|
|
*/ |
|
61
|
2 |
|
public function name(): ?string |
|
62
|
|
|
{ |
|
63
|
2 |
|
return $this->name; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Get the object value. |
|
68
|
|
|
* |
|
69
|
|
|
* @return string |
|
70
|
|
|
*/ |
|
71
|
8 |
|
public function value(): string |
|
72
|
|
|
{ |
|
73
|
8 |
|
return $this->value; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
|
|
/** |
|
77
|
|
|
* Get an array representation of the value object. |
|
78
|
|
|
* |
|
79
|
|
|
* @return array<string, string|null> |
|
80
|
|
|
*/ |
|
81
|
1 |
|
public function toArray(): array |
|
82
|
|
|
{ |
|
83
|
|
|
return [ |
|
|
|
|
|
|
84
|
1 |
|
'name' => $this->name(), |
|
85
|
1 |
|
'value' => $this->value(), |
|
86
|
|
|
]; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* Verify the value object input. |
|
91
|
|
|
* |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
11 |
|
protected function validate(): void |
|
95
|
|
|
{ |
|
96
|
11 |
|
if (! Str::isUuid($this->value)) { |
|
97
|
1 |
|
throw new \InvalidArgumentException('UUID is invalid.'); |
|
98
|
|
|
} |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|