Grid::getResource()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
/*
4
 * This file is part of the Lug package.
5
 *
6
 * (c) Eric GELOEN <[email protected]>
7
 *
8
 * For the full copyright and license information, please read the LICENSE
9
 * file that was distributed with this source code.
10
 */
11
12
namespace Lug\Component\Grid\Model;
13
14
use Lug\Component\Grid\Exception\InvalidArgumentException;
15
use Lug\Component\Grid\Exception\OptionNotFoundException;
16
use Lug\Component\Resource\Model\ResourceInterface;
17
18
/**
19
 * @author GeLo <[email protected]>
20
 */
21
class Grid implements GridInterface
22
{
23
    /**
24
     * @var ResourceInterface
25
     */
26
    private $resource;
27
28
    /**
29
     * @var ColumnInterface[]
30
     */
31
    private $columns;
32
33
    /**
34
     * @var FilterInterface[]
35
     */
36
    private $filters;
37
38
    /**
39
     * @var SortInterface[]
40
     */
41
    private $sorts;
42
43
    /**
44
     * @var ActionInterface[]
45
     */
46
    private $globalActions;
47
48
    /**
49
     * @var ActionInterface[]
50
     */
51
    private $columnActions;
52
53
    /**
54
     * @var BatchInterface[]
55
     */
56
    private $batches;
57
58
    /**
59
     * @var mixed[]
60
     */
61
    private $data;
62
63
    /**
64
     * @var mixed[]
65
     */
66
    private $options;
67
68
    /**
69
     * @param ResourceInterface $resource
70
     * @param ColumnInterface[] $columns
71
     * @param FilterInterface[] $filters
72
     * @param SortInterface[]   $sorts
73
     * @param ActionInterface[] $globalActions
74
     * @param ActionInterface[] $columnActions
75
     * @param BatchInterface[]  $batches
76
     * @param mixed[]           $data
77
     * @param mixed[]           $options
78
     */
79 14
    public function __construct(
80
        ResourceInterface $resource,
81
        array $columns = [],
82
        array $filters = [],
83
        array $sorts = [],
84
        array $globalActions = [],
85
        array $columnActions = [],
86
        array $batches = [],
87
        array $data = [],
88
        array $options = []
89
    ) {
90 14
        $this->resource = $resource;
91 14
        $this->columns = $columns;
92 14
        $this->filters = $filters;
93 14
        $this->sorts = $sorts;
94 14
        $this->globalActions = $globalActions;
95 14
        $this->columnActions = $columnActions;
96 14
        $this->batches = $batches;
97 14
        $this->data = $data;
98 14
        $this->options = $options;
99 14
    }
100
101
    /**
102
     * {@inheritdoc}
103
     */
104 5
    public function getResource()
105
    {
106 5
        return $this->resource;
107
    }
108
109
    /**
110
     * {@inheritdoc}
111
     */
112 4
    public function hasColumns()
113
    {
114 4
        return !empty($this->columns);
115
    }
116
117
    /**
118
     * {@inheritdoc}
119
     */
120 4
    public function getColumns()
121
    {
122 4
        return $this->columns;
123
    }
124
125
    /**
126
     * {@inheritdoc}
127
     */
128 3
    public function hasColumn($name)
129
    {
130 3
        return isset($this->columns[$name]);
131
    }
132
133
    /**
134
     * {@inheritdoc}
135
     */
136 2
    public function getColumn($name)
137
    {
138 2
        if (!$this->hasColumn($name)) {
139 1
            throw new InvalidArgumentException(sprintf('The column "%s" could not be found.', $name));
140
        }
141
142 1
        return $this->columns[$name];
143
    }
144
145
    /**
146
     * {@inheritdoc}
147
     */
148 4
    public function hasFilters()
149
    {
150 4
        return !empty($this->filters);
151
    }
152
153
    /**
154
     * {@inheritdoc}
155
     */
156 4
    public function getFilters()
157
    {
158 4
        return $this->filters;
159
    }
160
161
    /**
162
     * {@inheritdoc}
163
     */
164 3
    public function hasFilter($name)
165
    {
166 3
        return isset($this->filters[$name]);
167
    }
168
169
    /**
170
     * {@inheritdoc}
171
     */
172 2
    public function getFilter($name)
173
    {
174 2
        if (!$this->hasFilter($name)) {
175 1
            throw new InvalidArgumentException(sprintf('The filter "%s" could not be found.', $name));
176
        }
177
178 1
        return $this->filters[$name];
179
    }
180
181
    /**
182
     * {@inheritdoc}
183
     */
184 4
    public function hasSorts()
185
    {
186 4
        return !empty($this->sorts);
187
    }
188
189
    /**
190
     * {@inheritdoc}
191
     */
192 4
    public function getSorts()
193
    {
194 4
        return $this->sorts;
195
    }
196
197
    /**
198
     * {@inheritdoc}
199
     */
200 3
    public function hasSort($name)
201
    {
202 3
        return isset($this->sorts[$name]);
203
    }
204
205
    /**
206
     * {@inheritdoc}
207
     */
208 2
    public function getSort($name)
209
    {
210 2
        if (!$this->hasSort($name)) {
211 1
            throw new InvalidArgumentException(sprintf('The sort "%s" could not be found.', $name));
212
        }
213
214 1
        return $this->sorts[$name];
215
    }
216
217
    /**
218
     * {@inheritdoc}
219
     */
220 4
    public function hasGlobalActions()
221
    {
222 4
        return !empty($this->globalActions);
223
    }
224
225
    /**
226
     * {@inheritdoc}
227
     */
228 4
    public function getGlobalActions()
229
    {
230 4
        return $this->globalActions;
231
    }
232
233
    /**
234
     * {@inheritdoc}
235
     */
236 3
    public function hasGlobalAction($name)
237
    {
238 3
        return isset($this->globalActions[$name]);
239
    }
240
241
    /**
242
     * {@inheritdoc}
243
     */
244 2
    public function getGlobalAction($name)
245
    {
246 2
        if (!$this->hasGlobalAction($name)) {
247 1
            throw new InvalidArgumentException(sprintf('The global action "%s" could not be found.', $name));
248
        }
249
250 1
        return $this->globalActions[$name];
251
    }
252
253
    /**
254
     * {@inheritdoc}
255
     */
256 4
    public function hasColumnActions()
257
    {
258 4
        return !empty($this->columnActions);
259
    }
260
261
    /**
262
     * {@inheritdoc}
263
     */
264 4
    public function getColumnActions()
265
    {
266 4
        return $this->columnActions;
267
    }
268
269
    /**
270
     * {@inheritdoc}
271
     */
272 3
    public function hasColumnAction($name)
273
    {
274 3
        return isset($this->columnActions[$name]);
275
    }
276
277
    /**
278
     * {@inheritdoc}
279
     */
280 2
    public function getColumnAction($name)
281
    {
282 2
        if (!$this->hasColumnAction($name)) {
283 1
            throw new InvalidArgumentException(sprintf('The column action "%s" could not be found.', $name));
284
        }
285
286 1
        return $this->columnActions[$name];
287
    }
288
289
    /**
290
     * {@inheritdoc}
291
     */
292 4
    public function hasBatches()
293
    {
294 4
        return !empty($this->batches);
295
    }
296
297
    /**
298
     * {@inheritdoc}
299
     */
300 4
    public function getBatches()
301
    {
302 4
        return $this->batches;
303
    }
304
305
    /**
306
     * {@inheritdoc}
307
     */
308 3
    public function hasBatch($name)
309
    {
310 3
        return isset($this->batches[$name]);
311
    }
312
313
    /**
314
     * {@inheritdoc}
315
     */
316 2
    public function getBatch($name)
317
    {
318 2
        if (!$this->hasBatch($name)) {
319 1
            throw new InvalidArgumentException(sprintf('The batch "%s" could not be found.', $name));
320
        }
321
322 1
        return $this->batches[$name];
323
    }
324
325
    /**
326
     * {@inheritdoc}
327
     */
328 4
    public function hasData()
329
    {
330 4
        return !empty($this->data);
331
    }
332
333
    /**
334
     * {@inheritdoc}
335
     */
336 4
    public function getData()
337
    {
338 4
        return $this->data;
339
    }
340
341
    /**
342
     * {@inheritdoc}
343
     */
344 3
    public function hasOptions()
345
    {
346 3
        return !empty($this->options);
347
    }
348
349
    /**
350
     * {@inheritdoc}
351
     */
352 5
    public function getOptions()
353
    {
354 5
        return $this->options;
355
    }
356
357
    /**
358
     * {@inheritdoc}
359
     */
360 3
    public function hasOption($name)
361
    {
362 3
        return isset($this->options[$name]);
363
    }
364
365
    /**
366
     * {@inheritdoc}
367
     */
368 2
    public function getOption($name)
369
    {
370 2
        if (!$this->hasOption($name)) {
371 1
            throw new OptionNotFoundException(sprintf('The grid option "%s" could not be found.', $name));
372
        }
373
374 1
        return $this->options[$name];
375
    }
376
}
377