These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more
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\DoctrineMongoDBAdminBundle\Filter; |
||
15 | |||
16 | use Doctrine\Bundle\MongoDBBundle\Form\Type\DocumentType; |
||
17 | use Doctrine\Common\Collections\Collection; |
||
18 | use Sonata\AdminBundle\Datagrid\ProxyQueryInterface; |
||
19 | use Sonata\AdminBundle\Form\Type\Filter\DefaultType; |
||
20 | use Sonata\CoreBundle\Form\Type\EqualType; |
||
21 | |||
22 | class ModelFilter extends Filter |
||
23 | { |
||
24 | /** |
||
25 | * @param ProxyQueryInterface $queryBuilder |
||
26 | * @param string $alias |
||
27 | * @param string $field |
||
28 | * @param mixed $data |
||
29 | */ |
||
30 | public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void |
||
31 | { |
||
32 | if (!$data || !\is_array($data) || !array_key_exists('value', $data)) { |
||
33 | return; |
||
34 | } |
||
35 | |||
36 | if ($data['value'] instanceof Collection) { |
||
37 | $data['value'] = $data['value']->toArray(); |
||
38 | } |
||
39 | |||
40 | $field = $this->getIdentifierField($field); |
||
41 | |||
42 | if (\is_array($data['value'])) { |
||
43 | $this->handleMultiple($queryBuilder, $alias, $field, $data); |
||
44 | } else { |
||
45 | $this->handleScalar($queryBuilder, $alias, $field, $data); |
||
46 | } |
||
47 | } |
||
48 | |||
49 | public function getDefaultOptions() |
||
50 | { |
||
51 | return [ |
||
52 | 'mapping_type' => false, |
||
53 | 'field_name' => false, |
||
54 | 'field_type' => DocumentType::class, |
||
55 | 'field_options' => [], |
||
56 | 'operator_type' => EqualType::class, |
||
57 | 'operator_options' => [], |
||
58 | ]; |
||
59 | } |
||
60 | |||
61 | public function getRenderSettings() |
||
62 | { |
||
63 | return [DefaultType::class, [ |
||
64 | 'field_type' => $this->getFieldType(), |
||
65 | 'field_options' => $this->getFieldOptions(), |
||
66 | 'operator_type' => $this->getOption('operator_type'), |
||
67 | 'operator_options' => $this->getOption('operator_options'), |
||
68 | 'label' => $this->getLabel(), |
||
69 | ]]; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param ProxyQueryInterface $queryBuilder |
||
74 | * @param type $alias |
||
75 | * @param type $field |
||
76 | * @param type $data |
||
77 | * |
||
78 | * @return type |
||
79 | */ |
||
80 | protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $field, $data) |
||
0 ignored issues
–
show
|
|||
81 | { |
||
82 | if (0 == \count($data['value'])) { |
||
83 | return; |
||
84 | } |
||
85 | |||
86 | $ids = []; |
||
87 | foreach ($data['value'] as $value) { |
||
88 | $ids[] = self::fixIdentifier($value->getId()); |
||
89 | } |
||
90 | |||
91 | if (isset($data['type']) && EqualType::TYPE_IS_NOT_EQUAL == $data['type']) { |
||
92 | $queryBuilder->field($field)->notIn($ids); |
||
93 | } else { |
||
94 | $queryBuilder->field($field)->in($ids); |
||
95 | } |
||
96 | |||
97 | $this->active = true; |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * @param ProxyQueryInterface $queryBuilder |
||
102 | * @param type $alias |
||
103 | * @param type $field |
||
104 | * @param type $data |
||
105 | * |
||
106 | * @return type |
||
107 | */ |
||
108 | protected function handleScalar(ProxyQueryInterface $queryBuilder, $alias, $field, $data) |
||
0 ignored issues
–
show
|
|||
109 | { |
||
110 | if (empty($data['value'])) { |
||
111 | return; |
||
112 | } |
||
113 | |||
114 | $id = self::fixIdentifier($data['value']->getId()); |
||
115 | |||
116 | if (isset($data['type']) && EqualType::TYPE_IS_NOT_EQUAL == $data['type']) { |
||
117 | $queryBuilder->field($field)->notEqual($id); |
||
118 | } else { |
||
119 | $queryBuilder->field($field)->equals($id); |
||
120 | } |
||
121 | |||
122 | $this->active = true; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Return \MongoId if $id is MongoId in string representation, otherwise custom string. |
||
127 | * |
||
128 | * @param mixed $id |
||
129 | * |
||
130 | * @return \MongoId|string |
||
131 | */ |
||
132 | protected static function fixIdentifier($id) |
||
133 | { |
||
134 | try { |
||
135 | return new \MongoId($id); |
||
136 | } catch (\MongoException $ex) { |
||
137 | return $id; |
||
138 | } |
||
139 | } |
||
140 | |||
141 | /** |
||
142 | * Identifier field name is 'field' if mapping type is simple; otherwise, it's 'field.$id'. |
||
143 | * |
||
144 | * @param string $field |
||
145 | * |
||
146 | * @return string |
||
147 | */ |
||
148 | protected function getIdentifierField($field) |
||
149 | { |
||
150 | $field_mapping = $this->getFieldMapping(); |
||
151 | |||
152 | return (true === $field_mapping['simple']) ? $field : $field.'.$id'; |
||
153 | } |
||
154 | } |
||
155 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.