Completed
Push — master ( a8e426...cdcd92 )
by Simonas
62:46
created

Filter/Widget/Sort/Sort.php (1 issue)

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

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\Sort;
13
14
use ONGR\ElasticsearchDSL\Search;
15
use ONGR\ElasticsearchBundle\Result\DocumentIterator;
16
use ONGR\ElasticsearchDSL\Sort\FieldSort;
17
use ONGR\FilterManagerBundle\Filter\FilterState;
18
use ONGR\FilterManagerBundle\Filter\Helper\ViewDataFactoryInterface;
19
use ONGR\FilterManagerBundle\Filter\ViewData\ChoicesAwareViewData;
20
use ONGR\FilterManagerBundle\Filter\ViewData;
21
use ONGR\FilterManagerBundle\Filter\Widget\AbstractFilter;
22
use ONGR\FilterManagerBundle\Search\SearchRequest;
23
24
/**
25
 * This class provides sorting filter.
26
 */
27
class Sort extends AbstractFilter implements ViewDataFactoryInterface
28
{
29
    /**
30
     * {@inheritdoc}
31
     */
32
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
33
    {
34
        if ($state && $state->isActive()) {
35
            $stateValue = $state->getValue();
36
37
            if (!empty($this->getChoices()[$stateValue]['fields'])) {
38
                foreach ($this->getChoices()[$stateValue]['fields'] as $sortField) {
39
                    $this->addFieldToSort($search, $sortField);
40
                }
41
            } else {
42
                $this->addFieldToSort($search, $this->getChoices()[$stateValue]);
43
            }
44
        } else {
45
            foreach ($this->getChoices() as $choice) {
46
                if ($choice['default']) {
47
                    $this->addFieldToSort($search, $choice);
48
49
                    break;
50
                }
51
            }
52
        }
53
    }
54
55
    /**
56
     * {@inheritdoc}
57
     */
58
    public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null)
59
    {
60
        // Nothing to do here.
61
    }
62
63
    /**
64
     * {@inheritdoc}
65
     */
66
    public function createViewData()
67
    {
68
        return new ChoicesAwareViewData();
69
    }
70
71
    /**
72
     * {@inheritdoc}
73
     */
74
    public function getViewData(DocumentIterator $result, ViewData $data)
75
    {
76
        /** @var ChoicesAwareViewData $data */
77
        foreach ($this->getChoices() as $key => $choice) {
78
            $active = $data->getState()->isActive() && strcmp($data->getState()->getValue(), $key) === 0;
79
            $viewChoice = new ViewData\ChoiceAwareViewData();
80
            $viewChoice->setLabel($key);
81
82
            if (isset($choice['default'])) {
83
                $viewChoice->setDefault($choice['default']);
84
            }
85
86
            if (isset($choice['mode'])) {
87
                $viewChoice->setMode($choice['mode']);
88
            }
89
            $viewChoice->setActive($active);
90
            if ($active) {
91
                $viewChoice->setUrlParameters($data->getResetUrlParameters());
92
            } else {
93
                $viewChoice->setUrlParameters($this->getOptionUrlParameters($key, $data));
94
            }
95
            $data->addChoice($viewChoice);
96
        }
97
98
        return $data;
99
    }
100
101
    /**
102
     * Returns choices.
103
     *
104
     * @return array
105
     */
106
    public function getChoices()
107
    {
108
        return $this->getOption('choices', []);
0 ignored issues
show
array() is of type array, but the function expects a string|null.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function isRelated()
115
    {
116
        return false;
117
    }
118
119
    /**
120
     * @param string   $key
121
     * @param ViewData $data
122
     *
123
     * @return array
124
     */
125
    protected function getOptionUrlParameters($key, ViewData $data)
126
    {
127
        $parameters = $data->getResetUrlParameters();
128
        $parameters[$this->getRequestField()] = $key;
129
130
        return $parameters;
131
    }
132
133
    /**
134
     * Adds sort field parameters into given search object.
135
     *
136
     * @param Search $search
137
     * @param array  $sortField
138
     */
139
    private function addFieldToSort(Search $search, $sortField)
140
    {
141
        $search->addSort(
142
            new FieldSort(
143
                $sortField['field'],
144
                $sortField['order'],
145
                isset($sortField['mode']) ? ['mode' => $sortField['mode']] : []
146
            )
147
        );
148
    }
149
}
150