Uuid::fromNative()   A
last analyzed

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 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 2
rs 10
c 1
b 0
f 0
1
<?php declare(strict_types=1);
2
/**
3
 * This file is part of the daikon-cqrs/value-object project.
4
 *
5
 * For the full copyright and license information, please view the LICENSE
6
 * file that was distributed with this source code.
7
 */
8
9
namespace Daikon\ValueObject;
10
11
use Daikon\Interop\Assertion;
12
use Ramsey\Uuid\Uuid as RamseyUuid;
13
use Ramsey\Uuid\UuidInterface;
14
15
final class Uuid implements ValueObjectInterface
16
{
17
    private ?UuidInterface $value;
18
19 1
    public static function generate(): self
20
    {
21 1
        return new self(RamseyUuid::uuid4());
22
    }
23
24
    /** @param null|string $value */
25 3
    public static function fromNative($value): self
26
    {
27 3
        Assertion::nullOrString($value, 'Trying to create Uuid VO from unsupported value type.');
28 3
        return empty($value) ? new self : new self(RamseyUuid::fromString($value));
29
    }
30
31
    /** @param self $comparator */
32 1
    public function equals($comparator): bool
33
    {
34 1
        Assertion::isInstanceOf($comparator, self::class);
35 1
        return $this->toNative() === $comparator->toNative();
36
    }
37
38 2
    public function toNative(): ?string
39
    {
40 2
        return $this->value ? $this->value->toString() : $this->value;
41
    }
42
43 1
    public function __toString(): string
44
    {
45 1
        return $this->value ? $this->value->toString() : 'null';
46
    }
47
48 3
    private function __construct(UuidInterface $value = null)
49
    {
50 3
        $this->value = $value;
51 3
    }
52
}
53