|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace ControleOnline\Filter; |
|
4
|
|
|
|
|
5
|
|
|
use ApiPlatform\Doctrine\Orm\Filter\AbstractFilter; |
|
|
|
|
|
|
6
|
|
|
use ApiPlatform\Doctrine\Orm\Util\QueryNameGeneratorInterface; |
|
|
|
|
|
|
7
|
|
|
use ApiPlatform\Metadata\Operation; |
|
|
|
|
|
|
8
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
|
|
|
|
|
9
|
|
|
|
|
10
|
|
|
class CustomOrFilter extends AbstractFilter |
|
11
|
|
|
{ |
|
12
|
|
|
public function getDescription(string $resourceClass): array |
|
13
|
|
|
{ |
|
14
|
|
|
return [ |
|
15
|
|
|
'search' => [ |
|
16
|
|
|
'property' => null, |
|
17
|
|
|
'type' => 'string', |
|
18
|
|
|
'required' => false, |
|
19
|
|
|
'openapi' => [ |
|
20
|
|
|
'description' => 'Search across multiple fields', |
|
21
|
|
|
], |
|
22
|
|
|
] |
|
23
|
|
|
]; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
protected function filterProperty(string $property, $value, QueryBuilder $queryBuilder, QueryNameGeneratorInterface $queryNameGenerator, string $resourceClass, ?Operation $operation = null, array $context = []): void |
|
27
|
|
|
{ |
|
28
|
|
|
if ($property !== 'search') { |
|
29
|
|
|
return; |
|
30
|
|
|
} |
|
31
|
|
|
|
|
32
|
|
|
$alias = $queryBuilder->getRootAliases()[0]; |
|
33
|
|
|
$andWhere = ''; |
|
34
|
|
|
$relations = []; |
|
35
|
|
|
|
|
36
|
|
|
foreach ($this->properties as $property => $propVal) { |
|
37
|
|
|
$relation = explode('.', $property); |
|
38
|
|
|
|
|
39
|
|
|
if (count($relation) > 1) { |
|
40
|
|
|
if (!array_key_exists($relation[0], $relations)) { |
|
41
|
|
|
$relations[$relation[0]] = uniqid(); |
|
42
|
|
|
$queryBuilder->leftJoin(sprintf('%s.%s', $alias, $relation[0]), 'i'); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
$queryBuilder->orWhere(sprintf('%s.%s LIKE :search', 'i', $relation[1])); |
|
46
|
|
|
$queryBuilder->setParameter('search', '%' . $value . '%'); |
|
47
|
|
|
continue; |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
$andWhere .= sprintf('%s.%s LIKE :search', $alias, $property); |
|
51
|
|
|
|
|
52
|
|
|
next($this->properties); |
|
53
|
|
|
$nextKey = key($this->properties); |
|
54
|
|
|
|
|
55
|
|
|
if ($nextKey !== null && !strpos($nextKey, '.') !== false) { |
|
56
|
|
|
$andWhere .= ' OR '; |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
|
|
|
|
61
|
|
|
if (empty($relations)) { |
|
62
|
|
|
$queryBuilder->andWhere($andWhere); |
|
63
|
|
|
} else { |
|
64
|
|
|
$queryBuilder->orWhere($andWhere); |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
$queryBuilder->setParameter('search', '%' . $value . '%'); |
|
68
|
|
|
} |
|
69
|
|
|
} |
|
70
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths