Skip to content

Commit 794a7f3

Browse files
committed
Improve DataType::when conditionable
1 parent a1f988b commit 794a7f3

File tree

5 files changed

+60
-60
lines changed

5 files changed

+60
-60
lines changed

src/Dictionary.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use ArrayAccess;
88
use Bakame\Http\StructuredFields\Validation\Violation;
99
use CallbackFilterIterator;
10-
use Closure;
1110
use Countable;
1211
use DateTimeInterface;
1312
use Iterator;
@@ -241,22 +240,23 @@ public function equals(mixed $other): bool
241240
}
242241

243242
/**
244-
* @template TWhenParameter
243+
* Apply the callback if the given "condition" is (or resolves to) true.
245244
*
246-
* @param (Closure($this): TWhenParameter)|TWhenParameter $value
247-
* @param callable($this, TWhenParameter): ($this|null) $callback
245+
* @param (callable($this): bool)|bool $condition
246+
* @param callable($this): (self|null) $onSuccess
247+
* @param ?callable($this): (self|null) $onFail
248248
*/
249-
public function when($value, callable $callback): self
249+
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): self
250250
{
251-
if ($value instanceof Closure) {
252-
$value = $value($this);
251+
if (!is_bool($condition)) {
252+
$condition = $condition($this);
253253
}
254254

255-
if (!$value) {
256-
return $this;
257-
}
258-
259-
return $callback($this, $value) ?? $this;
255+
return match (true) {
256+
$condition => $onSuccess($this),
257+
null !== $onFail => $onFail($this),
258+
default => $this,
259+
} ?? $this;
260260
}
261261

262262
public function count(): int

src/InnerList.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use ArrayAccess;
88
use Bakame\Http\StructuredFields\Validation\Violation;
9-
use Closure;
109
use Countable;
1110
use DateTimeInterface;
1211
use Iterator;
@@ -183,22 +182,23 @@ public function equals(mixed $other): bool
183182
}
184183

185184
/**
186-
* @template TWhenParameter
185+
* Apply the callback if the given "condition" is (or resolves to) true.
187186
*
188-
* @param (Closure($this): TWhenParameter)|TWhenParameter $value
189-
* @param callable($this, TWhenParameter): ($this|null) $callback
187+
* @param (callable($this): bool)|bool $condition
188+
* @param callable($this): (self|null) $onSuccess
189+
* @param ?callable($this): (self|null) $onFail
190190
*/
191-
public function when($value, callable $callback): self
191+
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): self
192192
{
193-
if ($value instanceof Closure) {
194-
$value = $value($this);
193+
if (!is_bool($condition)) {
194+
$condition = $condition($this);
195195
}
196196

197-
if (!$value) {
198-
return $this;
199-
}
200-
201-
return $callback($this, $value) ?? $this;
197+
return match (true) {
198+
$condition => $onSuccess($this),
199+
null !== $onFail => $onFail($this),
200+
default => $this,
201+
} ?? $this;
202202
}
203203

204204
/**

src/Item.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55
namespace Bakame\Http\StructuredFields;
66

77
use Bakame\Http\StructuredFields\Validation\Violation;
8-
use Closure;
98
use DateTimeImmutable;
109
use DateTimeInterface;
1110
use DateTimeZone;
@@ -368,22 +367,23 @@ public function equals(mixed $other): bool
368367
}
369368

370369
/**
371-
* @template TWhenParameter
370+
* Apply the callback if the given "condition" is (or resolves to) true.
372371
*
373-
* @param (Closure($this): TWhenParameter)|TWhenParameter $value
374-
* @param callable($this, TWhenParameter): ($this|null) $callback
372+
* @param (callable($this): bool)|bool $condition
373+
* @param callable($this): (self|null) $onSuccess
374+
* @param ?callable($this): (self|null) $onFail
375375
*/
376-
public function when($value, callable $callback): self
376+
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): self
377377
{
378-
if ($value instanceof Closure) {
379-
$value = $value($this);
378+
if (!is_bool($condition)) {
379+
$condition = $condition($this);
380380
}
381381

382-
if (!$value) {
383-
return $this;
384-
}
385-
386-
return $callback($this, $value) ?? $this;
382+
return match (true) {
383+
$condition => $onSuccess($this),
384+
null !== $onFail => $onFail($this),
385+
default => $this,
386+
} ?? $this;
387387
}
388388

389389
/**

src/OuterList.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
use ArrayAccess;
88
use Bakame\Http\StructuredFields\Validation\Violation;
9-
use Closure;
109
use Countable;
1110
use DateTimeInterface;
1211
use Iterator;
@@ -189,22 +188,23 @@ public function equals(mixed $other): bool
189188
}
190189

191190
/**
192-
* @template TWhenParameter
191+
* Apply the callback if the given "condition" is (or resolves to) true.
193192
*
194-
* @param (Closure($this): TWhenParameter)|TWhenParameter $value
195-
* @param callable($this, TWhenParameter): ($this|null) $callback
193+
* @param (callable($this): bool)|bool $condition
194+
* @param callable($this): (self|null) $onSuccess
195+
* @param ?callable($this): (self|null) $onFail
196196
*/
197-
public function when($value, callable $callback): self
197+
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): self
198198
{
199-
if ($value instanceof Closure) {
200-
$value = $value($this);
199+
if (!is_bool($condition)) {
200+
$condition = $condition($this);
201201
}
202202

203-
if (!$value) {
204-
return $this;
205-
}
206-
207-
return $callback($this, $value) ?? $this;
203+
return match (true) {
204+
$condition => $onSuccess($this),
205+
null !== $onFail => $onFail($this),
206+
default => $this,
207+
} ?? $this;
208208
}
209209

210210
public function getIterator(): Iterator

src/Parameters.php

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use ArrayAccess;
88
use Bakame\Http\StructuredFields\Validation\Violation;
99
use CallbackFilterIterator;
10-
use Closure;
1110
use Countable;
1211
use DateTimeImmutable;
1312
use DateTimeInterface;
@@ -182,22 +181,23 @@ public function equals(mixed $other): bool
182181
}
183182

184183
/**
185-
* @template TWhenParameter
184+
* Apply the callback if the given "condition" is (or resolves to) true.
186185
*
187-
* @param (Closure($this): TWhenParameter)|TWhenParameter $value
188-
* @param callable($this, TWhenParameter): ($this|null) $callback
186+
* @param (callable($this): bool)|bool $condition
187+
* @param callable($this): (self|null) $onSuccess
188+
* @param ?callable($this): (self|null) $onFail
189189
*/
190-
public function when($value, callable $callback): self
190+
public function when(callable|bool $condition, callable $onSuccess, ?callable $onFail = null): self
191191
{
192-
if ($value instanceof Closure) {
193-
$value = $value($this);
192+
if (!is_bool($condition)) {
193+
$condition = $condition($this);
194194
}
195195

196-
if (!$value) {
197-
return $this;
198-
}
199-
200-
return $callback($this, $value) ?? $this;
196+
return match (true) {
197+
$condition => $onSuccess($this),
198+
null !== $onFail => $onFail($this),
199+
default => $this,
200+
} ?? $this;
201201
}
202202

203203
public function count(): int

0 commit comments

Comments
 (0)