Completed
Pull Request — master (#71)
by Arnaud
15:22 queued 12:55
created

RequestFilter::getCriteria()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace LAG\AdminBundle\Filter;
4
5
use Symfony\Component\HttpFoundation\Request;
6
use Symfony\Component\HttpFoundation\ParameterBag;
7
8
class RequestFilter implements RequestFilterInterface
9
{
10
    /**
11
     * @var array
12
     */
13
    protected $filters;
14
15
    /**
16
     * @var array
17
     */
18
    protected $criteria = [];
19
20
    /**
21
     * @var array
22
     */
23
    protected $orders;
24
25
    /**
26
     * @var int
27
     */
28
    protected $maxPerPage;
29
30
    /**
31
     * @var int
32
     */
33
    protected $currentPage;
34
35
    /**
36
     * Configure the filter.
37
     *
38
     * @param array $filters Filters for criteria parameters from configuration. Those parameters will be filtered from
39
     * the request
40
     * @param array $orders Default orders from configuration
41
     * @param int $maxPerPage
42
     */
43 16
    public function configure(array $filters = [], array $orders = [], $maxPerPage = 1)
44
    {
45 16
        $this->filters = $filters;
46 16
        $this->orders = $orders;
47 16
        $this->maxPerPage = $maxPerPage;
48 16
    }
49
50
    /**
51
     * Filter request values according to configured filters. Get orders and sort parameters from the request too.
52
     *
53
     * @param Request $request
54
     */
55 14
    public function filter(Request $request)
56
    {
57
        // filter the request parameters with configured filters for criteria parameters
58 14
        $filteredValues = new ParameterBag();
59
60 14
        foreach ($this->filters as $filter) {
61 2
            $value = $request->get($filter);
62
63 2
            if ($value !== null) {
64 2
                $filteredValues->set($filter, $value);
65
            }
66
        }
67 14
        $this->criteria = $filteredValues->all();
68
69
        // filter the request parameters to find some orders parameters
70 14
        if ($request->get('sort') && $request->get('order')) {
71
            // if sort and order parameters are present in the request, we use them
72 2
            $this->orders = [
73 2
                $request->get('sort') => $request->get('order')
74
            ];
75
        }
76 14
    }
77
78 16
    public function getCriteria()
79
    {
80 16
        return $this->criteria;
81
    }
82
83 16
    public function getOrder()
84
    {
85 16
        return $this->orders;
86
    }
87
88 16
    public function getMaxPerPage()
89
    {
90 16
        return $this->maxPerPage;
91
    }
92
93 13
    public function getCurrentPage()
94
    {
95 13
        return 1;
96
    }
97
}
98