|
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\Validation\ValidationException; |
|
16
|
|
|
use InvalidArgumentException; |
|
17
|
|
|
use MichaelRubel\ValueObjects\ValueObject; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* "Uuid" object presenting unique ID. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Michael Rubél <[email protected]> |
|
23
|
|
|
* |
|
24
|
|
|
* @template TKey of array-key |
|
25
|
|
|
* @template TValue |
|
26
|
|
|
* |
|
27
|
|
|
* @method static static make(string $value, string|null $name = null) |
|
28
|
|
|
* @method static static from(string $value, string|null $name = null) |
|
29
|
|
|
* @method static static makeOrNull(string|null $value, string|null $name = null) |
|
30
|
|
|
* |
|
31
|
|
|
* @extends ValueObject<TKey, TValue> |
|
32
|
|
|
*/ |
|
33
|
|
|
class Uuid extends ValueObject |
|
34
|
|
|
{ |
|
35
|
|
|
/** |
|
36
|
|
|
* @var string |
|
37
|
|
|
*/ |
|
38
|
|
|
protected string $value; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string|null |
|
42
|
|
|
*/ |
|
43
|
|
|
protected ?string $name = null; |
|
44
|
|
|
|
|
45
|
|
|
/** |
|
46
|
|
|
* Create a new instance of the value object. |
|
47
|
|
|
* |
|
48
|
|
|
* @param string $value |
|
49
|
|
|
* @param string|null $name |
|
50
|
|
|
*/ |
|
51
|
14 |
|
public function __construct(string $value, ?string $name = null) |
|
52
|
|
|
{ |
|
53
|
14 |
|
if (isset($this->value)) { |
|
54
|
1 |
|
throw new InvalidArgumentException(static::IMMUTABLE_MESSAGE); |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
14 |
|
$this->value = $value; |
|
58
|
14 |
|
$this->name = $name; |
|
59
|
|
|
|
|
60
|
14 |
|
$this->validate(); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* Get the UUID value. |
|
65
|
|
|
* |
|
66
|
|
|
* @return string |
|
67
|
|
|
*/ |
|
68
|
1 |
|
public function uuid(): string |
|
69
|
|
|
{ |
|
70
|
1 |
|
return $this->value(); |
|
71
|
|
|
} |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Get the UUID name if present. |
|
75
|
|
|
* |
|
76
|
|
|
* @return string|null |
|
77
|
|
|
*/ |
|
78
|
2 |
|
public function name(): ?string |
|
79
|
|
|
{ |
|
80
|
2 |
|
return $this->name; |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Get the object value. |
|
85
|
|
|
* |
|
86
|
|
|
* @return string |
|
87
|
|
|
*/ |
|
88
|
14 |
|
public function value(): string |
|
89
|
|
|
{ |
|
90
|
14 |
|
return $this->value; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
/** |
|
94
|
|
|
* Get an array representation of the value object. |
|
95
|
|
|
* |
|
96
|
|
|
* @return array<string, string|null> |
|
97
|
|
|
*/ |
|
98
|
1 |
|
public function toArray(): array |
|
99
|
|
|
{ |
|
100
|
1 |
|
return [ |
|
101
|
1 |
|
'name' => $this->name(), |
|
102
|
1 |
|
'value' => $this->value(), |
|
103
|
1 |
|
]; |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* Validate the value object data. |
|
108
|
|
|
* |
|
109
|
|
|
* @return void |
|
110
|
|
|
*/ |
|
111
|
14 |
|
protected function validate(): void |
|
112
|
|
|
{ |
|
113
|
14 |
|
if (! str($this->value())->isUuid()) { |
|
114
|
2 |
|
throw ValidationException::withMessages(['UUID is invalid.']); |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|