Skip to content

Commit

Permalink
Adding tryNew to all URI related classes which already have a new method
Browse files Browse the repository at this point in the history
  • Loading branch information
nyamsprod committed Dec 21, 2024
1 parent c01d87e commit c02e605
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
21 changes: 21 additions & 0 deletions BaseUri.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
use function explode;
use function implode;
use function in_array;
use function is_bool;
use function preg_match;
use function rawurldecode;
use function str_repeat;
Expand Down Expand Up @@ -361,6 +362,26 @@ public function relativize(Stringable|string $uri): static
);
}

/**
* Apply the callback if the given "condition" is (or resolves to) true.
*
* @param (callable($this): bool)|bool $condition
* @param callable($this): (self|null) $onSuccess
* @param ?callable($this): (self|null) $onFail
*/
final public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): self
{
if (!is_bool($condition)) {
$condition = $condition($this);
}

return match (true) {
$condition => $onSuccess($this),
null !== $onFail => $onFail($this),
default => $this,
} ?? $this;
}

final protected function computeOrigin(Psr7UriInterface|UriInterface $uri, ?string $nullValue): Psr7UriInterface|UriInterface|null
{
$scheme = $uri->getScheme();
Expand Down
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ All Notable changes to `League\Uri` will be documented in this file
- `Http::when` conditional method to ease component building logic.
- `Uri::when` conditional method to ease component building logic.
- `BaseUri::when` conditional method to ease component building logic.
- `Uri::tryNew` returns a new `Uri` instance on success or null on failure.
- `Http::tryNew` returns a new `Uri` instance on success or null on failure.

### Fixed

Expand Down
12 changes: 12 additions & 0 deletions Http.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,18 @@ public static function new(Stringable|string $uri = ''): self
return self::fromComponents(UriString::parse($uri));
}

/**
* Create a new instance from a string.or a stringable structure or returns null on failure.
*/
public static function tryNew(Stringable|string $uri = ''): ?self
{
try {
return self::new($uri);
} catch (UriException) {
return null;
}
}

/**
* Create a new instance from a hash of parse_url parts.
*
Expand Down
12 changes: 12 additions & 0 deletions Uri.php
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,18 @@ private function formatPort(?int $port = null): ?int
};
}

/**
* Create a new instance from a string.or a stringable structure or returns null on failure.
*/
public static function tryNew(Stringable|string $uri = ''): ?self
{
try {
return self::new($uri);
} catch (UriException) {
return null;
}
}

/**
* Create a new instance from a string.
*/
Expand Down

0 comments on commit c02e605

Please sign in to comment.