Completed
Push — dev ( 243dfc...851a92 )
by Arnaud
9s
created

RequestFilter::getOrder()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 0
cts 0
cp 0
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 2
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 9
    /**
21
     * @var array
22 9
     */
23 9
    protected $orders;
24
25
    /**
26
     * @var int
27
     */
28
    protected $maxPerPage;
29
30
    /**
31 9
     * @var int
32
     */
33 9
    protected $currentPage;
34
35 9
    /**
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 9
     * @param int $maxPerPage
42 9
     */
43
    public function configure(array $filters = [], array $orders = [], $maxPerPage = 1)
44
    {
45
        $this->filters = $filters;
46
        $this->orders = $orders;
47
        $this->maxPerPage = $maxPerPage;
48
    }
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
    public function filter(Request $request)
56
    {
57
        // filter the request parameters with configured filters for criteria parameters
58
        $filteredValues = new ParameterBag();
59
60
        foreach ($this->filters as $filter) {
61
            $value = $request->get($filter);
62
63
            if ($value !== null) {
64
                $filteredValues->set($filter, $value);
65
            }
66
        }
67
        $this->criteria = $filteredValues->all();
68
69
        // filter the request parameters to find some orders parameters
70
        if ($request->get('sort') && $request->get('order')) {
71
            // if sort and order parameters are present in the request, we use them
72
            $this->orders = [
73
                $request->get('sort') => $request->get('order')
74
            ];
75
        }
76
    }
77
78
    public function getCriteria()
79
    {
80
        return $this->criteria;
81
    }
82
83
    public function getOrder()
84
    {
85
        return $this->orders;
86
    }
87
88
    public function getMaxPerPage()
89
    {
90
        return $this->maxPerPage;
91
    }
92
93
    public function getCurrentPage()
94
    {
95
        return 1;
96
    }
97
}
98