Completed
Push — master ( 9bde92...3d27da )
by Nils
03:57
created

Datatable19QueryParser::parse()   C

Complexity

Conditions 8
Paths 5

Size

Total Lines 33
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 0 Features 2
Metric Value
c 6
b 0
f 2
dl 0
loc 33
rs 5.3846
cc 8
eloc 15
nc 5
nop 2
1
<?php
2
3
namespace OpenSkill\Datatable\Queries\Parser;
4
5
6
use OpenSkill\Datatable\Columns\ColumnConfiguration;
7
use OpenSkill\Datatable\Queries\QueryConfiguration;
8
use OpenSkill\Datatable\Queries\QueryConfigurationBuilder;
9
use Symfony\Component\HttpFoundation\JsonResponse;
10
use Symfony\Component\HttpFoundation\ParameterBag;
11
use Symfony\Component\HttpFoundation\Request;
12
13
class Datatable19QueryParser
14
{
15
16
    /**
17
     * Method to determine if this parser can handle the query parameters. If so then the parser should return true
18
     * and be able to return a DTQueryConfiguration
19
     *
20
     * @param Request $request The current request, that should be investigated
21
     * @return bool true if the parser is able to parse the query parameters and to return a DTQueryConfiguration
22
     */
23
    public function canParse(Request $request)
24
    {
25
        return $request->query->has("sEcho");
26
    }
27
28
    /**
29
     * Method that should parse the request and return a DTQueryConfiguration
30
     *
31
     * @param Request $request The current request that should be investigated
32
     * @param ColumnConfiguration[] $columnConfiguration The configuration of the columns
33
     * @return QueryConfiguration the configuration the provider can use to prepare the data
34
     */
35
    public function parse(Request $request, array $columnConfiguration)
36
    {
37
        $query = $request->query;
38
        $builder = QueryConfigurationBuilder::create();
39
40
        $this->getDrawCall($query, $builder);
41
42
        $this->getStart($query, $builder);
43
44
        $this->getLength($query, $builder);
45
46
        $this->getSearch($query, $builder);
47
48
        $this->getRegex($query, $builder);
49
50
        // for each column we need to see if there is a search value
51
        foreach ($columnConfiguration as $i => $c) {
52
            // increment the index as we are 0 based but data tables is not
53
            $i++;
54
            // check if there is something search related
55
            if ($c->getSearch()->isSearchable() && $query->has("sSearch_" . $i) && !$this->isEmpty($query->get("sSearch_" . $i))) {
56
                // search for this column is available
57
                $builder->columnSearch($c->getName(), $query->get("sSearch_" . $i));
58
            }
59
            // check if there is something order related
60
            if ($c->getOrder()->isOrderable() && $query->has("iSortCol_" . $i) && !$this->isEmpty($query->get("iSortCol_" . $i))) {
61
                // order for this column is available
62
                $builder->columnOrder($c->getName(), $query->get("sSortDir_" . $i));
63
            }
64
        }
65
66
        return $builder->build();
67
    }
68
69
    /**
70
     * Helper function that will check if a variable is empty
71
     * @param mixed $string
72
     * @return bool true if empty, false otherwise
73
     */
74
    private function isEmpty($string) {
75
        return empty($string);
76
    }
77
78
    /**
79
     * @param ParameterBag $query
80
     * @param QueryConfigurationBuilder $builder
81
     */
82
    public function getDrawCall($query, $builder)
83
    {
84
        if ($query->has('sEcho')) {
85
            $builder->drawCall($query->get('sEcho'));
86
        }
87
    }
88
89
    /**
90
     * @param ParameterBag $query
91
     * @param QueryConfigurationBuilder $builder
92
     */
93
    public function getStart($query, $builder)
94
    {
95
        if ($query->has('iDisplayStart')) {
96
            $builder->start($query->get('iDisplayStart'));
97
        }
98
    }
99
100
    /**
101
     * @param ParameterBag $query
102
     * @param QueryConfigurationBuilder $builder
103
     */
104
    public function getLength($query, $builder)
105
    {
106
        if ($query->has('iDisplayLength')) {
107
            $builder->length($query->get('iDisplayLength'));
108
        }
109
    }
110
111
    /**
112
     * @param ParameterBag $query
113
     * @param QueryConfigurationBuilder $builder
114
     */
115
    public function getSearch($query, $builder)
116
    {
117
        if ($query->has('sSearch') && !$this->isEmpty($query->get('sSearch'))) {
118
            $builder->searchValue($query->get('sSearch'));
119
        }
120
    }
121
122
    /**
123
     * @param ParameterBag $query
124
     * @param QueryConfigurationBuilder $builder
125
     */
126
    public function getRegex($query, $builder)
127
    {
128
        if ($query->has('bRegex')) {
129
            $builder->searchRegex($query->get('bRegex'));
130
        }
131
    }
132
}