1212
1313namespace CloudCreativity \Modules \Toolkit \Identifiers ;
1414
15- use BackedEnum ;
1615use CloudCreativity \Modules \Contracts \Toolkit \Identifiers \Identifier ;
1716use CloudCreativity \Modules \Toolkit \ContractException ;
1817use CloudCreativity \Modules \Toolkit \Contracts ;
1918use Ramsey \Uuid \UuidInterface ;
2019use UnitEnum ;
2120
21+ use function CloudCreativity \Modules \Toolkit \enum_string ;
22+ use function CloudCreativity \Modules \Toolkit \enum_value ;
23+
2224final class Guid implements Identifier
2325{
2426 /**
@@ -62,10 +64,10 @@ public static function fromString(UnitEnum|string $type, string $id): self
6264 * Create a GUID for a UUID.
6365 *
6466 * @param UnitEnum|string $type
65- * @param UuidInterface|string $uuid
67+ * @param Uuid| UuidInterface|string $uuid
6668 * @return self
6769 */
68- public static function fromUuid (UnitEnum |string $ type , UuidInterface |string $ uuid ): self
70+ public static function fromUuid (UnitEnum |string $ type , Uuid | UuidInterface |string $ uuid ): self
6971 {
7072 return new self ($ type , Uuid::from ($ uuid ));
7173 }
@@ -157,7 +159,7 @@ public function equals(self $other): bool
157159 */
158160 public function toString (string $ glue = ': ' ): string
159161 {
160- $ type = $ this ->type ( );
162+ $ type = enum_string ( $ this ->type );
161163
162164 return "{$ type }{$ glue }{$ this ->id ->value }" ;
163165 }
@@ -176,7 +178,7 @@ public function key(): string
176178 public function context (): array
177179 {
178180 return [
179- 'type ' => $ this ->type ( ),
181+ 'type ' => enum_string ( $ this ->type ),
180182 'id ' => $ this ->id ->context (),
181183 ];
182184 }
@@ -190,14 +192,10 @@ public function context(): array
190192 */
191193 public function assertType (UnitEnum |string $ expected , string $ message = '' ): self
192194 {
193- Contracts::assert ($ this ->type === $ expected , $ message ?: sprintf (
195+ Contracts::assert ($ this ->type === $ expected , $ message ?: fn () => sprintf (
194196 'Expecting type "%s", received "%s". ' ,
195- match (true ) {
196- $ expected instanceof BackedEnum => $ expected ->value ,
197- $ expected instanceof UnitEnum => $ expected ->name ,
198- default => $ expected ,
199- },
200- $ this ->type (),
197+ enum_string ($ expected ),
198+ enum_string ($ this ->type ),
201199 ));
202200
203201 return $ this ;
@@ -210,10 +208,7 @@ public function assertType(UnitEnum|string $expected, string $message = ''): sel
210208 */
211209 public function type (): string |int
212210 {
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- };
211+ // TODO 4.0 use enum_string() instead
212+ return enum_value ($ this ->type );
218213 }
219214}
0 commit comments