Completed
Push — master ( e2173c...0d60f0 )
by Simonas
61:10
created

Sort::getChoices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
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\Filter\Widget\AbstractSingleRequestValueFilter;
23
use ONGR\FilterManagerBundle\Search\SearchRequest;
24
25
/**
26
 * This class provides sorting filter.
27
 */
28
class Sort extends AbstractFilter implements ViewDataFactoryInterface
29
{
30
    /**
31
     * {@inheritdoc}
32
     */
33
    public function modifySearch(Search $search, FilterState $state = null, SearchRequest $request = null)
34
    {
35
        if ($state && $state->isActive()) {
36
            $stateValue = $state->getValue();
37
38
            if (!empty($this->getChoices()[$stateValue]['fields'])) {
39
                foreach ($this->getChoices()[$stateValue]['fields'] as $sortField) {
40
                    $this->addFieldToSort($search, $sortField);
41
                }
42
            } else {
43
                $this->addFieldToSort($search, $this->getChoices()[$stateValue]);
44
            }
45
        } else {
46
            foreach ($this->getChoices() as $choice) {
47
                if ($choice['default']) {
48
                    $this->addFieldToSort($search, $choice);
49
50
                    break;
51
                }
52
            }
53
        }
54
    }
55
56
    /**
57
     * {@inheritdoc}
58
     */
59
    public function preProcessSearch(Search $search, Search $relatedSearch, FilterState $state = null)
60
    {
61
        // Nothing to do here.
62
    }
63
64
    /**
65
     * {@inheritdoc}
66
     */
67
    public function createViewData()
68
    {
69
        return new ChoicesAwareViewData();
70
    }
71
72
    /**
73
     * {@inheritdoc}
74
     */
75
    public function getViewData(DocumentIterator $result, ViewData $data)
76
    {
77
        /** @var ChoicesAwareViewData $data */
78
        foreach ($this->getChoices() as $key => $choice) {
79
            $active = $data->getState()->isActive() && strcmp($data->getState()->getValue(), $key) === 0;
80
            $viewChoice = new ViewData\Choice();
81
            $viewChoice->setLabel($key);
82
83
            if (isset($choice['default'])) {
84
                $viewChoice->setDefault($choice['default']);
85
            }
86
87
            if (isset($choice['mode'])) {
88
                $viewChoice->setMode($choice['mode']);
89
            }
90
            $viewChoice->setActive($active);
91
            if ($active) {
92
                $viewChoice->setUrlParameters($data->getResetUrlParameters());
93
            } else {
94
                $viewChoice->setUrlParameters($this->getOptionUrlParameters($key, $data));
95
            }
96
            $data->addChoice($viewChoice);
97
        }
98
99
        return $data;
100
    }
101
102
    /**
103
     * Returns choices.
104
     *
105
     * @return array
106
     */
107
    public function getChoices()
108
    {
109
        return $this->getOption('choices', []);
0 ignored issues
show
Documentation introduced by
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...
110
    }
111
112
    /**
113
     * {@inheritdoc}
114
     */
115
    public function isRelated()
116
    {
117
        return false;
118
    }
119
120
    /**
121
     * @param string   $key
122
     * @param ViewData $data
123
     *
124
     * @return array
125
     */
126
    protected function getOptionUrlParameters($key, ViewData $data)
127
    {
128
        $parameters = $data->getResetUrlParameters();
129
        $parameters[$this->getRequestField()] = $key;
130
131
        return $parameters;
132
    }
133
134
    /**
135
     * Adds sort field parameters into given search object.
136
     *
137
     * @param Search $search
138
     * @param array  $sortField
139
     */
140
    private function addFieldToSort(Search $search, $sortField)
141
    {
142
        $search->addSort(
143
            new FieldSort(
144
                $sortField['field'],
145
                $sortField['order'],
146
                isset($sortField['mode']) ? ['mode' => $sortField['mode']] : []
147
            )
148
        );
149
    }
150
}
151