|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
declare(strict_types=1); |
|
4
|
|
|
|
|
5
|
|
|
/* |
|
6
|
|
|
* This file is part of the Sonata Project package. |
|
7
|
|
|
* |
|
8
|
|
|
* (c) Thomas Rabaix <[email protected]> |
|
9
|
|
|
* |
|
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
11
|
|
|
* file that was distributed with this source code. |
|
12
|
|
|
*/ |
|
13
|
|
|
|
|
14
|
|
|
namespace Sonata\DoctrineORMAdminBundle\Filter; |
|
15
|
|
|
|
|
16
|
|
|
use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
|
17
|
|
|
use Sonata\AdminBundle\Form\Type\Filter\DefaultType; |
|
18
|
|
|
use Sonata\AdminBundle\Form\Type\Operator\EqualOperatorType; |
|
19
|
|
|
use Symfony\Component\Form\Extension\Core\Type\ChoiceType; |
|
20
|
|
|
|
|
21
|
|
|
class ClassFilter extends Filter |
|
22
|
|
|
{ |
|
23
|
|
|
public const CHOICES = [ |
|
24
|
|
|
EqualOperatorType::TYPE_EQUAL => 'INSTANCE OF', |
|
25
|
|
|
EqualOperatorType::TYPE_NOT_EQUAL => 'NOT INSTANCE OF', |
|
26
|
|
|
]; |
|
27
|
|
|
|
|
28
|
|
|
public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data) |
|
29
|
|
|
{ |
|
30
|
|
|
if (!$data || !\is_array($data) || !\array_key_exists('value', $data)) { |
|
|
|
|
|
|
31
|
|
|
return; |
|
32
|
|
|
} |
|
33
|
|
|
|
|
34
|
|
|
if (0 === \strlen($data['value'])) { |
|
35
|
|
|
return; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
$data['type'] = !isset($data['type']) ? EqualOperatorType::TYPE_EQUAL : $data['type']; |
|
39
|
|
|
|
|
40
|
|
|
$operator = $this->getOperator((int) $data['type']); |
|
41
|
|
|
|
|
42
|
|
|
$this->applyWhere($queryBuilder, sprintf('%s %s %s', $alias, $operator, $data['value'])); |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
public function getDefaultOptions() |
|
46
|
|
|
{ |
|
47
|
|
|
return [ |
|
48
|
|
|
'operator_type' => EqualOperatorType::class, |
|
49
|
|
|
'operator_options' => [], |
|
50
|
|
|
]; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
public function getFieldType() |
|
54
|
|
|
{ |
|
55
|
|
|
return $this->getOption('field_type', ChoiceType::class); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
public function getFieldOptions() |
|
59
|
|
|
{ |
|
60
|
|
|
$choiceOptions = [ |
|
61
|
|
|
'required' => false, |
|
62
|
|
|
'choices' => $this->getOption('sub_classes'), |
|
63
|
|
|
]; |
|
64
|
|
|
|
|
65
|
|
|
return $this->getOption('choices', $choiceOptions); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
public function getRenderSettings() |
|
69
|
|
|
{ |
|
70
|
|
|
return [DefaultType::class, [ |
|
71
|
|
|
'operator_type' => $this->getOption('operator_type'), |
|
72
|
|
|
'operator_options' => $this->getOption('operator_options'), |
|
73
|
|
|
'field_type' => $this->getFieldType(), |
|
74
|
|
|
'field_options' => $this->getFieldOptions(), |
|
75
|
|
|
'label' => $this->getLabel(), |
|
76
|
|
|
]]; |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
private function getOperator(int $type): string |
|
80
|
|
|
{ |
|
81
|
|
|
return self::CHOICES[$type] ?? self::CHOICES[EqualOperatorType::TYPE_EQUAL]; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.