File tree Expand file tree Collapse file tree 5 files changed +60
-60
lines changed Expand file tree Collapse file tree 5 files changed +60
-60
lines changed Original file line number Diff line number Diff line change 7
7
use ArrayAccess ;
8
8
use Bakame \Http \StructuredFields \Validation \Violation ;
9
9
use CallbackFilterIterator ;
10
- use Closure ;
11
10
use Countable ;
12
11
use DateTimeInterface ;
13
12
use Iterator ;
@@ -241,22 +240,23 @@ public function equals(mixed $other): bool
241
240
}
242
241
243
242
/**
244
- * @template TWhenParameter
243
+ * Apply the callback if the given "condition" is (or resolves to) true.
245
244
*
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
248
248
*/
249
- public function when ($ value , callable $ callback ): self
249
+ public function when (callable | bool $ condition , callable $ onSuccess , ? callable $ onFail = null ): self
250
250
{
251
- if ($ value instanceof Closure ) {
252
- $ value = $ value ($ this );
251
+ if (! is_bool ( $ condition ) ) {
252
+ $ condition = $ condition ($ this );
253
253
}
254
254
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 ;
260
260
}
261
261
262
262
public function count (): int
Original file line number Diff line number Diff line change 6
6
7
7
use ArrayAccess ;
8
8
use Bakame \Http \StructuredFields \Validation \Violation ;
9
- use Closure ;
10
9
use Countable ;
11
10
use DateTimeInterface ;
12
11
use Iterator ;
@@ -183,22 +182,23 @@ public function equals(mixed $other): bool
183
182
}
184
183
185
184
/**
186
- * @template TWhenParameter
185
+ * Apply the callback if the given "condition" is (or resolves to) true.
187
186
*
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
190
190
*/
191
- public function when ($ value , callable $ callback ): self
191
+ public function when (callable | bool $ condition , callable $ onSuccess , ? callable $ onFail = null ): self
192
192
{
193
- if ($ value instanceof Closure ) {
194
- $ value = $ value ($ this );
193
+ if (! is_bool ( $ condition ) ) {
194
+ $ condition = $ condition ($ this );
195
195
}
196
196
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 ;
202
202
}
203
203
204
204
/**
Original file line number Diff line number Diff line change 5
5
namespace Bakame \Http \StructuredFields ;
6
6
7
7
use Bakame \Http \StructuredFields \Validation \Violation ;
8
- use Closure ;
9
8
use DateTimeImmutable ;
10
9
use DateTimeInterface ;
11
10
use DateTimeZone ;
@@ -368,22 +367,23 @@ public function equals(mixed $other): bool
368
367
}
369
368
370
369
/**
371
- * @template TWhenParameter
370
+ * Apply the callback if the given "condition" is (or resolves to) true.
372
371
*
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
375
375
*/
376
- public function when ($ value , callable $ callback ): self
376
+ public function when (callable | bool $ condition , callable $ onSuccess , ? callable $ onFail = null ): self
377
377
{
378
- if ($ value instanceof Closure ) {
379
- $ value = $ value ($ this );
378
+ if (! is_bool ( $ condition ) ) {
379
+ $ condition = $ condition ($ this );
380
380
}
381
381
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 ;
387
387
}
388
388
389
389
/**
Original file line number Diff line number Diff line change 6
6
7
7
use ArrayAccess ;
8
8
use Bakame \Http \StructuredFields \Validation \Violation ;
9
- use Closure ;
10
9
use Countable ;
11
10
use DateTimeInterface ;
12
11
use Iterator ;
@@ -189,22 +188,23 @@ public function equals(mixed $other): bool
189
188
}
190
189
191
190
/**
192
- * @template TWhenParameter
191
+ * Apply the callback if the given "condition" is (or resolves to) true.
193
192
*
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
196
196
*/
197
- public function when ($ value , callable $ callback ): self
197
+ public function when (callable | bool $ condition , callable $ onSuccess , ? callable $ onFail = null ): self
198
198
{
199
- if ($ value instanceof Closure ) {
200
- $ value = $ value ($ this );
199
+ if (! is_bool ( $ condition ) ) {
200
+ $ condition = $ condition ($ this );
201
201
}
202
202
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 ;
208
208
}
209
209
210
210
public function getIterator (): Iterator
Original file line number Diff line number Diff line change 7
7
use ArrayAccess ;
8
8
use Bakame \Http \StructuredFields \Validation \Violation ;
9
9
use CallbackFilterIterator ;
10
- use Closure ;
11
10
use Countable ;
12
11
use DateTimeImmutable ;
13
12
use DateTimeInterface ;
@@ -182,22 +181,23 @@ public function equals(mixed $other): bool
182
181
}
183
182
184
183
/**
185
- * @template TWhenParameter
184
+ * Apply the callback if the given "condition" is (or resolves to) true.
186
185
*
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
189
189
*/
190
- public function when ($ value , callable $ callback ): self
190
+ public function when (callable | bool $ condition , callable $ onSuccess , ? callable $ onFail = null ): self
191
191
{
192
- if ($ value instanceof Closure ) {
193
- $ value = $ value ($ this );
192
+ if (! is_bool ( $ condition ) ) {
193
+ $ condition = $ condition ($ this );
194
194
}
195
195
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 ;
201
201
}
202
202
203
203
public function count (): int
You can’t perform that action at this time.
0 commit comments