Completed
Push — master ( fb0957...b281c2 )
by Sébastien
16:16
created

Column::getAlign()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Soluble\FlexStore\Column;
4
5
use Soluble\FlexStore\Column\Type\AbstractType;
6
use Soluble\FlexStore\Formatter\FormatterInterface;
7
8
class Column implements ColumnSettableInterface
9
{
10
    /**
11
     *
12
     * @var string
13
     */
14
    protected $name;
15
16
    /**
17
     *
18
     * @var array
19
     */
20
    protected $properties = [
21
        'type' => null, // will be defaulted to string
22
        'formatter' => null,
23
        'excluded' => false,
24
        'hidden' => false,
25
        'width' => null,
26
        'header' => null, // will be defaulted to name
27
        'filterable' => true,
28
        'groupable' => true,
29
        'sortable' => true,
30
        'editable' => false,
31
        'virtual' => true,
32
        'align' => null,
33
        'class' => null
34
    ];
35
36
37
    /**
38
     * Constructor
39
     * @param string $name unique identifier name for the column
40
     * @param array $properties associative array with (header,width,filterable,groupable,sortable,hidden,excluded,editable...)
41 38
     * @throws Exception\InvalidArgumentException
42
     */
43 38
    public function __construct($name, array $properties = null)
44 3
    {
45
        if (!is_string($name)) {
46 37
            throw new Exception\InvalidArgumentException(__METHOD__ . " Column name must be a string");
47 1
        }
48
        if (trim($name) == '') {
49 36
            throw new Exception\InvalidArgumentException(__METHOD__ . " Column name cannot be empty");
50 36
        }
51 7
        $this->name = $name;
52 6
        if ($properties !== null) {
53 35
            $this->setProperties($properties);
54 35
        }
55
        $this->initDefaults();
56
    }
57
58
    /**
59
     * This method ensure some properties are defaulted.
60 35
     * For example header with name and type is string
61
     */
62 35
    protected function initDefaults()
63 33
    {
64 33
        if ($this->properties['header'] == '') {
65 35
            $this->setHeader($this->name);
66 33
        }
67 33
        if ($this->properties['type'] == '') {
68 35
            $this->setType(ColumnType::createType(ColumnType::TYPE_STRING));
69
        }
70
    }
71
72
    /**
73
     * Get the name of the column
74
     *
75 31
     * @return string
76
     */
77 31
    public function getName()
78
    {
79
        return $this->name;
80
    }
81
82
    /**
83
     * Set column datatype
84
     * @param string|AbstractType $type
85
     * @throws Exception\InvalidArgumentException when the type is not supported.
86 35
     * @return Column
87
     */
88 35
    public function setType($type)
89 10
    {
90 35
        if (is_string($type)) {
91
            $type = ColumnType::createType($type);
92
        } elseif (!$type instanceof AbstractType) {
93 35
            throw new Exception\InvalidArgumentException(__METHOD__ . " setType() accepts only AbstractType or string.");
94 35
        }
95
        $this->properties['type'] = $type;
96
        return $this;
97
    }
98
99
100
    /**
101
     *
102 4
     * @return AbstractType
103
     */
104 4
    public function getType()
105
    {
106
        return $this->properties['type'];
107
    }
108
109
    /**
110
     *
111
     * @param FormatterInterface $formatter
112 4
     * @return Column
113
     */
114 4
    public function setFormatter(FormatterInterface $formatter)
115 4
    {
116
        $this->properties['formatter'] = $formatter;
117
        return $this;
118
    }
119
120
    /**
121
     *
122 11
     * @return FormatterInterface|null
123
     */
124 11
    public function getFormatter()
125
    {
126
        return $this->properties['formatter'];
127
    }
128
129
    /**
130
     *
131
     * @param boolean $virtual
132 31
     * @return Column
133
     */
134 31
    public function setVirtual($virtual = true)
135 31
    {
136
        $this->properties['virtual'] = (bool) $virtual;
137
        return $this;
138
    }
139
140
    /**
141
     *
142 5
     * @return boolean
143
     */
144 5
    public function isVirtual()
145
    {
146
        return $this->properties['virtual'];
147
    }
148
149
150
    /**
151
     *
152
     * @param boolean $excluded
153 10
     * @return Column
154
     */
155 10
    public function setExcluded($excluded = true)
156 10
    {
157
        $this->properties['excluded'] = (bool) $excluded;
158
        return $this;
159
    }
160
161
    /**
162
     *
163 21
     * @return boolean
164
     */
165 21
    public function isExcluded()
166
    {
167
        return $this->properties['excluded'];
168
    }
169
170
    /**
171
     *
172
     * @param boolean $editable
173 4
     * @return Column
174
     */
175 4
    public function setEditable($editable = true)
176 4
    {
177
        $this->properties['editable'] = (bool) $editable;
178
        return $this;
179
    }
180
181
    /**
182
     *
183 4
     * @return boolean
184
     */
185 4
    public function isEditable()
186
    {
187
        return $this->properties['editable'];
188
    }
189
190
    /**
191
     *
192
     * @param boolean $hidden
193 4
     * @return Column
194
     */
195 4
    public function setHidden($hidden = true)
196 4
    {
197
        $this->properties['hidden'] = (bool) $hidden;
198
        return $this;
199
    }
200
201
    /**
202
     *
203 3
     * @return boolean
204
     */
205 3
    public function isHidden()
206
    {
207
        return (bool) $this->properties['hidden'];
208
    }
209
210
    /**
211
     *
212
     * @param boolean $sortable
213 4
     * @return Column
214
     */
215 4
    public function setSortable($sortable = true)
216 4
    {
217
        $this->properties['sortable'] = (bool) $sortable;
218
        return $this;
219
    }
220
221
    /**
222
     *
223 4
     * @return boolean
224
     */
225 4
    public function isSortable()
226
    {
227
        return (bool) $this->properties['sortable'];
228
    }
229
230
    /**
231
     *
232
     * @param boolean $groupable
233 4
     * @return Column
234
     */
235 4
    public function setGroupable($groupable = true)
236 4
    {
237
        $this->properties['groupable'] = (bool) $groupable;
238
        return $this;
239
    }
240
241
    /**
242
     *
243 4
     * @return boolean
244
     */
245 4
    public function isGroupable()
246
    {
247
        return (bool) $this->properties['groupable'];
248
    }
249
250
    /**
251
     *
252
     * @param boolean $filterable
253 4
     * @return Column
254
     */
255 4
    public function setFilterable($filterable = true)
256 4
    {
257
        $this->properties['filterable'] = (bool) $filterable;
258
        return $this;
259
    }
260
261
    /**
262
     *
263 4
     * @return boolean
264
     */
265 4
    public function isFilterable()
266
    {
267
        return (bool) $this->properties['filterable'];
268
    }
269
270
    
271
    /**
272
     * Set recommended width for the column
273
     *
274
     * @throws Exception\InvalidArgumentException
275 5
     * @param float|int|string $width
276
     * @return Column
277 5
     */
278 1
    public function setWidth($width)
279
    {
280 4
        if (!is_scalar($width)) {
281 4
            throw new Exception\InvalidArgumentException(__METHOD__ . " Width parameter must be scalar.");
282
        }
283
        $this->properties['width'] = $width;
284
        return $this;
285
    }
286
287
    /**
288 4
     *
289
     * @return float|int|string
290 4
     */
291
    public function getWidth()
292
    {
293
        return $this->properties['width'];
294
    }
295
296
    /**
297
     * Set table header for this column
298
     *
299
     * @throws Exception\InvalidArgumentException
300 36
     * @param string|null $header
301
     * @return Column
302 36
     */
303 36
    public function setHeader($header)
304
    {
305
        $this->properties['header'] = $header;
306
        return $this;
307
    }
308
309
    /**
310 4
     *
311
     * @return string|null
312 4
     */
313
    public function getHeader()
314
    {
315
        return $this->properties['header'];
316
    }
317
    
318
    /**
319 2
     * Set recommended horizontal align 
320
     *
321 2
     * @throws Exception\InvalidArgumentException
322
     * @param string $align can be left|center|right
323
     * @return Column
324
     */
325
    public function setAlign($align)
326
    {
327
        if (!is_string($align) || ! in_array(strtolower($align), ['left', 'right', 'center'])) {
328
            throw new Exception\InvalidArgumentException(__METHOD__ . " Align param must be a string : right, left, center.");
329
        }
330
        $this->properties['align'] = $align;
331 8
        return $this;
332
    }
333 8
334 8
    /**
335 8
     * Return recommended horizontal alignment
336 8
     * @return string|null
337 8
     */
338 2
    public function getAlign()
339
    {
340 8
        return $this->properties['align'];
341 6
    }    
342
343
    /**
344
     * Set recommended css class
345
     *
346
     * @throws Exception\InvalidArgumentException
347
     * @param string $class css class
348 5
     * @return Column
349
     */
350 5
    public function setClass($class)
351
    {
352
        if (!is_string($class)) {
353
            throw new Exception\InvalidArgumentException(__METHOD__ . " Class param must be a string");
354
        }
355
        $this->properties['class'] = $class;
356
        return $this;
357
    }
358
359
    /**
360
     * Return recommended css class
361
     * @return string|null
362
     */
363
    public function getClass()
364
    {
365
        return $this->properties['class'];
366
    }        
367
    
368
    /**
369
     *
370
     * @return array
371
     */
372
    public function getProperties()
373
    {
374
        return $this->properties;
375
    }
376
377
    /**
378
     * Set properties for the column
379
     *
380
     * @throws Exception\InvalidArgumentException
381
     * @param array $properties associative array with (header,width,filterable,groupable,sortable,hidden,excluded,editable...)
382
     * @return Column
383
     */
384
    public function setProperties(array $properties)
385
    {
386
        foreach ($properties as $key => $value) {
387
            if (array_key_exists($key, $this->properties)) {
388
                $method = 'set' . ucfirst($key);
389
                $this->$method($value);
390
            } else {
391
                throw new Exception\InvalidArgumentException(__METHOD__ . " property '$key' is not supported in column.");
392
            }
393
        }
394
        return $this;
395
    }
396
397
    /**
398
     *
399
     * @return string
400
     */
401
    public function __toString()
402
    {
403
        return $this->name;
404
    }
405
}
406