|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
namespace GraphQLTests\Doctrine\Blog\Filtering; |
|
6
|
|
|
|
|
7
|
|
|
use Doctrine\ORM\Mapping\ClassMetadata; |
|
8
|
|
|
use Doctrine\ORM\QueryBuilder; |
|
9
|
|
|
use GraphQL\Doctrine\Definition\Operator\AbstractOperator; |
|
10
|
|
|
use GraphQL\Doctrine\Factory\UniqueNameFactory; |
|
11
|
|
|
use GraphQL\Doctrine\Types; |
|
12
|
|
|
use GraphQL\Type\Definition\LeafType; |
|
13
|
|
|
use GraphQL\Type\Definition\Type; |
|
14
|
|
|
|
|
15
|
|
|
class Search extends AbstractOperator |
|
16
|
|
|
{ |
|
17
|
|
|
protected function getConfiguration(Types $types, LeafType $leafType): array |
|
18
|
|
|
{ |
|
19
|
|
|
return [ |
|
20
|
|
|
'fields' => [ |
|
21
|
|
|
[ |
|
22
|
|
|
'name' => 'term', |
|
23
|
|
|
'type' => Type::nonNull($leafType), |
|
24
|
|
|
], |
|
25
|
|
|
], |
|
26
|
|
|
]; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public function getDqlCondition(UniqueNameFactory $uniqueNameFactory, ClassMetadata $metadata, QueryBuilder $queryBuilder, string $alias, string $field, array $args): string |
|
30
|
|
|
{ |
|
31
|
|
|
$search = $args['term']; |
|
32
|
|
|
|
|
33
|
|
|
$fields = []; |
|
34
|
|
|
|
|
35
|
|
|
// Find all textual fields for the entity |
|
36
|
|
|
$textType = ['string', 'text']; |
|
37
|
|
|
foreach ($metadata->fieldMappings as $g) { |
|
38
|
|
|
if (in_array($g['type'], $textType, true)) { |
|
39
|
|
|
$fields[] = $alias . '.' . $g['name']; |
|
40
|
|
|
} |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
// Build the WHERE clause |
|
44
|
|
|
$wordWheres = []; |
|
45
|
|
|
foreach (preg_split('/[[:space:]]+/', $search, -1, PREG_SPLIT_NO_EMPTY) as $i => $word) { |
|
46
|
|
|
$parameterName = $uniqueNameFactory->createParameterName(); |
|
47
|
|
|
|
|
48
|
|
|
$fieldWheres = []; |
|
49
|
|
|
foreach ($fields as $field) { |
|
50
|
|
|
$fieldWheres[] = $field . ' LIKE :' . $parameterName; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
if ($fieldWheres) { |
|
54
|
|
|
$wordWheres[] = '(' . implode(' OR ', $fieldWheres) . ')'; |
|
55
|
|
|
$queryBuilder->setParameter($parameterName, '%' . $word . '%'); |
|
56
|
|
|
} |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
|
|
return '(' . implode(' AND ', $wordWheres) . ')'; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|