AggregateUuid::unfold()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 1
c 1
b 0
f 0
dl 0
loc 3
rs 10
cc 1
nc 1
nop 1
1
<?php declare(strict_types=1);
2
namespace Kepawni\Twilted\Basic;
3
4
use Kepawni\Twilted\EntityIdentifier;
5
use Kepawni\Twilted\Foldable;
6
use Ramsey\Uuid\Exception\InvalidUuidStringException;
7
use Ramsey\Uuid\Uuid;
8
use Ramsey\Uuid\UuidInterface;
9
10
class AggregateUuid implements EntityIdentifier
11
{
12
    /** @var UuidInterface */
13
    private $uuid;
14
15
    private function __construct(UuidInterface $uuid)
16
    {
17
        $this->uuid = $uuid;
18
    }
19
20
    /**
21
     * @return static
22
     */
23
    public static function createRandom(): self
24
    {
25
        return new static(Uuid::uuid4());
26
    }
27
28
    /**
29
     * @param string $leaflet The compact representation of this instance.
30
     *
31
     * @return static The instance reconstituted from unfolding the leaflet.
32
     * @throws InvalidUuidStringException When the leaflet is not a valid UUID string.
33
     */
34
    public static function unfold(string $leaflet): Foldable
35
    {
36
        return new static(Uuid::fromString($leaflet));
37
    }
38
39
    /**
40
     * @param mixed $other Another value to test.
41
     *
42
     * @return bool True when the other value is equal to this instance.
43
     */
44
    public function equals($other): bool
45
    {
46
        return $other instanceof static && $this->uuid->equals($other->uuid);
47
    }
48
49
    /**
50
     * @return string A compact representation (like a leaflet) of this instance.
51
     */
52
    public function fold(): string
53
    {
54
        return $this->uuid->toString();
55
    }
56
}
57