File tree 3 files changed +62
-1
lines changed
3 files changed +62
-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,32 @@ 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
+ /** @SuppressWarnings(PHPMD.UnusedLocalVariable) */
256
+ public function selectPrefix (string $ prefix ):Trigger {
257
+ $ keys = [];
258
+
259
+ foreach ($ this ->parameters as $ key => $ param ) {
260
+ if (str_starts_with ($ key , $ prefix )) {
261
+ array_push ($ keys , $ key );
262
+ }
263
+ }
264
+
265
+ if ($ keys ) {
266
+ return $ this ->when (...$ keys );
267
+ }
268
+ else {
269
+ return new NeverTrigger ($ this );
270
+ }
271
+ }
272
+
254
273
/**
255
274
* Return a Trigger that will pass all keys apart from the provided
256
275
* 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
+ // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
6
+ public function call (callable $ callback , string ...$ args ):Trigger {
7
+ return $ this ;
8
+ }
9
+
10
+ // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
11
+ public function orCall (callable $ callback , string ...$ args ):Trigger {
12
+ return $ this ;
13
+ }
14
+
15
+ public function fire ():bool {
16
+ return false ;
17
+ }
18
+ }
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