QueryConfigurationBuilder   A
last analyzed

Complexity

Total Complexity 21

Size/Duplication

Total Lines 162
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Importance

Changes 0
Metric Value
wmc 21
lcom 1
cbo 3
dl 0
loc 162
rs 10
c 0
b 0
f 0

10 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 3 1
A create() 0 4 1
A drawCall() 0 8 3
A start() 0 8 2
A length() 0 8 2
A searchValue() 0 8 2
A searchRegex() 0 8 4
A columnSearch() 0 8 2
A columnOrder() 0 9 3
A build() 0 12 1
1
<?php
2
3
namespace OpenSkill\Datatable\Queries;
4
5
use OpenSkill\Datatable\Columns\ColumnOrder;
6
use OpenSkill\Datatable\Columns\ColumnSearch;
7
8
class QueryConfigurationBuilder
9
{
10
11
    /** @var mixed */
12
    protected $drawCall = 1;
13
14
    /** @var mixed zero based */
15
    protected $start = 0;
16
17
    /** @var mixed */
18
    protected $length = 10;
19
20
    /** @var string */
21
    protected $searchValue = null;
22
23
    /** @var bool */
24
    protected $searchRegex = false;
25
26
    /** @var array */
27
    protected $columSearches = [];
28
29
    /** @var array */
30
    protected $columnOrders = [];
31
32
    /**
33
     * DTQueryConfigurationBuilder constructor, private by default, so new instances are created using the builder
34
     * pattern
35
     */
36
    private function __construct()
37
    {
38
    }
39
40
    /**
41
     * Will create a new QueryConfigurationBuilder internally and acts as static builder method
42
     * @return QueryConfigurationBuilder
43
     */
44
    public static function create()
45
    {
46
        return new QueryConfigurationBuilder();
47
    }
48
49
    /**
50
     * Will set the drawCall parameter send by the frontend.
51
     * @param mixed $drawCall The draw call parameter
52
     * @return $this
53
     */
54
    public function drawCall($drawCall)
55
    {
56
        if (!is_string($drawCall) && !is_numeric($drawCall)) {
57
            throw new \InvalidArgumentException('$drawCall needs to be a string or numeric');
58
        }
59
        $this->drawCall = $drawCall;
60
        return $this;
61
    }
62
63
    /**
64
     * Will set the start parameter which indicates how many items should be skipped at the start
65
     * @param mixed $start
66
     * @return $this
67
     */
68
    public function start($start)
69
    {
70
        if (!is_numeric($start)) {
71
            throw new \InvalidArgumentException('$start needs to be numeric');
72
        }
73
        $this->start = $start;
74
        return $this;
75
    }
76
77
    /**
78
     * Will set the length parameter which indicates how many items should be returned by this request.
79
     * @param mixed $length
80
     * @return $this
81
     */
82
    public function length($length)
83
    {
84
        if (!is_numeric($length)) {
85
            throw new \InvalidArgumentException('$length needs to be numeric');
86
        }
87
        $this->length = $length;
88
        return $this;
89
    }
90
91
    /**
92
     * Will set the search value the frontend send that should be used for the global search
93
     * @param string $searchValue
94
     * @return $this
95
     */
96
    public function searchValue($searchValue)
97
    {
98
        if (!is_string($searchValue)) {
99
            throw new \InvalidArgumentException('$searchValue needs to be a string');
100
        }
101
        $this->searchValue = $searchValue;
102
        return $this;
103
    }
104
105
    /**
106
     * Will indicate if the global search value should be used as a regular expression
107
     * @param bool $searchRegex
108
     * @return $this
109
     */
110
    public function searchRegex($searchRegex)
111
    {
112
        if (!is_bool($searchRegex) && $searchRegex !== 'false' && $searchRegex !== 'true') {
113
            throw new \InvalidArgumentException('$searchRegex needs to be a boolean');
114
        }
115
        $this->searchRegex = $searchRegex;
116
        return $this;
117
    }
118
119
    /**
120
     * Will add the given search value to the given column which indicates that the frontend wants to search on the
121
     * given column for the given value
122
     * @param string $columnName The name of the column that will be searched
123
     * @param string $searchValue The value to search for
124
     * @return $this
125
     */
126
    public function columnSearch($columnName, $searchValue)
127
    {
128
        if (!is_string($searchValue)) {
129
            throw new \InvalidArgumentException('$searchValue needs to be a string');
130
        }
131
        $this->columSearches[$columnName] = new ColumnSearch($columnName, $searchValue);
132
        return $this;
133
    }
134
135
    /**
136
     * Will set the ordering of the column to the given direction if possible
137
     * @param string $columnName The column name that should be ordered
138
     * @param string $orderDirection the direction that the column should be ordered by
139
     * @return $this
140
     */
141
    public function columnOrder($columnName, $orderDirection)
142
    {
143
        if (!is_string($orderDirection)) {
144
            throw new \InvalidArgumentException('$orderDirection "' . $orderDirection . '" needs to be a string');
145
        }
146
        $isAscOrdering = $orderDirection === "asc" ? true : false;
147
        $this->columnOrders[] = new ColumnOrder($columnName, $isAscOrdering);
148
        return $this;
149
    }
150
151
    /**
152
     * Will build the final QueryConfiguration that will be used later in the process pipeline
153
     * @return QueryConfiguration
154
     */
155
    public function build()
156
    {
157
        return new QueryConfiguration(
158
            $this->drawCall,
159
            $this->start,
160
            $this->length,
161
            $this->searchValue,
162
            $this->searchRegex,
163
            $this->columSearches,
164
            $this->columnOrders
165
        );
166
    }
167
168
169
}