Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
69.23% |
9 / 13 |
|
75.00% |
3 / 4 |
CRAP | |
0.00% |
0 / 1 |
| QueryCompiler | |
69.23% |
9 / 13 |
|
75.00% |
3 / 4 |
9.86 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| compile | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
| build | |
100.00% |
6 / 6 |
|
100.00% |
1 / 1 |
3 | |||
| translateKeyword | |
0.00% |
0 / 4 |
|
0.00% |
0 / 1 |
12 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Cosray\Finder; |
| 6 | |
| 7 | use Cosray\Context; |
| 8 | |
| 9 | final class QueryCompiler |
| 10 | { |
| 11 | use CompilesField; |
| 12 | |
| 13 | public function __construct( |
| 14 | private readonly Context $context, |
| 15 | private readonly array $builtins, |
| 16 | ) {} |
| 17 | |
| 18 | public function compile(string $query): string |
| 19 | { |
| 20 | $parser = new QueryParser($this->context, $this->builtins); |
| 21 | |
| 22 | return $this->build($parser->parse($query)); |
| 23 | } |
| 24 | |
| 25 | private function build(array $parserOutput): string |
| 26 | { |
| 27 | if (count($parserOutput) === 0) { |
| 28 | return ''; |
| 29 | } |
| 30 | |
| 31 | $clause = ''; |
| 32 | |
| 33 | foreach ($parserOutput as $output) { |
| 34 | $clause .= $output->get(); |
| 35 | } |
| 36 | |
| 37 | return $clause; |
| 38 | } |
| 39 | |
| 40 | private function translateKeyword(string $keyword): string |
| 41 | { |
| 42 | return match ($keyword) { |
| 43 | 'now' => 'NOW()', |
| 44 | 'fulltext' => 'tsv websearch_to_tsquery', |
| 45 | }; |
| 46 | } |
| 47 | } |