1 | <?php |
||
25 | class ModelFilter extends Filter |
||
26 | { |
||
27 | /** |
||
28 | * @param string $alias |
||
29 | * @param string $field |
||
30 | * @param mixed $data |
||
31 | */ |
||
32 | public function filter(ProxyQueryInterface $queryBuilder, $alias, $field, $data): void |
||
33 | { |
||
34 | if (!$data || !\is_array($data) || !\array_key_exists('value', $data)) { |
||
35 | return; |
||
36 | } |
||
37 | |||
38 | if ($data['value'] instanceof Collection) { |
||
39 | $data['value'] = $data['value']->toArray(); |
||
40 | } |
||
41 | |||
42 | $field = $this->getIdentifierField($field); |
||
43 | |||
44 | if (\is_array($data['value'])) { |
||
45 | $this->handleMultiple($queryBuilder, $alias, $field, $data); |
||
46 | } else { |
||
47 | $this->handleScalar($queryBuilder, $alias, $field, $data); |
||
48 | } |
||
49 | } |
||
50 | |||
51 | public function getDefaultOptions() |
||
52 | { |
||
53 | return [ |
||
54 | 'mapping_type' => false, |
||
55 | 'field_name' => false, |
||
56 | 'field_type' => DocumentType::class, |
||
57 | 'field_options' => [], |
||
58 | 'operator_type' => EqualOperatorType::class, |
||
59 | 'operator_options' => [], |
||
60 | ]; |
||
61 | } |
||
62 | |||
63 | public function getRenderSettings() |
||
64 | { |
||
65 | return [DefaultType::class, [ |
||
66 | 'field_type' => $this->getFieldType(), |
||
67 | 'field_options' => $this->getFieldOptions(), |
||
68 | 'operator_type' => $this->getOption('operator_type'), |
||
69 | 'operator_options' => $this->getOption('operator_options'), |
||
70 | 'label' => $this->getLabel(), |
||
71 | ]]; |
||
72 | } |
||
73 | |||
74 | /** |
||
75 | * @param string $alias |
||
76 | * @param string $field |
||
77 | * @param array $data |
||
78 | * |
||
79 | * @return void |
||
80 | */ |
||
81 | protected function handleMultiple(ProxyQueryInterface $queryBuilder, $alias, $field, $data) |
||
|
|||
82 | { |
||
83 | if (0 === \count($data['value'])) { |
||
84 | return; |
||
85 | } |
||
86 | |||
87 | $ids = []; |
||
88 | foreach ($data['value'] as $value) { |
||
89 | $ids[] = self::fixIdentifier($value->getId()); |
||
90 | } |
||
91 | |||
92 | if (isset($data['type']) && EqualOperatorType::TYPE_NOT_EQUAL === $data['type']) { |
||
93 | $queryBuilder->field($field)->notIn($ids); |
||
94 | } else { |
||
95 | $queryBuilder->field($field)->in($ids); |
||
96 | } |
||
97 | |||
98 | $this->active = true; |
||
99 | } |
||
100 | |||
101 | /** |
||
102 | * @param string $alias |
||
103 | * @param string $field |
||
104 | * @param array $data |
||
105 | * |
||
106 | * @return void |
||
107 | */ |
||
108 | protected function handleScalar(ProxyQueryInterface $queryBuilder, $alias, $field, $data) |
||
124 | |||
125 | /** |
||
126 | * Return \MongoId|ObjectId if $id is MongoId|ObjectId in string representation, otherwise custom string. |
||
127 | * |
||
128 | * @param mixed $id |
||
129 | * |
||
130 | * @return \MongoId|string|ObjectId |
||
131 | */ |
||
132 | protected static function fixIdentifier($id) |
||
149 | |||
150 | /** |
||
151 | * Get identifier field name based on mapping type. |
||
152 | * |
||
153 | * @param string $field |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | protected function getIdentifierField($field) |
||
175 | } |
||
176 |
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.