|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sonata package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sonata\DoctrinePHPCRAdminBundle\Filter; |
|
13
|
|
|
|
|
14
|
|
|
use Sonata\DoctrinePHPCRAdminBundle\Form\Type\Filter\ChoiceType; |
|
15
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
|
16
|
|
|
|
|
17
|
|
|
class StringFilter extends Filter |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritDoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
public function filter(ProxyQueryInterface $proxyQuery, $alias, $field, $data) |
|
23
|
|
|
{ |
|
24
|
|
|
if (!$data || !is_array($data) || !array_key_exists('value', $data)) { |
|
25
|
|
|
return; |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
$data['value'] = trim($data['value']); |
|
29
|
|
|
$data['type'] = empty($data['type']) ? ChoiceType::TYPE_CONTAINS : $data['type']; |
|
30
|
|
|
|
|
31
|
|
|
if (strlen($data['value']) == 0) { |
|
32
|
|
|
return; |
|
33
|
|
|
} |
|
34
|
|
|
|
|
35
|
|
|
$where = $this->getWhere($proxyQuery); |
|
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
switch ($data['type']) { |
|
38
|
|
|
case ChoiceType::TYPE_EQUAL: |
|
39
|
|
|
$where->eq()->field('a.'.$field)->literal($data['value']); |
|
40
|
|
|
break; |
|
41
|
|
|
case ChoiceType::TYPE_NOT_CONTAINS: |
|
42
|
|
|
$where->fullTextSearch('a.'.$field, '* -'.$data['value']); |
|
43
|
|
|
break; |
|
44
|
|
|
case ChoiceType::TYPE_CONTAINS: |
|
45
|
|
|
$where->like()->field('a.'.$field)->literal('%'.$data['value'].'%'); |
|
46
|
|
|
break; |
|
47
|
|
|
case ChoiceType::TYPE_CONTAINS_WORDS: |
|
48
|
|
|
default: |
|
49
|
|
|
$where->fullTextSearch('a.'.$field, $data['value']); |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
// filter is active as we have now modified the query |
|
53
|
|
|
$this->active = true; |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* {@inheritDoc} |
|
58
|
|
|
*/ |
|
59
|
|
|
public function getDefaultOptions() |
|
60
|
|
|
{ |
|
61
|
|
|
return array( |
|
62
|
|
|
'format' => '%%%s%%' |
|
63
|
|
|
); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* {@inheritDoc} |
|
68
|
|
|
*/ |
|
69
|
|
|
public function getRenderSettings() |
|
70
|
|
|
{ |
|
71
|
|
|
return array('doctrine_phpcr_type_filter_choice', array( |
|
72
|
|
|
'field_type' => $this->getFieldType(), |
|
73
|
|
|
'field_options' => $this->getFieldOptions(), |
|
74
|
|
|
'label' => $this->getLabel() |
|
75
|
|
|
)); |
|
76
|
|
|
} |
|
77
|
|
|
} |
|
78
|
|
|
|
This check looks for parameters that are defined as one type in their type hint or doc comment but seem to be used as a narrower type, i.e an implementation of an interface or a subclass.
Consider changing the type of the parameter or doing an instanceof check before assuming your parameter is of the expected type.