File tree Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Expand file tree Collapse file tree 3 files changed +59
-1
lines changed Original file line number Diff line number Diff line change 3
3
4
4
use ArrayAccess ;
5
5
use Countable ;
6
+ use Gt \Input \Trigger \NeverTrigger ;
6
7
use Gt \Json \JsonDecodeException ;
7
8
use Gt \Json \JsonObject ;
8
9
use Gt \Json \JsonObjectBuilder ;
@@ -243,14 +244,31 @@ public function select(string...$keys):Trigger {
243
244
}
244
245
}
245
246
246
- return $ this ->newTrigger ("with " , ...$ keys );
247
+ return $ this ->newTrigger ("select " , ...$ keys );
247
248
}
248
249
249
250
/** @deprecated Use select() instead to avoid ambiguity with immutable `with` functions */
250
251
public function with (string ...$ keys ):Trigger {
251
252
return $ this ->select (...$ keys );
252
253
}
253
254
255
+ public function selectPrefix (string $ prefix ):Trigger {
256
+ $ keys = [];
257
+
258
+ foreach ($ this ->parameters as $ key => $ param ) {
259
+ if (str_starts_with ($ key , $ prefix )) {
260
+ array_push ($ keys , $ key );
261
+ }
262
+ }
263
+
264
+ if ($ keys ) {
265
+ return $ this ->when (...$ keys );
266
+ }
267
+ else {
268
+ return new NeverTrigger ($ this );
269
+ }
270
+ }
271
+
254
272
/**
255
273
* Return a Trigger that will pass all keys apart from the provided
256
274
* keys to its callback.
Original file line number Diff line number Diff line change
1
+ <?php
2
+ namespace Gt \Input \Trigger ;
3
+
4
+ class NeverTrigger extends Trigger {
5
+ public function call (callable $ callback , string ...$ args ):Trigger {
6
+ return $ this ;
7
+ }
8
+
9
+ public function orCall (callable $ callback , string ...$ args ):Trigger {
10
+ return $ this ;
11
+ }
12
+
13
+ public function fire ():bool {
14
+ return false ;
15
+ }
16
+ }
Original file line number Diff line number Diff line change @@ -710,6 +710,30 @@ public function testGetBodyJson_withQueryString():void {
710
710
self ::assertSame (123 , $ sut ->getInt ("id " ));
711
711
}
712
712
713
+ public function testSelectPrefix_noMatch ():void {
714
+ $ sut = new Input (["id " => 123 ]);
715
+ $ trigger = $ sut ->selectPrefix ("io_ " );
716
+
717
+ $ triggered = false ;
718
+ $ trigger ->call (function (...$ args )use (&$ triggered ) {
719
+ $ triggered = true ;
720
+ });
721
+ $ trigger ->fire ();
722
+ self ::assertFalse ($ triggered );
723
+ }
724
+
725
+ public function testSelectPrefix_match ():void {
726
+ $ sut = new Input (["id " => 123 , "io_database " => "connect " ]);
727
+ $ trigger = $ sut ->selectPrefix ("io_ " );
728
+
729
+ $ triggered = false ;
730
+ $ trigger ->call (function (...$ args )use (&$ triggered ) {
731
+ $ triggered = true ;
732
+ });
733
+ $ trigger ->fire ();
734
+ self ::assertTrue ($ triggered );
735
+ }
736
+
713
737
public static function dataRandomGetPost ():array {
714
738
$ data = [];
715
739
You can’t perform that action at this time.
0 commit comments