1212
1313namespace CloudCreativity \Modules \Toolkit \Identifiers ;
1414
15+ use BackedEnum ;
1516use CloudCreativity \Modules \Contracts \Toolkit \Identifiers \Identifier ;
1617use CloudCreativity \Modules \Toolkit \ContractException ;
1718use CloudCreativity \Modules \Toolkit \Contracts ;
1819use Ramsey \Uuid \UuidInterface ;
20+ use UnitEnum ;
1921
2022final class Guid implements Identifier
2123{
@@ -35,47 +37,47 @@ public static function from(Identifier $value): self
3537 /**
3638 * Create a GUID with an integer id.
3739 *
38- * @param string $type
40+ * @param UnitEnum| string $type
3941 * @param int $id
40- * @return static
42+ * @return self
4143 */
42- public static function fromInteger (string $ type , int $ id ): self
44+ public static function fromInteger (UnitEnum | string $ type , int $ id ): self
4345 {
4446 return new self ($ type , new IntegerId ($ id ));
4547 }
4648
4749 /**
4850 * Create a GUID with a string id.
4951 *
50- * @param string $type
52+ * @param UnitEnum| string $type
5153 * @param string $id
52- * @return static
54+ * @return self
5355 */
54- public static function fromString (string $ type , string $ id ): self
56+ public static function fromString (UnitEnum | string $ type , string $ id ): self
5557 {
5658 return new self ($ type , new StringId ($ id ));
5759 }
5860
5961 /**
6062 * Create a GUID for a UUID.
6163 *
62- * @param string $type
64+ * @param UnitEnum| string $type
6365 * @param UuidInterface|string $uuid
6466 * @return self
6567 */
66- public static function fromUuid (string $ type , UuidInterface |string $ uuid ): self
68+ public static function fromUuid (UnitEnum | string $ type , UuidInterface |string $ uuid ): self
6769 {
6870 return new self ($ type , Uuid::from ($ uuid ));
6971 }
7072
7173 /**
7274 * Create a GUID.
7375 *
74- * @param string $type
76+ * @param UnitEnum| string $type
7577 * @param string|int $id
7678 * @return self
7779 */
78- public static function make (string $ type , string |int $ id ): self
80+ public static function make (UnitEnum | string $ type , string |int $ id ): self
7981 {
8082 if (is_int ($ id )) {
8183 return self ::fromInteger ($ type , $ id );
@@ -87,11 +89,11 @@ public static function make(string $type, string|int $id): self
8789 /**
8890 * Guid constructor.
8991 *
90- * @param string $type
92+ * @param UnitEnum| string $type
9193 * @param StringId|IntegerId|Uuid $id
9294 */
9395 public function __construct (
94- public readonly string $ type ,
96+ public readonly UnitEnum | string $ type ,
9597 public readonly StringId |IntegerId |Uuid $ id ,
9698 ) {
9799 Contracts::assert (!empty ($ this ->type ), 'Type must be a non-empty string. ' );
@@ -106,12 +108,18 @@ public function __toString(): string
106108 }
107109
108110 /**
109- * @param string $type
111+ * @param UnitEnum| string ...$types
110112 * @return bool
111113 */
112- public function isType (string $ type ): bool
114+ public function isType (UnitEnum | string ... $ types ): bool
113115 {
114- return $ this ->type === $ type ;
116+ foreach ($ types as $ type ) {
117+ if ($ this ->type === $ type ) {
118+ return true ;
119+ }
120+ }
121+
122+ return false ;
115123 }
116124
117125 /**
@@ -149,7 +157,9 @@ public function equals(self $other): bool
149157 */
150158 public function toString (string $ glue = ': ' ): string
151159 {
152- return "{$ this ->type }{$ glue }{$ this ->id ->value }" ;
160+ $ type = $ this ->type ();
161+
162+ return "{$ type }{$ glue }{$ this ->id ->value }" ;
153163 }
154164
155165 /**
@@ -166,26 +176,44 @@ public function key(): string
166176 public function context (): array
167177 {
168178 return [
169- 'type ' => $ this ->type ,
179+ 'type ' => $ this ->type () ,
170180 'id ' => $ this ->id ->context (),
171181 ];
172182 }
173183
174184 /**
175185 * Assert that this GUID is of the expected type.
176186 *
177- * @param string $expected
187+ * @param UnitEnum| string $expected
178188 * @param string $message
179189 * @return $this
180190 */
181- public function assertType (string $ expected , string $ message = '' ): self
191+ public function assertType (UnitEnum | string $ expected , string $ message = '' ): self
182192 {
183193 Contracts::assert ($ this ->type === $ expected , $ message ?: sprintf (
184194 'Expecting type "%s", received "%s". ' ,
185- $ expected ,
186- $ this ->type ,
195+ match (true ) {
196+ $ expected instanceof BackedEnum => $ expected ->value ,
197+ $ expected instanceof UnitEnum => $ expected ->name ,
198+ default => $ expected ,
199+ },
200+ $ this ->type (),
187201 ));
188202
189203 return $ this ;
190204 }
205+
206+ /**
207+ * Get the type expressed as a string or an integer.
208+ *
209+ * @return string|int
210+ */
211+ public function type (): string |int
212+ {
213+ return match (true ) {
214+ $ this ->type instanceof BackedEnum => $ this ->type ->value ,
215+ $ this ->type instanceof UnitEnum => $ this ->type ->name ,
216+ default => $ this ->type ,
217+ };
218+ }
191219}
0 commit comments