-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Search bar: Highlighting the search query
- Loading branch information
Showing
5 changed files
with
111 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\Services; | ||
|
||
readonly final class SearchHighlighter | ||
{ | ||
public function highlight(string|int|null $text, string $query): string | ||
{ | ||
$text = trim((string) $text); | ||
|
||
if ($text === '') { | ||
return ''; | ||
} | ||
|
||
$words = array_filter(explode(' ', trim($query))); | ||
|
||
if ($words === []) { | ||
return $text; | ||
} | ||
|
||
$escapedWords = array_map( | ||
callback: fn (string $word): string => preg_quote($word, '/'), | ||
array: $words | ||
); | ||
|
||
$pattern = '/' . implode('|', $escapedWords) . '/i'; | ||
|
||
$highlightedText = preg_replace_callback( | ||
pattern: $pattern, | ||
callback: fn (array $matches): string => '<span class="search-highlight">' . htmlspecialchars($matches[0], ENT_QUOTES, 'UTF-8') . '</span>', | ||
subject: $text, | ||
); | ||
|
||
return $highlightedText ?? $text; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\Twig; | ||
|
||
use SpeedPuzzling\Web\Services\SearchHighlighter; | ||
use Twig\Extension\AbstractExtension; | ||
use Twig\TwigFunction; | ||
|
||
final class SearchHighlightTwigExtension extends AbstractExtension | ||
{ | ||
public function __construct( | ||
readonly private SearchHighlighter $highlighter, | ||
) { | ||
} | ||
|
||
/** | ||
* @return array<TwigFunction> | ||
*/ | ||
public function getFunctions(): array | ||
{ | ||
return [ | ||
new TwigFunction('highlight', $this->highlighter->highlight(...), ['is_safe' => ['html']]), | ||
]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
<?php | ||
|
||
declare(strict_types=1); | ||
|
||
namespace SpeedPuzzling\Web\Tests\Services; | ||
|
||
use PHPUnit\Framework\Attributes\DataProvider; | ||
use SpeedPuzzling\Web\Services\SearchHighlighter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class SearchHighlighterTest extends TestCase | ||
{ | ||
#[DataProvider('provideTestHighlightData')] | ||
public function testHighlight(null|int|string $text, string $search, string $expected): void | ||
{ | ||
$highlighter = new SearchHighlighter(); | ||
|
||
$this->assertSame($expected, $highlighter->highlight($text, $search)); | ||
} | ||
|
||
/** | ||
* @return \Generator<array{null|int|string, string, string}> | ||
*/ | ||
public static function provideTestHighlightData(): \Generator | ||
{ | ||
yield ['', '', '']; | ||
yield [null, 'are', '']; | ||
yield [1234, 'are', '1234']; | ||
yield [1234, '23', '1<span class="search-highlight">23</span>4']; | ||
yield ['Karen', 'are', 'K<span class="search-highlight">are</span>n']; | ||
yield ['KAREN', 'are', 'K<span class="search-highlight">ARE</span>N']; | ||
yield ['karen', 'ARE', 'k<span class="search-highlight">are</span>n']; | ||
yield ['Karen Puzzle', 'kar puz', '<span class="search-highlight">Kar</span>en <span class="search-highlight">Puz</span>zle']; | ||
yield ['KAREN PUZZLE', 'kar puz', '<span class="search-highlight">KAR</span>EN <span class="search-highlight">PUZ</span>ZLE']; | ||
yield ['karen puzzle', 'kAR pUZ', '<span class="search-highlight">kar</span>en <span class="search-highlight">puz</span>zle']; | ||
} | ||
} |