Completed
Pull Request — master (#118)
by
unknown
05:41
created

FieldConfig::isHiddenXs()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php
2
namespace Nayjest\Grids;
3
4
use Illuminate\Support\Collection;
5
6
/**
7
 * Class FieldConfig
8
 *
9
 * This class describes grid column.
10
 *
11
 * @package Nayjest\Grids
12
 */
13
class FieldConfig
14
{
15
    /**
16
     * Field name.
17
     *
18
     * @var string
19
     */
20
    protected $name;
21
22
    /**
23
     * Text label that will be rendered in table header.
24
     *
25
     * @var string
26
     */
27
    protected $label;
28
29
    /**
30
     * @var int
31
     */
32
    protected $order = 0;
33
34
    /**
35
     * @var bool
36
     */
37
    protected $is_sortable = false;
38
39
    protected $sorting;
40
41
    /** @var  Collection|FilterConfig[] */
42
    protected $filters;
43
44
    /** @var  callable */
45
    protected $callback;
46
47
    protected $is_hidden = false;
48
    protected $is_hiddenXs = false;
49
    protected $is_hiddenSm = false;
50
    protected $is_hiddenMd = false;
51
    protected $is_hiddenLg = false;
52
53
54
    /**
55
     * Constructor.
56
     *
57
     * @param string|null $name column unique name for internal usage
58
     * @param string|null $label column label
59
     */
60
    public function __construct($name = null, $label = null)
61
    {
62
        if ($name !== null) {
63
            $this->setName($name);
64
        }
65
        if ($label !== null) {
66
            $this->setLabel($label);
67
        }
68
    }
69
70
    /**
71
     * Returns column order.
72
     *
73
     * This property used to to identify column position in grid.
74
     *
75
     * @return int
76
     */
77
    public function getOrder()
78
    {
79
        return $this->order;
80
    }
81
82
    /**
83
     * Sets column order.
84
     *
85
     * This property used to to identify column position in grid.
86
     *
87
     * @param $order
88
     * @return $this
89
     */
90
    public function setOrder($order)
91
    {
92
        $this->order = $order;
93
        return $this;
94
    }
95
96
    /**
97
     * Returns field name.
98
     *
99
     * @return string
100
     */
101
    public function getName()
102
    {
103
        return $this->name;
104
    }
105
106
    /**
107
     * Sets field name.
108
     *
109
     * @param string $name
110
     * @return $this
111
     */
112
    public function setName($name)
113
    {
114
        $this->name = $name;
115
        return $this;
116
    }
117
118
    /**
119
     * Returns true if column is hidden.
120
     *
121
     * @return bool
122
     */
123
    public function isHidden()
124
    {
125
        return $this->is_hidden;
126
    }
127
128
    /**
129
     * Returns true if column is hidden on xs.
130
     *
131
     * @return bool
132
     */
133
    public function isHiddenXs()
134
    {
135
        return $this->is_hiddenXs;
136
    }
137
138
    /**
139
     * Returns true if column is hidden on sm.
140
     *
141
     * @return bool
142
     */
143
    public function isHiddenSm()
144
    {
145
        return $this->is_hiddenSm;
146
    }
147
148
    /**
149
     * Returns true if column is hidden on md.
150
     *
151
     * @return bool
152
     */
153
    public function isHiddenMd()
154
    {
155
        return $this->is_hiddenMd;
156
    }
157
158
159
    /**
160
     * Returns true if column is hidden on lg.
161
     *
162
     * @return bool
163
     */
164
    public function isHiddenLg()
165
    {
166
        return $this->is_hiddenLg;
167
    }
168
169
    /**
170
     * Makes column hidden.
171
     *
172
     * @return $this
173
     */
174
    public function hide()
175
    {
176
        $this->is_hidden = true;
177
        return $this;
178
    }
179
180
    /**
181
     * Makes column hidden on xs screens.
182
     *
183
     * @return $this
184
     */
185
    public function hideXs()
186
    {
187
        $this->is_hiddenXs = true;
188
        return $this;
189
    }
190
191
    /**
192
     * Makes column hidden on sm screens.
193
     *
194
     * @return $this
195
     */
196
    public function hideSm()
197
    {
198
        $this->is_hiddenSm = true;
199
        return $this;
200
    }
201
202
    /**
203
     * Makes column hidden on md screens.
204
     *
205
     * @return $this
206
     */
207
    public function hideMd()
208
    {
209
        $this->is_hiddenMd = true;
210
        return $this;
211
    }
212
213
    /**
214
     * Makes column hidden on lg screens.
215
     *
216
     * @return $this
217
     */
218
    public function hideLg()
219
    {
220
        $this->is_hiddenLg = true;
221
        return $this;
222
    }
223
224
    /**
225
     * Makes column visible.
226
     *
227
     * @return $this
228
     */
229
    public function show()
230
    {
231
        $this->is_hidden = false;
232
        return $this;
233
    }
234
235
    /**
236
     * Returns text label that will be rendered in table header.
237
     *
238
     * @return string
239
     */
240
    public function getLabel()
241
    {
242
        return $this->label ? : ucwords(str_replace(array('-', '_', '.'), ' ', $this->name));
243
    }
244
245
    /**
246
     * Sets text label that will be rendered in table header.
247
     *
248
     * @param string $label
249
     * @return $this
250
     */
251
    public function setLabel($label)
252
    {
253
        $this->label = $label;
254
        return $this;
255
    }
256
257
    /**
258
     * Returns true if column is sortable (sorting controls must be rendered).
259
     *
260
     * @return bool
261
     */
262
    public function isSortable()
263
    {
264
        return $this->is_sortable;
265
    }
266
267
    /**
268
     * Allows to enable or disable sorting controls for column.
269
     *
270
     * @param boolean $isSortable
271
     * @return $this
272
     */
273
    public function setSortable($isSortable)
274
    {
275
        $this->is_sortable = $isSortable;
276
        return $this;
277
    }
278
279
    /**
280
     * Returns current sorting order
281
     * or null if table rows are not sorted using this column.
282
     *
283
     * @return null|string null|Grid::SORT_ASC|Grid::SORT_DESC
284
     */
285
    public function getSorting()
286
    {
287
        return $this->sorting;
288
    }
289
290
    /**
291
     * Allows to specify sorting by this column for data rows.
292
     *
293
     * @param null|string $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC
294
     * @return $this
295
     */
296
    public function setSorting($sortOrder)
297
    {
298
        $this->sorting = $sortOrder;
299
        return $this;
300
    }
301
302
    /**
303
     * Returns true if data rows are sorted ascending using this column.
304
     *
305
     * @return bool
306
     */
307
    public function isSortedAsc()
308
    {
309
        return $this->sorting === Grid::SORT_ASC;
310
    }
311
312
    /**
313
     * Returns true if data rows are sorted descending using this column.
314
     *
315
     * @return bool
316
     */
317
    public function isSortedDesc()
318
    {
319
        return $this->sorting === Grid::SORT_DESC;
320
    }
321
322
    /**
323
     * Allows to set callback function that will render
324
     * content of table cells for this column.
325
     *
326
     * @param callable $callback
327
     * @return $this
328
     */
329
    public function setCallback($callback)
330
    {
331
        $this->callback = $callback;
332
        return $this;
333
    }
334
335
    /**
336
     * Returns function that will render
337
     * content of table cells for this column if specified.
338
     *
339
     * @return callable|null
340
     */
341
    public function getCallback()
342
    {
343
        return $this->callback;
344
    }
345
346
    /**
347
     * Allows to specify filtering controls for column.
348
     *
349
     * @param Collection|FilterConfig[] $filters
350
     * @return $this
351
     */
352
    public function setFilters($filters)
353
    {
354
        $this->filters = Collection::make($filters);
355
        foreach ($this->filters as $filterConfig) {
356
            $filterConfig->attach($this);
357
        }
358
359
        return $this;
360
    }
361
362
    /**
363
     * Allows to add filtering control to column.
364
     *
365
     * @param FilterConfig $filter
366
     * @return $this
367
     */
368
    public function addFilter(FilterConfig $filter)
369
    {
370
        $this->getFilters()->push($filter);
371
        $filter->attach($this);
372
        return $this;
373
    }
374
375
    /**
376
     * Creates instance of filtering control configuration
377
     * and binds it to the column.
378
     *
379
     * @param string $class
380
     * @return FilterConfig
381
     */
382
    public function makeFilter($class = '\Nayjest\Grids\FilterConfig')
383
    {
384
        $filter = new $class;
385
        $this->addFilter($filter);
386
        return $filter;
387
    }
388
389
    /**
390
     * Returns true if any filtering controls specified for the column.
391
     *
392
     * @return bool
393
     */
394
    public function hasFilters()
395
    {
396
        return !$this->getFilters()->isEmpty();
397
    }
398
399
    /**
400
     * Returns list of filtering controls specified for the column.
401
     *
402
     * @return Collection|FilterConfig[]
403
     */
404
    public function getFilters()
405
    {
406
        if (null === $this->filters) {
407
            $this->filters = new Collection();
408
        }
409
        return $this->filters;
410
    }
411
412
    /**
413
     * @todo move to Field instance
414
     * @param DataRowInterface $row
415
     * @return mixed
416
     */
417
    public function getValue(DataRowInterface $row)
418
    {
419
        if ($function = $this->getCallback()) {
420
            return call_user_func($function, $row->getCellValue($this), $row);
421
        } else {
422
            return $row->getCellValue($this);
423
        }
424
    }
425
}
426