1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Form\Columns; |
4
|
|
|
|
5
|
|
|
use Exception; |
6
|
|
|
use KodiComponents\Support\HtmlAttributes; |
7
|
|
|
use SleepingOwl\Admin\Contracts\Form\Columns\ColumnInterface; |
8
|
|
|
use SleepingOwl\Admin\Form\FormElements; |
9
|
|
|
use SleepingOwl\Admin\Traits\Width; |
10
|
|
|
|
11
|
|
|
class Column extends FormElements implements ColumnInterface |
12
|
|
|
{ |
13
|
|
|
use HtmlAttributes, Width; |
14
|
|
|
|
15
|
|
|
/** |
16
|
|
|
* @var int |
17
|
|
|
*/ |
18
|
|
|
protected $size = 'col-md-'; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var string |
22
|
|
|
*/ |
23
|
|
|
protected $view = 'form.element.column'; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @throws \Exception |
27
|
|
|
*/ |
28
|
|
|
public function initialize() |
29
|
|
|
{ |
30
|
|
|
parent::initialize(); |
31
|
|
|
|
32
|
|
|
$this->setHtmlAttribute('class', $this->getClass()); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @return string |
37
|
|
|
*/ |
38
|
|
|
public function getSize() |
39
|
|
|
{ |
40
|
|
|
return $this->size; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param string $size |
45
|
|
|
* |
46
|
|
|
* @return $this |
47
|
|
|
*/ |
48
|
|
|
public function setSize($size) |
49
|
|
|
{ |
50
|
|
|
if (strpos($size, 'col-') === false) { |
51
|
|
|
$size = 'col-'.$size.'-'; |
52
|
|
|
} |
53
|
|
|
|
54
|
|
|
$this->size = $size; |
|
|
|
|
55
|
|
|
|
56
|
|
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @return string |
61
|
|
|
* @throws \Exception |
62
|
|
|
*/ |
63
|
|
|
protected function getClass() |
64
|
|
|
{ |
65
|
|
|
$width = $this->getWidth(); |
66
|
|
|
if (is_numeric($width)) { |
67
|
|
|
$class = $this->getSize().$width; |
68
|
|
|
} elseif (is_array($width) && count($width)) { |
69
|
|
|
$class = implode(' ', $width); |
70
|
|
|
} elseif (is_string($width)) { |
71
|
|
|
$class = $width; |
72
|
|
|
} else { |
73
|
|
|
throw new Exception('Column width should be integer (numeric), string (for example: col-sm-12 col-md-6) or array (list of the Bootstrap classes)'); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
return $class; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
* @throws \Exception |
82
|
|
|
*/ |
83
|
|
|
public function toArray() |
84
|
|
|
{ |
85
|
|
|
return parent::toArray() + [ |
86
|
|
|
'width' => $this->getWidth(), |
87
|
|
|
'elements' => $this->getElements()->onlyVisible(), |
88
|
|
|
'attributes' => $this->htmlAttributesToString(), |
89
|
|
|
]; |
90
|
|
|
} |
91
|
|
|
} |
92
|
|
|
|
This check looks for assignments to scalar types that may be of the wrong type.
To ensure the code behaves as expected, it may be a good idea to add an explicit type cast.