Skip to content

Commit

Permalink
Added Stringable support to Reference interface
Browse files Browse the repository at this point in the history
Signed-off-by: Tom Wright <[email protected]>
  • Loading branch information
betterthanclay committed Jan 19, 2024
1 parent ac8086c commit 1d5aacb
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 94 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
* Added Stringable support to Reference interface

## v0.1.0 (2024-01-19)
* Ported initial Reference implementation
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
17 changes: 9 additions & 8 deletions src/Reference.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,41 +11,42 @@

use Closure;
use DecodeLabs\Tagged\Markup;
use Stringable;

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(
Expand All @@ -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;
Expand Down
76 changes: 0 additions & 76 deletions src/Reference/Guid.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down
17 changes: 9 additions & 8 deletions src/ReferenceTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
use DecodeLabs\Exceptional;
use DecodeLabs\Tagged as Html;
use DecodeLabs\Tagged\Markup;
use Stringable;
use Throwable;

trait ReferenceTrait
Expand All @@ -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;
Expand All @@ -43,7 +44,7 @@ public static function instantiate(
}

public static function tryInstantiate(
?string $value
string|Stringable|null $value
): ?static {
$output = new static($value);

Expand All @@ -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);
}
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand All @@ -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;
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit 1d5aacb

Please sign in to comment.