Skip to content

Commit ab48a48

Browse files
feat: support int, float and bool values for FilterBy escape func
1 parent d16028d commit ab48a48

File tree

2 files changed

+36
-4
lines changed

2 files changed

+36
-4
lines changed

src/FilterBy.php

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,12 @@
44

55
class FilterBy
66
{
7-
public static function escapeString(string $value): string
7+
public static function escape(string|int|float|bool $value): string
88
{
9-
return '`' . str_replace('`', '\\`', $value) . '`';
9+
return match (true) {
10+
is_string($value) => '`' . str_replace('`', '\\`', $value) . '`',
11+
is_bool($value) => $value ? 'true' : 'false',
12+
default => (string) $value,
13+
};
1014
}
1115
}

tests/Feature/FilterByTest.php

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public function testEscapesSpecialCharactersByWrappingInBackticks(): void
1111
{
1212
$rawFilterValue = "The 17\" O'Conner && O`Series \n OR a || 1%2 book? (draft), [alpha]";
1313

14-
$escapedFilterValue = FilterBy::escapeString($rawFilterValue);
14+
$escapedFilterValue = FilterBy::escape($rawFilterValue);
1515

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

2222
public function testEscapesMultipleBackticksWithinAFilterString(): void
2323
{
24-
$escapedFilterValue = FilterBy::escapeString('`left` and `right`');
24+
$escapedFilterValue = FilterBy::escape('`left` and `right`');
2525

2626
$this->assertSame('`\\`left\\` and \\`right\\``', $escapedFilterValue);
2727
}
28+
29+
public function testEscapeBooleanTrue(): void
30+
{
31+
$escapedFilterValue = FilterBy::escape(true);
32+
33+
$this->assertSame('true', $escapedFilterValue);
34+
}
35+
36+
public function testEscapeBooleanFalse(): void
37+
{
38+
$escapedFilterValue = FilterBy::escape(false);
39+
40+
$this->assertSame('false', $escapedFilterValue);
41+
}
42+
43+
public function testEscapeFloat(): void
44+
{
45+
$escapedFilterValue = FilterBy::escape(1.12);
46+
47+
$this->assertSame('1.12', $escapedFilterValue);
48+
}
49+
50+
public function testEscapeInt(): void
51+
{
52+
$escapedFilterValue = FilterBy::escape(1);
53+
54+
$this->assertSame('1', $escapedFilterValue);
55+
}
2856
}

0 commit comments

Comments
 (0)