Completed
Pull Request — master (#148)
by
unknown
04:21
created

FieldConfig::getCellHtmlAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
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
49
    /**
50
     * HTML Attributes to add to cells
51
     * [attributeName => value]
52
     * @var array
53
     */
54
    protected $cell_html_attributes = [];
55
56
    /**
57
     * Constructor.
58
     *
59
     * @param string|null $name column unique name for internal usage
60
     * @param string|null $label column label
61
     */
62
    public function __construct($name = null, $label = null)
63
    {
64
        if ($name !== null) {
65
            $this->setName($name);
66
        }
67
        if ($label !== null) {
68
            $this->setLabel($label);
69
        }
70
    }
71
72
    /**
73
     * Returns column order.
74
     *
75
     * This property used to to identify column position in grid.
76
     *
77
     * @return int
78
     */
79
    public function getOrder()
80
    {
81
        return $this->order;
82
    }
83
84
    /**
85
     * Sets column order.
86
     *
87
     * This property used to to identify column position in grid.
88
     *
89
     * @param $order
90
     * @return $this
91
     */
92
    public function setOrder($order)
93
    {
94
        $this->order = $order;
95
        return $this;
96
    }
97
98
    /**
99
     * Returns field name.
100
     *
101
     * @return string
102
     */
103
    public function getName()
104
    {
105
        return $this->name;
106
    }
107
108
    /**
109
     * Sets field name.
110
     *
111
     * @param string $name
112
     * @return $this
113
     */
114
    public function setName($name)
115
    {
116
        $this->name = $name;
117
        return $this;
118
    }
119
120
    /**
121
     * Returns true if column is hidden.
122
     *
123
     * @return bool
124
     */
125
    public function isHidden()
126
    {
127
        return $this->is_hidden;
128
    }
129
130
    /**
131
     * Makes column hidden.
132
     *
133
     * @return $this
134
     */
135
    public function hide()
136
    {
137
        $this->is_hidden = true;
138
        return $this;
139
    }
140
141
    /**
142
     * Makes column visible.
143
     *
144
     * @return $this
145
     */
146
    public function show()
147
    {
148
        $this->is_hidden = false;
149
        return $this;
150
    }
151
152
    /**
153
     * Returns text label that will be rendered in table header.
154
     *
155
     * @return string
156
     */
157
    public function getLabel()
158
    {
159
        return $this->label ? : ucwords(str_replace(array('-', '_', '.'), ' ', $this->name));
160
    }
161
162
    /**
163
     * Sets text label that will be rendered in table header.
164
     *
165
     * @param string $label
166
     * @return $this
167
     */
168
    public function setLabel($label)
169
    {
170
        $this->label = $label;
171
        return $this;
172
    }
173
174
    /**
175
     * Returns true if column is sortable (sorting controls must be rendered).
176
     *
177
     * @return bool
178
     */
179
    public function isSortable()
180
    {
181
        return $this->is_sortable;
182
    }
183
184
    /**
185
     * Allows to enable or disable sorting controls for column.
186
     *
187
     * @param boolean $isSortable
188
     * @return $this
189
     */
190
    public function setSortable($isSortable)
191
    {
192
        $this->is_sortable = $isSortable;
193
        return $this;
194
    }
195
196
    /**
197
     * Returns current sorting order
198
     * or null if table rows are not sorted using this column.
199
     *
200
     * @return null|string null|Grid::SORT_ASC|Grid::SORT_DESC
201
     */
202
    public function getSorting()
203
    {
204
        return $this->sorting;
205
    }
206
207
    /**
208
     * Allows to specify sorting by this column for data rows.
209
     *
210
     * @param null|string $sortOrder null|Grid::SORT_ASC|Grid::SORT_DESC
211
     * @return $this
212
     */
213
    public function setSorting($sortOrder)
214
    {
215
        $this->sorting = $sortOrder;
216
        return $this;
217
    }
218
219
    /**
220
     * Returns true if data rows are sorted ascending using this column.
221
     *
222
     * @return bool
223
     */
224
    public function isSortedAsc()
225
    {
226
        return $this->sorting === Grid::SORT_ASC;
227
    }
228
229
    /**
230
     * Returns true if data rows are sorted descending using this column.
231
     *
232
     * @return bool
233
     */
234
    public function isSortedDesc()
235
    {
236
        return $this->sorting === Grid::SORT_DESC;
237
    }
238
239
    /**
240
     * Allows to set callback function that will render
241
     * content of table cells for this column.
242
     *
243
     * @param callable $callback
244
     * @return $this
245
     */
246
    public function setCallback($callback)
247
    {
248
        $this->callback = $callback;
249
        return $this;
250
    }
251
252
    /**
253
     * Returns function that will render
254
     * content of table cells for this column if specified.
255
     *
256
     * @return callable|null
257
     */
258
    public function getCallback()
259
    {
260
        return $this->callback;
261
    }
262
263
    /**
264
     * Allows to specify filtering controls for column.
265
     *
266
     * @param Collection|FilterConfig[] $filters
267
     * @return $this
268
     */
269
    public function setFilters($filters)
270
    {
271
        $this->filters = Collection::make($filters);
272
        foreach ($this->filters as $filterConfig) {
273
            $filterConfig->attach($this);
274
        }
275
276
        return $this;
277
    }
278
279
    /**
280
     * Allows to add filtering control to column.
281
     *
282
     * @param FilterConfig $filter
283
     * @return $this
284
     */
285
    public function addFilter(FilterConfig $filter)
286
    {
287
        $this->getFilters()->push($filter);
288
        $filter->attach($this);
289
        return $this;
290
    }
291
292
    /**
293
     * Creates instance of filtering control configuration
294
     * and binds it to the column.
295
     *
296
     * @param string $class
297
     * @return FilterConfig
298
     */
299
    public function makeFilter($class = '\Nayjest\Grids\FilterConfig')
300
    {
301
        $filter = new $class;
302
        $this->addFilter($filter);
303
        return $filter;
304
    }
305
306
    /**
307
     * Returns true if any filtering controls specified for the column.
308
     *
309
     * @return bool
310
     */
311
    public function hasFilters()
312
    {
313
        return !$this->getFilters()->isEmpty();
314
    }
315
316
    /**
317
     * Returns list of filtering controls specified for the column.
318
     *
319
     * @return Collection|FilterConfig[]
320
     */
321
    public function getFilters()
322
    {
323
        if (null === $this->filters) {
324
            $this->filters = new Collection();
325
        }
326
        return $this->filters;
327
    }
328
329
    /**
330
     * @todo move to Field instance
331
     * @param DataRowInterface $row
332
     * @return mixed
333
     */
334
    public function getValue(DataRowInterface $row)
335
    {
336
        if ($function = $this->getCallback()) {
337
            return call_user_func($function, $row->getCellValue($this), $row);
338
        } else {
339
            return $row->getCellValue($this);
340
        }
341
    }
342
343
    /**
344
     * @return array
345
     */
346
    public function getCellHtmlAttributes()
347
    {
348
        return $this->cell_html_attributes;
349
    }
350
351
    /**
352
     * @param array $cell_html_attributes
353
     */
354
    public function setCellHtmlAttributes($cell_html_attributes)
355
    {
356
        $this->cell_html_attributes = $cell_html_attributes;
357
        return $this;
358
    }
359
}
360