Completed
Pull Request — master (#173)
by
unknown
03:28
created

FieldConfig::addStyleAttribute()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

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

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
338
        return $this;
339
    }
340
341
    public function getStyleAttributes(){
342
        return $this->styleAttributes;
343
    }
344
345
    public function setFormat($format){
346
        $this->format = $format;
0 ignored issues
show
Bug introduced by
The property format does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
347
    }
348
349
    public function getFormat(){
350
        return $this->format;
351
    }
352
}
353