Completed
Push — master ( e62fa2...4391c3 )
by Kristof
133:20 queued 117:59
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
use Symfony\Component\HttpFoundation\Request;
7
8
/**
9
 * AdminListFilter
10
 */
11
class FilterBuilder
12
{
13
    /**
14
     * @var array
15
     */
16
    private $filterDefinitions = array();
17
18
    /**
19
     * @var Filter[]
20
     */
21
    private $currentFilters = array();
22
23
    /**
24
     * @var array
25
     */
26
    private $currentParameters = array();
27
28
    /**
29
     * @param string              $columnName The column name
30
     * @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...
31
     * @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...
32
     * @param array               $options    Options
33
     *
34
     * @return FilterBuilder
35
     */
36
    public function add($columnName, FilterTypeInterface $type = null, $filterName = null, array $options = array())
37
    {
38
        $this->filterDefinitions[$columnName] = array(
39
            'type' => $type,
40
            'options' => $options,
41
            'filtername' => $filterName,
42
        );
43
44
        return $this;
45
    }
46
47
    /**
48
     * @param string $columnName
49
     *
50
     * @return mixed|null
51
     */
52
    public function get($columnName)
53
    {
54
        if (isset($this->filterDefinitions[$columnName])) {
55
            return $this->filterDefinitions[$columnName];
56
        }
57
58
        return null;
59
    }
60
61
    /**
62
     * @param string $columnName
63
     *
64
     * @return FilterBuilder
65
     */
66
    public function remove($columnName)
67
    {
68
        if (isset($this->filterDefinitions[$columnName])) {
69
            unset($this->filterDefinitions[$columnName]);
70
        }
71
72
        return $this;
73
    }
74
75
    /**
76
     * @param string $columnName
77
     *
78
     * @return bool
79
     */
80
    public function has($columnName)
81
    {
82
        return isset($this->filterDefinitions[$columnName]);
83
    }
84
85
    /**
86
     * @return array
87
     */
88
    public function getFilterDefinitions()
89
    {
90
        return $this->filterDefinitions;
91
    }
92
93
    /**
94
     * @param Request $request
95
     */
96
    public function bindRequest(Request $request)
97
    {
98
        $filterBuilderName = 'filter_' . $request->get('_route');
99
100
        $this->currentParameters = $request->query->all();
101
        if (count($this->currentParameters) === 0) {
102
            if (!$request->query->has('filter')) {
103
                if ($request->getSession()->has($filterBuilderName)) {
104
                    $savedQuery = $request->getSession()->get($filterBuilderName);
105
                    $request->query->replace($savedQuery);
106
                    $this->currentParameters = $savedQuery;
107
                }
108
            }
109
        } else {
110
            $request->getSession()->set($filterBuilderName, $this->currentParameters);
111
        }
112
113
        $filterColumnNames = $request->query->get('filter_columnname');
114
        if (isset($filterColumnNames)) {
115
            $uniqueIds = $request->query->get('filter_uniquefilterid');
116
            $index = 0;
117
            foreach ($filterColumnNames as $filterColumnName) {
118
                $uniqueId = $uniqueIds[$index];
119
                $filter = new Filter($filterColumnName, $this->get($filterColumnName), $uniqueId);
120
                $this->currentFilters[] = $filter;
121
                $filter->bindRequest($request);
122
                ++$index;
123
            }
124
        }
125
    }
126
127
    /**
128
     * @return array
129
     */
130
    public function getCurrentParameters()
131
    {
132
        return $this->currentParameters;
133
    }
134
135
    /**
136
     * @return Filter[]
137
     */
138
    public function getCurrentFilters()
139
    {
140
        return $this->currentFilters;
141
    }
142
}
143