Skip to content

feature: selectPrefix #306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Mar 26, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 20 additions & 1 deletion src/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

use ArrayAccess;
use Countable;
use Gt\Input\Trigger\NeverTrigger;
use Gt\Json\JsonDecodeException;
use Gt\Json\JsonObject;
use Gt\Json\JsonObjectBuilder;
Expand Down Expand Up @@ -243,14 +244,32 @@ public function select(string...$keys):Trigger {
}
}

return $this->newTrigger("with", ...$keys);
return $this->newTrigger("select", ...$keys);
}

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

/** @SuppressWarnings(PHPMD.UnusedLocalVariable) */
public function selectPrefix(string $prefix):Trigger {
$keys = [];

foreach($this->parameters as $key => $param) {
if(str_starts_with($key, $prefix)) {
array_push($keys, $key);
}
}

if($keys) {
return $this->when(...$keys);
}
else {
return new NeverTrigger($this);
}
}

/**
* Return a Trigger that will pass all keys apart from the provided
* keys to its callback.
Expand Down
18 changes: 18 additions & 0 deletions src/Trigger/NeverTrigger.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php
namespace Gt\Input\Trigger;

class NeverTrigger extends Trigger {
// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
public function call(callable $callback, string ...$args):Trigger {
return $this;
}

// phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter
public function orCall(callable $callback, string ...$args):Trigger {
return $this;
}

public function fire():bool {
return false;
}
}
24 changes: 24 additions & 0 deletions test/phpunit/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -710,6 +710,30 @@ public function testGetBodyJson_withQueryString():void {
self::assertSame(123, $sut->getInt("id"));
}

public function testSelectPrefix_noMatch():void {
$sut = new Input(["id" => 123]);
$trigger = $sut->selectPrefix("io_");

$triggered = false;
$trigger->call(function(...$args)use(&$triggered) {
$triggered = true;
});
$trigger->fire();
self::assertFalse($triggered);
}

public function testSelectPrefix_match():void {
$sut = new Input(["id" => 123, "io_database" => "connect"]);
$trigger = $sut->selectPrefix("io_");

$triggered = false;
$trigger->call(function(...$args)use(&$triggered) {
$triggered = true;
});
$trigger->fire();
self::assertTrue($triggered);
}

public static function dataRandomGetPost():array {
$data = [];

Expand Down