Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
CRAP | |
100.00% |
1 / 1 |
| FulltextSearchFilter | |
100.00% |
23 / 23 |
|
100.00% |
4 / 4 |
7 | |
100.00% |
1 / 1 |
| __construct | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| createJoinQuery | |
100.00% |
20 / 20 |
|
100.00% |
1 / 1 |
4 | |||
| getWhereCondition | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| getOrderByCode | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
| 1 | <?php |
| 2 | namespace Apie\DoctrineEntityDatalayer\Query; |
| 3 | |
| 4 | use Apie\Core\BoundedContext\BoundedContextId; |
| 5 | use Apie\Core\Datalayers\Search\QuerySearch; |
| 6 | use Apie\Core\Entities\EntityInterface; |
| 7 | use Apie\Core\IdentifierUtils; |
| 8 | use Apie\CountWords\WordCounter; |
| 9 | use Apie\DoctrineEntityDatalayer\Enums\SortingOrder; |
| 10 | use Apie\DoctrineEntityDatalayer\LikeUtils; |
| 11 | use Doctrine\DBAL\Connection; |
| 12 | use ReflectionClass; |
| 13 | |
| 14 | final class FulltextSearchFilter implements TextSearchFilterInterface, AddsJoinFilterInterface |
| 15 | { |
| 16 | /** |
| 17 | * @param ReflectionClass<EntityInterface> $entityClass |
| 18 | */ |
| 19 | public function __construct( |
| 20 | private readonly ReflectionClass $entityClass, |
| 21 | private readonly BoundedContextId $boundedContextId |
| 22 | ) { |
| 23 | } |
| 24 | public function createJoinQuery(QuerySearch $querySearch, Connection $connection): string |
| 25 | { |
| 26 | $textSearch = $querySearch->getTextSearch(); |
| 27 | assert(null !== $textSearch); |
| 28 | $words = array_keys(WordCounter::countFromString($textSearch)); |
| 29 | $lastWord = end($words); |
| 30 | $whereStatement = array_map( |
| 31 | function (string $word) use ($connection, $lastWord) { |
| 32 | if ($word === $lastWord) { |
| 33 | return 'text LIKE ' . $connection->quote(LikeUtils::toLikeString($word)); |
| 34 | } |
| 35 | return 'text = ' . $connection->quote($word); |
| 36 | }, |
| 37 | $words |
| 38 | ); |
| 39 | |
| 40 | return sprintf( |
| 41 | '%sJOIN ( |
| 42 | SELECT ref_apie_resource__%s_%s_id AS entity_id, SUM(idf * tf) AS accuracy |
| 43 | FROM apie_index_table |
| 44 | WHERE %s |
| 45 | GROUP BY entity_id |
| 46 | ) subquery ON entity.id = subquery.entity_id', |
| 47 | empty($words) ? 'LEFT ' : '', |
| 48 | $this->boundedContextId, |
| 49 | IdentifierUtils::classNameToUnderscore($this->entityClass), |
| 50 | empty($words) ? '1' : implode(' OR ', $whereStatement) |
| 51 | ); |
| 52 | } |
| 53 | |
| 54 | public function getWhereCondition(QuerySearch $querySearch, Connection $connection): string |
| 55 | { |
| 56 | return '1'; |
| 57 | } |
| 58 | |
| 59 | public function getOrderByCode(SortingOrder $sortingOrder): string |
| 60 | { |
| 61 | return 'MAX(subquery.accuracy) ' . $sortingOrder->value; |
| 62 | } |
| 63 | } |