-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathSearchFilter.php
More file actions
123 lines (111 loc) · 3.88 KB
/
Copy pathSearchFilter.php
File metadata and controls
123 lines (111 loc) · 3.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
<?php
declare(strict_types=1);
namespace SourceBroker\T3api\Filter;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Parameter;
use GoldSpecDigital\ObjectOrientedOAS\Objects\Schema;
use SourceBroker\T3api\Domain\Model\ApiFilter;
use TYPO3\CMS\Core\Database\ConnectionPool;
use TYPO3\CMS\Core\Utility\GeneralUtility;
use TYPO3\CMS\Extbase\Persistence\Exception\InvalidQueryException;
use TYPO3\CMS\Extbase\Persistence\Generic\Exception\UnexpectedTypeException;
use TYPO3\CMS\Extbase\Persistence\Generic\Mapper\DataMapper;
use TYPO3\CMS\Extbase\Persistence\Generic\Qom\ConstraintInterface;
use TYPO3\CMS\Extbase\Persistence\QueryInterface;
class SearchFilter extends AbstractFilter implements OpenApiSupportingFilterInterface
{
/**
* @return Parameter[]
*/
public static function getOpenApiParameters(ApiFilter $apiFilter): array
{
return [
Parameter::create()
->name($apiFilter->getParameterName())
->schema(Schema::string()),
];
}
/**
* @inheritDoc
* @throws InvalidQueryException
* @throws UnexpectedTypeException
*/
public function filterProperty(
string $property,
$values,
QueryInterface $query,
ApiFilter $apiFilter
): ?ConstraintInterface {
$values = (array)$values;
switch ($apiFilter->getStrategy()->getName()) {
case 'partial':
return $query->logicalOr(
...array_map(
static function ($value) use ($query, $property) {
return $query->like($property, '%' . $value . '%');
},
$values
)
);
case 'matchAgainst':
$ids = $this->matchAgainstFindIds(
$property,
$values,
$query,
$apiFilter
);
return $query->in('uid', $ids + [0]);
case 'exact':
default:
return $query->in($property, $values);
}
}
/**
* @return int[]
* @throws UnexpectedTypeException
*/
protected function matchAgainstFindIds(
string $property,
array $values,
QueryInterface $query,
ApiFilter $apiFilter
): array {
$tableName = $this->getTableName($query);
$conditions = [];
$binds = [];
$rootAlias = 'o';
$queryExpansion = (bool)$apiFilter->getArgument('withQueryExpansion');
$queryBuilder = GeneralUtility::makeInstance(ConnectionPool::class)
->getQueryBuilderForTable($tableName);
if ($this->isPropertyNested($property)) {
[$tableAlias, $propertyName] = $this->addJoinsForNestedProperty(
$property,
$rootAlias,
$query,
$queryBuilder
);
} else {
$tableAlias = $rootAlias;
$propertyName = $property;
}
foreach ($values as $i => $value) {
$key = ':text_ma_' . ((int)$i);
$conditions[] = sprintf(
'MATCH(%s) AGAINST (%s IN NATURAL LANGUAGE MODE %s)',
$queryBuilder->quoteIdentifier(
$tableAlias . '.' . GeneralUtility::makeInstance(DataMapper::class)
->convertPropertyNameToColumnName($propertyName, $apiFilter->getFilterClass())
),
$key,
$queryExpansion ? ' WITH QUERY EXPANSION ' : ''
);
$binds[ltrim($key, ':')] = $value;
}
return $queryBuilder
->select($rootAlias . '.uid')
->from($tableName, $rootAlias)
->andWhere($queryBuilder->expr()->or(...$conditions))
->setParameters($binds)
->executeQuery()
->fetchFirstColumn();
}
}