This repository has been archived by the owner on Jul 22, 2022. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 93
/
StringFilter.php
92 lines (78 loc) · 2.69 KB
/
StringFilter.php
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
<?php
declare(strict_types=1);
/*
* This file is part of the Sonata Project package.
*
* (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Sonata\DoctrinePHPCRAdminBundle\Filter;
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface;
use Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType;
class StringFilter extends Filter
{
/**
* {@inheritdoc}
*/
public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data)
{
if (!$data || !\is_array($data) || !\array_key_exists('value', $data) || null === $data['value']) {
return;
}
$value = trim((string) $data['value']);
$data['type'] = empty($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type'];
if (0 === \strlen($value)) {
return;
}
$where = $this->getWhere($proxyQuery);
$isComparisonLowerCase = $this->getOption('compare_case_insensitive');
$value = $isComparisonLowerCase ? strtolower($value) : $value;
switch ($data['type']) {
case ChoiceType::TYPE_EQUAL:
if ($isComparisonLowerCase) {
$where->eq()->lowerCase()->field('a.'.$field)->end()->literal($value);
} else {
$where->eq()->field('a.'.$field)->literal($value);
}
break;
case ChoiceType::TYPE_NOT_CONTAINS:
$where->fullTextSearch('a.'.$field, '* -'.$value);
break;
case ChoiceType::TYPE_CONTAINS:
if ($isComparisonLowerCase) {
$where->like()->lowerCase()->field('a.'.$field)->end()->literal('%'.$value.'%');
} else {
$where->like()->field('a.'.$field)->literal('%'.$value.'%');
}
break;
case ChoiceType::TYPE_CONTAINS_WORDS:
default:
$where->fullTextSearch('a.'.$field, $value);
}
// filter is active as we have now modified the query
$this->active = true;
}
/**
* {@inheritdoc}
*/
public function getDefaultOptions()
{
return [
'format' => '%%%s%%',
'compare_lower_case' => false,
];
}
/**
* {@inheritdoc}
*/
public function getRenderSettings()
{
return ['Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType', [
'field_type' => $this->getFieldType(),
'field_options' => $this->getFieldOptions(),
'label' => $this->getLabel(),
]];
}
}