From 1d5aacb90be927ec4826aaf4a303e3dfe256ee4c Mon Sep 17 00:00:00 2001 From: Tom Wright Date: Fri, 19 Jan 2024 18:27:25 +0000 Subject: [PATCH] Added Stringable support to Reference interface Signed-off-by: Tom Wright --- CHANGELOG.md | 2 ++ composer.json | 3 +- src/Reference.php | 17 +++++----- src/Reference/Guid.php | 76 ------------------------------------------ src/ReferenceTrait.php | 17 +++++----- 5 files changed, 21 insertions(+), 94 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 904b75f..7a9049a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,2 +1,4 @@ +* Added Stringable support to Reference interface + ## v0.1.0 (2024-01-19) * Ported initial Reference implementation diff --git a/composer.json b/composer.json index 38554c9..9b3f491 100644 --- a/composer.json +++ b/composer.json @@ -15,8 +15,7 @@ "require-dev": { "decodelabs/glitch": "^0.18.11", "decodelabs/tagged": "^0.14.12", - "decodelabs/phpstan-decodelabs": "^0.6.7", - "decodelabs/guidance": "^0.1.8" + "decodelabs/phpstan-decodelabs": "^0.6.7" }, "suggest": { "decodelabs/tagged": "For formatting as HTML" diff --git a/src/Reference.php b/src/Reference.php index 7dc9a24..2887f91 100644 --- a/src/Reference.php +++ b/src/Reference.php @@ -11,6 +11,7 @@ use Closure; use DecodeLabs\Tagged\Markup; +use Stringable; interface Reference { @@ -18,34 +19,34 @@ interface Reference * @return static */ public static function instantiate( - ?string $value + string|Stringable|null $value ): Reference; /** * @return static|null */ public static function tryInstantiate( - ?string $value + string|Stringable|null $value ): ?Reference; public static function isValid( - ?string $value + string|Stringable|null $value ): bool; public static function isCanonical( - ?string $value + string|Stringable|null $value ): bool; public static function canonicalize( - ?string $value + string|Stringable|null $value ): ?string; public static function normalize( - ?string $value + string|Stringable|null $value ): ?string; public static function format( - ?string $value + string|Stringable|null $value ): ?Markup; public static function getCanonicalPattern( @@ -64,7 +65,7 @@ public static function getSanitizer(): Closure; public static function isGeneric(): bool; public function __construct( - ?string $value + string|Stringable|null $value ); public function validate(): bool; diff --git a/src/Reference/Guid.php b/src/Reference/Guid.php index 33e84a5..aa46389 100644 --- a/src/Reference/Guid.php +++ b/src/Reference/Guid.php @@ -9,7 +9,6 @@ namespace DecodeLabs\Referential\Reference; -use DecodeLabs\Guidance\Uuid; use DecodeLabs\Referential\Reference; use DecodeLabs\Referential\ReferenceTrait; use DecodeLabs\Tagged as Html; @@ -33,81 +32,6 @@ class Guid implements Reference public const EXAMPLE = 'd2516786-28da-c4d4-f701-30df4b2159d9'; - /** - * Create object if valid - */ - public static function instantiate( - string|Uuid|null $value - ): static { - if ($value instanceof Uuid) { - $value = (string)$value; - } - - return static::parentInstantiate($value); - } - - public static function tryInstantiate( - string|Uuid|null $value - ): ?static { - if ($value instanceof Uuid) { - $value = (string)$value; - } - - return static::parentTryInstantiate($value); - } - - /** - * Generate and validate - */ - public static function isValid( - string|Uuid|null $value - ): bool { - if ($value instanceof Uuid) { - $value = (string)$value; - } - - return static::parentIsValid($value); - } - - /** - * Generate and convert to canonical - */ - public static function canonicalize( - string|Uuid|null $value - ): ?string { - if ($value instanceof Uuid) { - $value = (string)$value; - } - - return static::parentCanonicalize($value); - } - - /** - * Generate and format - */ - public static function normalize( - string|Uuid|null $value - ): ?string { - if ($value instanceof Uuid) { - $value = (string)$value; - } - - return static::parentNormalize($value); - } - - /** - * Generate and format as HTML - */ - public static function format( - string|Uuid|null $value - ): ?Markup { - if ($value instanceof Uuid) { - $value = (string)$value; - } - - return static::parentFormat($value); - } - /** * Prepare canonical string diff --git a/src/ReferenceTrait.php b/src/ReferenceTrait.php index 5c6bc46..761da1a 100644 --- a/src/ReferenceTrait.php +++ b/src/ReferenceTrait.php @@ -13,6 +13,7 @@ use DecodeLabs\Exceptional; use DecodeLabs\Tagged as Html; use DecodeLabs\Tagged\Markup; +use Stringable; use Throwable; trait ReferenceTrait @@ -33,7 +34,7 @@ trait ReferenceTrait * Create object if valid */ public static function instantiate( - ?string $value + string|Stringable|null $value ): static { if ($output = static::tryInstantiate($value)) { return $output; @@ -43,7 +44,7 @@ public static function instantiate( } public static function tryInstantiate( - ?string $value + string|Stringable|null $value ): ?static { $output = new static($value); @@ -58,7 +59,7 @@ public static function tryInstantiate( * Generate and validate */ public static function isValid( - ?string $value + string|Stringable|null $value ): bool { return (bool)static::tryInstantiate($value); } @@ -67,7 +68,7 @@ public static function isValid( * Is reference in canonical format? */ public static function isCanonical( - ?string $value + string|Stringable|null $value ): bool { if (!$ref = static::tryInstantiate($value)) { return false; @@ -80,7 +81,7 @@ public static function isCanonical( * Generate and convert to canonical */ public static function canonicalize( - ?string $value + string|Stringable|null $value ): ?string { if (!$ref = static::tryInstantiate($value)) { return null; @@ -93,7 +94,7 @@ public static function canonicalize( * Generate and format */ public static function normalize( - ?string $value + string|Stringable|null $value ): ?string { if (!$ref = static::tryInstantiate($value)) { return null; @@ -106,7 +107,7 @@ public static function normalize( * Generate and format as HTML */ public static function format( - ?string $value + string|Stringable|null $value ): ?Markup { if (!$ref = static::tryInstantiate($value)) { return null; @@ -226,7 +227,7 @@ public static function isGeneric(): bool * Init with any value */ public function __construct( - ?string $value + string|Stringable|null $value ) { $this->raw = (string)$value; $this->canonical = $this->prepareCanonical($this->raw);