Passed
Pull Request — main (#2)
by Michael
03:07
created

Uuid::validate()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 0
b 0
f 0
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 MichaelRubel\ValueObjects\ValueObject;
16
17
/**
18
 * "Uuid" object presenting unique ID.
19
 *
20
 * @author Michael Rubél <[email protected]>
21
 *
22
 * @template TKey of array-key
23
 * @template TValue
24
 *
25
 * @method static static make(string $value, string|null $name = null)
26
 * @method static static from(string $value, string|null $name = null)
27
 *
28
 * @extends ValueObject<TKey, TValue>
29
 */
30
class Uuid extends ValueObject
31
{
32
    /**
33
     * Create a new instance of the value object.
34
     *
35
     * @param  string  $value
36
     * @param  string|null  $name
37
     */
38 11
    public function __construct(
39
        protected string $value,
40
        protected ?string $name = null,
41
    ) {
42 11
        $this->validate();
43
    }
44
45
    /**
46
     * Get the UUID value.
47
     *
48
     * @return string
49
     */
50 1
    public function uuid(): string
51
    {
52 1
        return $this->value();
53
    }
54
55
    /**
56
     * Get the UUID name if present.
57
     *
58
     * @return string|null
59
     */
60 2
    public function name(): ?string
61
    {
62 2
        return $this->name;
63
    }
64
65
    /**
66
     * Get the object value.
67
     *
68
     * @return string
69
     */
70 8
    public function value(): string
71
    {
72 8
        return $this->value;
73
    }
74
75
    /**
76
     * Get an array representation of the value object.
77
     *
78
     * @return array<string, string|null>
79
     */
80 1
    public function toArray(): array
81
    {
82
        return [
0 ignored issues
show
introduced by
The expression return array('name' => $...lue' => $this->value()) returns an array which contains values of type string which are incompatible with the return type Illuminate\Contracts\Support\TValue mandated by Illuminate\Contracts\Support\Arrayable::toArray().
Loading history...
83 1
            'name'  => $this->name(),
84 1
            'value' => $this->value(),
85
        ];
86
    }
87
88
    /**
89
     * Validate the value object data.
90
     *
91
     * @return void
92
     */
93 11
    protected function validate(): void
94
    {
95 11
        if (! str($this->value)->isUuid()) {
96 1
            throw new \InvalidArgumentException('UUID is invalid.');
97
        }
98
    }
99
}
100