Skip to content

Commit 5386e51

Browse files
g105bGreg Bowler
and
Greg Bowler
authored
feature: selectPrefix (#306)
* ci: remove old artifacts * ci: run all php versions * feature: implement selectPrefix closes #13 * tweak: ignore unused function parameter * tweak: improve looping over parameters --------- Co-authored-by: Greg Bowler <[email protected]>
1 parent f5096b5 commit 5386e51

File tree

3 files changed

+62
-1
lines changed

3 files changed

+62
-1
lines changed

src/Input.php

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use ArrayAccess;
55
use Countable;
6+
use Gt\Input\Trigger\NeverTrigger;
67
use Gt\Json\JsonDecodeException;
78
use Gt\Json\JsonObject;
89
use Gt\Json\JsonObjectBuilder;
@@ -243,14 +244,32 @@ public function select(string...$keys):Trigger {
243244
}
244245
}
245246

246-
return $this->newTrigger("with", ...$keys);
247+
return $this->newTrigger("select", ...$keys);
247248
}
248249

249250
/** @deprecated Use select() instead to avoid ambiguity with immutable `with` functions */
250251
public function with(string...$keys):Trigger {
251252
return $this->select(...$keys);
252253
}
253254

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+
254273
/**
255274
* Return a Trigger that will pass all keys apart from the provided
256275
* keys to its callback.

src/Trigger/NeverTrigger.php

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}

test/phpunit/InputTest.php

+24
Original file line numberDiff line numberDiff line change
@@ -710,6 +710,30 @@ public function testGetBodyJson_withQueryString():void {
710710
self::assertSame(123, $sut->getInt("id"));
711711
}
712712

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+
713737
public static function dataRandomGetPost():array {
714738
$data = [];
715739

0 commit comments

Comments
 (0)