Completed
Push — master ( 3af233...d71cef )
by WEBEWEB
18:03
created

DataTablesRequest::getRequest()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 3
c 0
b 0
f 0
rs 10
cc 1
nc 1
nop 0
1
<?php
2
3
/**
4
 * This file is part of the jquery-datatables-bundle package.
5
 *
6
 * (c) 2018 WEBEWEB
7
 *
8
 * For the full copyright and license information, please view the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace WBW\Bundle\JQuery\DataTablesBundle\API;
13
14
use Symfony\Component\HttpFoundation\ParameterBag;
15
use WBW\Library\Core\Network\HTTP\HTTPInterface;
16
17
/**
18
 * DataTables request.
19
 *
20
 * @author webeweb <https://github.com/webeweb/>
21
 * @package WBW\Bundle\JQuery\DataTablesBundle\API
22
 */
23
class DataTablesRequest implements DataTablesRequestInterface, HTTPInterface {
24
25
    /**
26
     * @var DataTablesWrapperTrait
27
     */
28
    use DataTablesWrapperTrait;
29
30
    /**
31
     * Columns.
32
     *
33
     * @var DataTablesColumnInterface[]
34
     */
35
    private $columns;
36
37
    /**
38
     * Draw.
39
     *
40
     * @var int
41
     */
42
    private $draw;
43
44
    /**
45
     * Length.
46
     *
47
     * @var int
48
     */
49
    private $length;
50
51
    /**
52
     * Order.
53
     *
54
     * @var DataTablesOrderInterface[]
55
     */
56
    private $order;
57
58
    /**
59
     * Query.
60
     *
61
     * @var ParameterBag
62
     */
63
    private $query;
64
65
    /**
66
     * Request.
67
     *
68
     * @var ParameterBag
69
     */
70
    private $request;
71
72
    /**
73
     * Search.
74
     *
75
     * @var DataTablesSearchInterface
76
     */
77
    private $search;
78
79
    /**
80
     * Start.
81
     *
82
     * @var int
83
     */
84
    private $start;
85
86
    /**
87
     * Constructor.
88
     */
89
    public function __construct() {
90
        $this->setColumns([]);
91
        $this->setDraw(0);
92
        $this->setLength(10);
93
        $this->setOrder([]);
94
        $this->setQuery(new ParameterBag());
95
        $this->setRequest(new ParameterBag());
96
        $this->setStart(0);
97
    }
98
99
    /**
100
     * {@inheritdoc}
101
     */
102
    public function getColumn($data) {
103
        foreach ($this->columns as $current) {
104
            if ($data === $current->getData()) {
105
                return $current;
0 ignored issues
show
Bug Best Practice introduced by
The return type of return $current; (WBW\Bundle\JQuery\DataTa...taTablesColumnInterface) is incompatible with the return type declared by the interface WBW\Bundle\JQuery\DataTa...estInterface::getColumn of type array.

If you return a value from a function or method, it should be a sub-type of the type that is given by the parent type f.e. an interface, or abstract method. This is more formally defined by the Lizkov substitution principle, and guarantees that classes that depend on the parent type can use any instance of a child type interchangably. This principle also belongs to the SOLID principles for object oriented design.

Let’s take a look at an example:

class Author {
    private $name;

    public function __construct($name) {
        $this->name = $name;
    }

    public function getName() {
        return $this->name;
    }
}

abstract class Post {
    public function getAuthor() {
        return 'Johannes';
    }
}

class BlogPost extends Post {
    public function getAuthor() {
        return new Author('Johannes');
    }
}

class ForumPost extends Post { /* ... */ }

function my_function(Post $post) {
    echo strtoupper($post->getAuthor());
}

Our function my_function expects a Post object, and outputs the author of the post. The base class Post returns a simple string and outputting a simple string will work just fine. However, the child class BlogPost which is a sub-type of Post instead decided to return an object, and is therefore violating the SOLID principles. If a BlogPost were passed to my_function, PHP would not complain, but ultimately fail when executing the strtoupper call in its body.

Loading history...
106
            }
107
        }
108
        return null;
109
    }
110
111
    /**
112
     * {@inheritdoc}
113
     */
114
    public function getColumns() {
115
        return $this->columns;
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121
    public function getDraw() {
122
        return $this->draw;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128
    public function getLength() {
129
        return $this->length;
130
    }
131
132
    /**
133
     * {@inheritdoc}
134
     */
135
    public function getOrder() {
136
        return $this->order;
137
    }
138
139
    /**
140
     * {@inheritdoc}
141
     */
142
    public function getQuery() {
143
        return $this->query;
144
    }
145
146
    /**
147
     * {@inheritdoc}
148
     */
149
    public function getRequest() {
150
        return $this->request;
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156
    public function getSearch() {
157
        return $this->search;
158
    }
159
160
    /**
161
     * {@inheritdoc}
162
     */
163
    public function getStart() {
164
        return $this->start;
165
    }
166
167
    /**
168
     * Set the columns.
169
     *
170
     * @param DataTablesColumnInterface[] $columns The columns.
171
     * @return DataTablesRequestInterface Returns this request.
172
     */
173
    public function setColumns(array $columns) {
174
        $this->columns = $columns;
175
        return $this;
176
    }
177
178
    /**
179
     * Set the draw.
180
     *
181
     * @param int $draw The draw.
182
     * @return DataTablesRequestInterface Returns this request.
183
     */
184
    public function setDraw($draw) {
185
        $this->draw = $draw;
186
        return $this;
187
    }
188
189
    /**
190
     * Set the length.
191
     *
192
     * @param int $length The length.
193
     * @return DataTablesRequestInterface Returns this request.
194
     */
195
    public function setLength($length) {
196
        $this->length = $length;
197
        return $this;
198
    }
199
200
    /**
201
     * Set the order.
202
     *
203
     * @param DataTablesOrderInterface[] $order The order.
204
     * @return DataTablesRequestInterface Returns this request.
205
     */
206
    public function setOrder(array $order) {
207
        $this->order = $order;
208
        return $this;
209
    }
210
211
    /**
212
     * Set the request.
213
     *
214
     * @param ParameterBag $query The query.
215
     * @return DataTablesRequestInterface Returns this request.
216
     */
217
    protected function setQuery(ParameterBag $query) {
218
        $this->query = $query;
219
        return $this;
220
    }
221
222
    /**
223
     * Set the request.
224
     *
225
     * @param ParameterBag $request The request.
226
     * @return DataTablesRequestInterface Returns this request.
227
     */
228
    protected function setRequest(ParameterBag $request) {
229
        $this->request = $request;
230
        return $this;
231
    }
232
233
    /**
234
     * Set the search.
235
     *
236
     * @param DataTablesSearchInterface $search The search.
237
     * @return DataTablesRequestInterface Returns this request.
238
     */
239
    public function setSearch(DataTablesSearchInterface $search) {
240
        $this->search = $search;
241
        return $this;
242
    }
243
244
    /**
245
     * Set the start.
246
     *
247
     * @param int $start The start.
248
     * @return DataTablesRequestInterface Returns this request.
249
     */
250
    public function setStart($start) {
251
        $this->start = $start;
252
        return $this;
253
    }
254
255
}
256