Completed
Push — master ( bd6be0...317eb6 )
by Tim
02:07
created

Datatable19QueryParser::parse()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 19
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 8
Bugs 0 Features 3
Metric Value
c 8
b 0
f 3
dl 0
loc 19
rs 9.4285
cc 1
eloc 10
nc 1
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 extends QueryParser
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
        $this->getSearchColumns($query, $builder, $columnConfiguration);
51
52
        return $builder->build();
53
    }
54
55
    /**
56
     * Helper function that will check if a variable is empty
57
     * @param mixed $string
58
     * @return bool true if empty, false otherwise
59
     */
60
    private function isEmpty($string)
61
    {
62
        return empty($string);
63
    }
64
65
    /**
66
     * @param ParameterBag $query
67
     * @param QueryConfigurationBuilder $builder
68
     */
69
    public function getDrawCall($query, $builder)
70
    {
71
        if ($query->has('sEcho')) {
72
            $builder->drawCall($query->get('sEcho'));
73
        }
74
    }
75
76
    /**
77
     * @param ParameterBag $query
78
     * @param QueryConfigurationBuilder $builder
79
     */
80
    public function getStart($query, $builder)
81
    {
82
        if ($query->has('iDisplayStart')) {
83
            $builder->start($query->get('iDisplayStart'));
84
        }
85
    }
86
87
    /**
88
     * @param ParameterBag $query
89
     * @param QueryConfigurationBuilder $builder
90
     */
91
    public function getLength($query, $builder)
92
    {
93
        if ($query->has('iDisplayLength')) {
94
            $builder->length($query->get('iDisplayLength'));
95
        }
96
    }
97
98
    /**
99
     * @param ParameterBag $query
100
     * @param QueryConfigurationBuilder $builder
101
     */
102
    public function getSearch($query, $builder)
103
    {
104
        if ($query->has('sSearch') && !$this->isEmpty($query->get('sSearch'))) {
105
            $builder->searchValue($query->get('sSearch'));
106
        }
107
    }
108
109
    /**
110
     * @param ParameterBag $query
111
     * @param QueryConfigurationBuilder $builder
112
     */
113
    public function getSearchColumns($query, $builder, array $columnConfiguration)
114
    {
115
        // for each column we need to see if there is a search value
116
        foreach ($columnConfiguration as $i => $c) {
117
            // check if there is something search related
118
            if ($c->getSearch()->isSearchable() && $query->has("sSearch_" . $i) && !$this->isEmpty($query->get("sSearch_" . $i))) {
119
                // search for this column is available
120
                $builder->columnSearch($c->getName(), $query->get("sSearch_" . $i));
121
            }
122
            // check if there is something order related
123
            if ($c->getOrder()->isOrderable() && $query->has("iSortCol_" . $i) && !$this->isEmpty($query->get("iSortCol_" . $i))) {
124
                // order for this column is available
125
                $builder->columnOrder($c->getName(), $query->get("sSortDir_" . $i));
126
            }
127
        }
128
    }
129
130
    /**
131
     * @param ParameterBag $query
132
     * @param QueryConfigurationBuilder $builder
133
     */
134
    public function getRegex($query, $builder)
135
    {
136
        if ($query->has('bRegex')) {
137
            $builder->searchRegex($query->get('bRegex'));
138
        }
139
    }
140
}