Lines 100.00% 9 / 9
Functions and Methods 100.00% 5 / 5
Classes and Traits 100.00% 1 / 1
Name Lines Functions and Methods CRAP Classes and Traits
RegistryId 100.00% 9 / 9 100.00% 5 / 5 9 100.00% 1 / 1
 __construct 100.00% 3 / 3 100.00% 1 / 1 2
 tryFromString 100.00% 3 / 3 100.00% 1 / 1 2
 fromStringOrId 100.00% 1 / 1 100.00% 1 / 1 2
 equals 100.00% 1 / 1 100.00% 1 / 1 2
 __toString 100.00% 1 / 1 100.00% 1 / 1 1
 describeKind n/a 0 / 0 n/a 0 / 0 0
1<?php
2
3declare(strict_types=1);
4
5namespace Stewart\Contracts\Registry;
6
7use Stewart\Contracts\Exception\IdentifierError;
8use Stewart\Contracts\Exception\IdentifierException;
9use Stringable;
10
11abstract readonly class RegistryId implements Stringable
12{
13    public string $value;
14
15    /** @throws IdentifierException */
16    final public function __construct(string $value)
17    {
18        if ($value === '') {
19            throw IdentifierException::createForReason(IdentifierError::RegistryIdEmpty, kind: $this->describeKind());
20        }
21
22        $this->value = $value;
23    }
24
25    public static function tryFromString(string $value): ?static
26    {
27        try {
28            return new static($value);
29        } catch (IdentifierException) {
30            return null;
31        }
32    }
33
34    /** @throws IdentifierException */
35    public static function fromStringOrId(self|string $id): static
36    {
37        return $id instanceof static ? $id : new static((string) $id);
38    }
39
40    public function equals(self $other): bool
41    {
42        return $other::class === static::class && $other->value === $this->value;
43    }
44
45    public function __toString(): string
46    {
47        return $this->value;
48    }
49
50    abstract protected function describeKind(): string;
51}