Passed
Push — main ( 6175ee...752a2b )
by Michael
03:40
created

Uuid::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 1
c 1
b 0
f 1
dl 0
loc 3
ccs 2
cts 2
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
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;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->uuid could return the type null which is incompatible with the type-hinted return string. Consider adding an additional type-check to rule them out.
Loading history...
61
    }
62
63
    /**
64
     * @return array
65
     */
66 1
    public function toArray(): array
67
    {
68
        return [
0 ignored issues
show
introduced by
The expression return array('name' => $...'value' => $this->uuid) 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...
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