Skip to content

Commit 77878b8

Browse files
committed
Adding conditionable on Host interface
1 parent f06c740 commit 77878b8

File tree

4 files changed

+70
-2
lines changed

4 files changed

+70
-2
lines changed

src/Domain.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use Iterator;
88
use Stringable;
99

10+
use function is_bool;
11+
1012
use const FILTER_FLAG_IPV4;
1113
use const FILTER_VALIDATE_IP;
1214

@@ -183,4 +185,25 @@ public function isAbsolute(): bool
183185
{
184186
return '' === $this->label(0);
185187
}
188+
189+
/**
190+
* Apply the callback if the given "condition" is (or resolves to) true.
191+
*
192+
* @param (callable($this): bool)|bool $condition
193+
* @param callable($this): (self|null) $onSuccess
194+
* @param ?callable($this): (self|null) $onFail
195+
*
196+
*/
197+
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): self
198+
{
199+
if (!is_bool($condition)) {
200+
$condition = $condition($this);
201+
}
202+
203+
return match (true) {
204+
$condition => $onSuccess($this),
205+
null !== $onFail => $onFail($this),
206+
default => $this,
207+
} ?? $this;
208+
}
186209
}

src/Host.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
* @method bool isAbsolute() tells whether the domain is absolute or not.
1616
* @method static withRootLabel() returns an instance with its Root label. (see https://tools.ietf.org/html/rfc3986#section-3.2.2)
1717
* @method static withoutRootLabel() returns an instance without its Root label. (see https://tools.ietf.org/html/rfc3986#section-3.2.2)
18+
* @method static when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null) apply the callback if the given "condition" is (or resolves to) true.
1819
*/
1920
interface Host extends Countable, JsonSerializable
2021
{

src/RegisteredName.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use function filter_var;
1717
use function implode;
1818
use function in_array;
19+
use function is_bool;
1920
use function ksort;
2021
use function preg_match;
2122
use function rawurldecode;
@@ -384,4 +385,25 @@ public function isAbsolute(): bool
384385
{
385386
return '' === $this->label(0);
386387
}
388+
389+
/**
390+
* Apply the callback if the given "condition" is (or resolves to) true.
391+
*
392+
* @param (callable($this): bool)|bool $condition
393+
* @param callable($this): (self|null) $onSuccess
394+
* @param ?callable($this): (self|null) $onFail
395+
*
396+
*/
397+
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): self
398+
{
399+
if (!is_bool($condition)) {
400+
$condition = $condition($this);
401+
}
402+
403+
return match (true) {
404+
$condition => $onSuccess($this),
405+
null !== $onFail => $onFail($this),
406+
default => $this,
407+
} ?? $this;
408+
}
387409
}

src/ResolvedDomain.php

Lines changed: 24 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use Stringable;
88

99
use function count;
10+
use function is_bool;
1011

1112
final class ResolvedDomain implements ResolvedDomainName
1213
{
@@ -217,7 +218,7 @@ public function withSuffix(DomainNameProvider|Host|Stringable|string|int|null $s
217218
$domain = $this->domain->withoutRootLabel()->slice(count($this->suffix))->append($suffix);
218219

219220
return new self(
220-
$this->domain->isAbsolute() ? $domain->withRootLabel() : $domain,
221+
$domain->when($this->domain->isAbsolute(), fn (DomainName $domainName) => $domain->withRootLabel()),
221222
$suffix->normalize($this->domain)
222223
);
223224
}
@@ -245,7 +246,7 @@ public function withSubDomain(DomainNameProvider|Host|Stringable|string|int|null
245246

246247
$domain = $this->registrableDomain->prepend($subDomain);
247248

248-
return new self($this->domain->isAbsolute() ? $domain->withRootLabel() : $domain, $this->suffix);
249+
return new self($domain->when($this->domain->isAbsolute(), fn (DomainName $domainName) => $domain->withRootLabel()), $this->suffix);
249250
}
250251

251252
/**
@@ -276,4 +277,25 @@ public function withSecondLevelDomain(DomainNameProvider|Host|Stringable|string|
276277

277278
return new self($newRegistrableDomain->prepend($this->subDomain), $this->suffix);
278279
}
280+
281+
/**
282+
* Apply the callback if the given "condition" is (or resolves to) true.
283+
*
284+
* @param (callable($this): bool)|bool $condition
285+
* @param callable($this): (self|null) $onSuccess
286+
* @param ?callable($this): (self|null) $onFail
287+
*
288+
*/
289+
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): self
290+
{
291+
if (!is_bool($condition)) {
292+
$condition = $condition($this);
293+
}
294+
295+
return match (true) {
296+
$condition => $onSuccess($this),
297+
null !== $onFail => $onFail($this),
298+
default => $this,
299+
} ?? $this;
300+
}
279301
}

0 commit comments

Comments
 (0)