Skip to content
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
8 changes: 6 additions & 2 deletions src/FilterBy.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@

class FilterBy
{
public static function escapeString(string $value): string
public static function escape(string|int|float|bool $value): string
{
return '`' . str_replace('`', '\\`', $value) . '`';
return match (true) {
is_string($value) => '`' . str_replace('`', '\\`', $value) . '`',
is_bool($value) => $value ? 'true' : 'false',
default => (string) $value,
};
}
}
32 changes: 30 additions & 2 deletions tests/Feature/FilterByTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public function testEscapesSpecialCharactersByWrappingInBackticks(): void
{
$rawFilterValue = "The 17\" O'Conner && O`Series \n OR a || 1%2 book? (draft), [alpha]";

$escapedFilterValue = FilterBy::escapeString($rawFilterValue);
$escapedFilterValue = FilterBy::escape($rawFilterValue);

$this->assertSame(
"`The 17\" O'Conner && O\\`Series \n OR a || 1%2 book? (draft), [alpha]`",
Expand All @@ -21,8 +21,36 @@ public function testEscapesSpecialCharactersByWrappingInBackticks(): void

public function testEscapesMultipleBackticksWithinAFilterString(): void
{
$escapedFilterValue = FilterBy::escapeString('`left` and `right`');
$escapedFilterValue = FilterBy::escape('`left` and `right`');

$this->assertSame('`\\`left\\` and \\`right\\``', $escapedFilterValue);
}

public function testEscapeBooleanTrue(): void
{
$escapedFilterValue = FilterBy::escape(true);

$this->assertSame('true', $escapedFilterValue);
}

public function testEscapeBooleanFalse(): void
{
$escapedFilterValue = FilterBy::escape(false);

$this->assertSame('false', $escapedFilterValue);
}

public function testEscapeFloat(): void
{
$escapedFilterValue = FilterBy::escape(1.12);

$this->assertSame('1.12', $escapedFilterValue);
}

public function testEscapeInt(): void
{
$escapedFilterValue = FilterBy::escape(1);

$this->assertSame('1', $escapedFilterValue);
}
}