Completed
Pull Request — master (#155)
by Tri
63:20
created

MultiTermChoice::getOptionUrlParameters()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 12
rs 9.4285
cc 2
eloc 7
nc 2
nop 2
1
<?php
2
3
/*
4
 * This file is part of the ONGR package.
5
 *
6
 * (c) NFQ Technologies UAB <[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 ONGR\FilterManagerBundle\Filter\Widget\Choice;
13
14
use ONGR\ElasticsearchDSL\Filter\NotFilter;
15
use ONGR\ElasticsearchDSL\Filter\TermsFilter;
16
use ONGR\ElasticsearchDSL\Search;
17
use ONGR\FilterManagerBundle\Filter\FilterState;
18
use ONGR\FilterManagerBundle\Filter\ViewData;
19
use ONGR\FilterManagerBundle\Search\SearchRequest;
20
use Symfony\Component\HttpFoundation\Request;
21
22
/**
23
 * This class provides multiple terms choice filter.
24
 */
25
class MultiTermChoice extends SingleTermChoice
26
{
27
28
    const OPERATION_OR = 'or';
29
    const OPERATION_AND = 'and';
30
    const OPERATION_NOT_AND = 'not_and';
31
32
    /**
33
     * @var string
34
     */
35
    private $booleanOperation;
36
37
    /**
38
     * @return mixed
39
     */
40
    public function getBooleanOperation()
41
    {
42
        return $this->booleanOperation;
43
    }
44
45
    /**
46
     * @param $operation
47
     */
48
    public function setBooleanOperation($operation)
49
    {
50
        $this->booleanOperation = $operation;
51
    }
52
53
    /**
54
     * {@inheritdoc}
55
     */
56
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
57
    {
58
        if ($state && $state->isActive()) {
59
            $filter = new TermsFilter($this->getField(), $state->getValue());
0 ignored issues
show
Deprecated Code introduced by
The class ONGR\ElasticsearchDSL\Filter\TermsFilter has been deprecated with message: Will be removed in 2.0. Use the TermsQuery instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
60
61
            switch (strtolower($this->getBooleanOperation())) {
62
                case self::OPERATION_AND:
63
                    $filter->setParameters(['execution' => 'and']);
64
                    break;
65
                case self::OPERATION_NOT_AND:
66
                    $filter = new NotFilter($filter);
0 ignored issues
show
Deprecated Code introduced by
The class ONGR\ElasticsearchDSL\Filter\NotFilter has been deprecated with message: Will be removed in 2.0. Use the BoolQuery instead.

This class, trait or interface has been deprecated. The supplier of the file has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the type will be removed from the class and what other constant to use instead.

Loading history...
67
                    break;
68
                case self::OPERATION_OR:
69
                default:
70
                    // Do nothing
71
            }
72
73
            $search->addPostFilter($filter);
74
        }
75
    }
76
77
    /**
78
     * {@inheritdoc}
79
     */
80
    public function getState(Request $request)
81
    {
82
        $value = $request->get($this->getRequestField());
83
84
        if (isset($value) && $value !== '' && !is_array($value)) {
85
            $request->query->set($this->getRequestField(), [ $value ]);
86
        }
87
88
        return parent::getState($request);
89
    }
90
91
    /**
92
     * Returns url with selected term applied.
93
     *
94
     * @param string   $key
95
     * @param ViewData $data
96
     *
97
     * @return array
98
     */
99
    protected function getOptionUrlParameters($key, ViewData $data)
100
    {
101
        $parameters = $data->getUrlParameters();
102
103
        if (isset($parameters[$this->getRequestField()])) {
104
            $parameters[$this->getRequestField()][] = $key;
105
        } else {
106
            $parameters[$this->getRequestField()] = [$key];
107
        }
108
109
        return $parameters;
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    protected function getUnsetUrlParameters($key, ViewData $data)
116
    {
117
        $parameters = $data->getUrlParameters();
118
119
        if (isset($parameters[$this->getRequestField()]) && count($parameters[$this->getRequestField()]) > 1) {
120
            $parameters[$this->getRequestField()] = array_values(
121
                array_diff($parameters[$this->getRequestField()], [$key])
122
            );
123
        } else {
124
            $parameters = $data->getResetUrlParameters();
125
        }
126
127
        return $parameters;
128
    }
129
130
    /**
131
     * {@inheritdoc}
132
     */
133
    protected function isChoiceActive($key, ViewData $data)
134
    {
135
        return $data->getState()->isActive() && in_array($key, $data->getState()->getValue());
136
    }
137
}
138