Completed
Push — master ( 91fdab...75a7b9 )
by
unknown
13:37
created

AdminListBundle/AdminList/FilterBuilder.php (2 issues)

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
namespace Kunstmaan\AdminListBundle\AdminList;
4
5
use Kunstmaan\AdminListBundle\AdminList\FilterType\FilterTypeInterface;
6
7
use Symfony\Component\HttpFoundation\Request;
8
9
/**
10
 * AdminListFilter
11
 */
12
class FilterBuilder
13
{
14
15
    /**
16
     * @var array
17
     */
18
    private $filterDefinitions = array();
19
20
    /**
21
     * @var Filter[]
22
     */
23
    private $currentFilters = array();
24
25
    /**
26
     * @var array
27
     */
28
    private $currentParameters = array();
29
30
    /**
31
     * @param string              $columnName The column name
32
     * @param FilterTypeInterface $type       The filter type
0 ignored issues
show
Should the type for parameter $type not be null|FilterTypeInterface?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
33
     * @param string              $filterName The name of the filter
0 ignored issues
show
Should the type for parameter $filterName not be string|null?

This check looks for @param annotations where the type inferred by our type inference engine differs from the declared type.

It makes a suggestion as to what type it considers more descriptive.

Most often this is a case of a parameter that can be null in addition to its declared types.

Loading history...
34
     * @param array               $options    Options
35
     *
36
     * @return FilterBuilder
37
     */
38
    public function add($columnName, FilterTypeInterface $type = null, $filterName = null, array $options = array())
39
    {
40
        $this->filterDefinitions[$columnName] = array(
41
            'type' => $type,
42
            'options' => $options,
43
            'filtername' => $filterName
44
        );
45
46
        return $this;
47
    }
48
49
    /**
50
     * @param string $columnName
51
     *
52
     * @return mixed|null
53
     */
54
    public function get($columnName)
55
    {
56
        if (isset($this->filterDefinitions[$columnName])) {
57
            return $this->filterDefinitions[$columnName];
58
        }
59
60
        return null;
61
    }
62
63
    /**
64
     * @param string $columnName
65
     *
66
     * @return FilterBuilder
67
     */
68
    public function remove($columnName)
69
    {
70
        if (isset($this->filterDefinitions[$columnName])) {
71
            unset($this->filterDefinitions[$columnName]);
72
        }
73
74
        return $this;
75
    }
76
77
    /**
78
     * @param string $columnName
79
     *
80
     * @return bool
81
     */
82
    public function has($columnName)
83
    {
84
        return isset($this->filterDefinitions[$columnName]);
85
    }
86
87
    /**
88
     * @return array
89
     */
90
    public function getFilterDefinitions()
91
    {
92
        return $this->filterDefinitions;
93
    }
94
95
    /**
96
     * @param Request $request
97
     */
98
    public function bindRequest(Request $request)
99
    {
100
        $filterBuilderName = 'filter_' . $request->get('_route');
101
102
        $this->currentParameters = $request->query->all();
103
        if(count($this->currentParameters) === 0) {
104
            if (!$request->query->has('filter')) {
105
                if ($request->getSession()->has($filterBuilderName)) {
106
                    $savedQuery = $request->getSession()->get($filterBuilderName);
107
                    $request->query->replace($savedQuery);
108
                    $this->currentParameters = $savedQuery;
109
                }
110
            }
111
        } else {
112
            $request->getSession()->set($filterBuilderName, $this->currentParameters);
113
        }
114
115
        $filterColumnNames = $request->query->get('filter_columnname');
116
        if (isset($filterColumnNames)) {
117
            $uniqueIds = $request->query->get('filter_uniquefilterid');
118
            $index = 0;
119
            foreach ($filterColumnNames as $filterColumnName) {
120
                $uniqueId = $uniqueIds[$index];
121
                $filter = new Filter($filterColumnName, $this->get($filterColumnName), $uniqueId);
122
                $this->currentFilters[] = $filter;
123
                $filter->bindRequest($request);
124
                $index++;
125
            }
126
        }
127
    }
128
129
    /**
130
     * @return array
131
     */
132
    public function getCurrentParameters()
133
    {
134
        return $this->currentParameters;
135
    }
136
137
    /**
138
     * @return Filter[]
139
     */
140
    public function getCurrentFilters()
141
    {
142
        return $this->currentFilters;
143
    }
144
}
145