Passed
Pull Request — main (#2)
by Michael
04:04
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 InvalidArgumentException;
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 11
    public function value(): string
72
    {
73 11
        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 [
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...
84 1
            'name'  => $this->name(),
85 1
            'value' => $this->value(),
86
        ];
87
    }
88
89
    /**
90
     * Validate the value object data.
91
     *
92
     * @return void
93
     */
94 11
    protected function validate(): void
95
    {
96 11
        if (! str($this->value())->isUuid()) {
97 1
            throw new InvalidArgumentException('UUID is invalid.');
98
        }
99
    }
100
}
101