-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringId.php
More file actions
81 lines (66 loc) · 1.81 KB
/
StringId.php
File metadata and controls
81 lines (66 loc) · 1.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
<?php
/*
* Copyright 2025 Cloud Creativity Limited
*
* Use of this source code is governed by an MIT-style
* license that can be found in the LICENSE file or at
* https://opensource.org/licenses/MIT.
*/
declare(strict_types=1);
namespace CloudCreativity\Modules\Toolkit\Identifiers;
use CloudCreativity\Modules\Contracts\Toolkit\Identifiers\Identifier;
use CloudCreativity\Modules\Toolkit\ContractException;
use CloudCreativity\Modules\Toolkit\Contracts;
use JsonSerializable;
final readonly class StringId implements Identifier, JsonSerializable
{
use IsIdentifier;
public static function from(Identifier|string $value): self
{
return match(true) {
$value instanceof self => $value,
is_string($value) => new self($value),
default => throw new ContractException(
'Unexpected identifier type, received: ' . get_debug_type($value),
),
};
}
public function __construct(public string $value)
{
Contracts::assert(
!empty($this->value) || '0' === $this->value,
'Identifier value must be a non-empty string.',
);
}
public function __toString(): string
{
return $this->value;
}
public function toString(): string
{
return $this->value;
}
public function is(?Identifier $other): bool
{
if ($other instanceof self) {
return $this->equals($other);
}
return false;
}
public function equals(self $other): bool
{
return $this->value === $other->value;
}
public function key(): string
{
return $this->value;
}
public function context(): string
{
return $this->value;
}
public function jsonSerialize(): string
{
return $this->value;
}
}