1 | <?php |
||
2 | |||
3 | namespace ChurakovMike\EasyGrid\Columns; |
||
4 | |||
5 | use ChurakovMike\EasyGrid\Filters\BaseFilter; |
||
6 | use ChurakovMike\EasyGrid\Filters\StubFilter; |
||
7 | use ChurakovMike\EasyGrid\Filters\TextFilter; |
||
8 | use ChurakovMike\EasyGrid\Formatters\BaseFormatter; |
||
9 | use ChurakovMike\EasyGrid\Formatters\HtmlFormatter; |
||
10 | use ChurakovMike\EasyGrid\Formatters\ImageFormatter; |
||
11 | use ChurakovMike\EasyGrid\Formatters\StubFormatter; |
||
12 | use ChurakovMike\EasyGrid\Formatters\TextFormatter; |
||
13 | use ChurakovMike\EasyGrid\Formatters\UrlFormatter; |
||
14 | use ChurakovMike\EasyGrid\Interfaces\Formattable; |
||
15 | use ChurakovMike\EasyGrid\Traits\Configurable; |
||
16 | |||
17 | /** |
||
18 | * Class BaseColumn. |
||
19 | * @package ChurakovMike\EasyGrid\Columns |
||
20 | * |
||
21 | * @property string $label |
||
22 | * @property string $attribute |
||
23 | * @property string|\Closure|mixed $value |
||
24 | * @property BaseFilter|mixed $filter |
||
25 | * @property string|BaseFormatter $format |
||
26 | * @property string $width |
||
27 | */ |
||
28 | abstract class BaseColumn |
||
29 | { |
||
30 | use Configurable; |
||
31 | |||
32 | /** |
||
33 | * @var string $label |
||
34 | */ |
||
35 | public $label; |
||
36 | |||
37 | /** |
||
38 | * @var string $attribute |
||
39 | */ |
||
40 | public $attribute; |
||
41 | |||
42 | /** |
||
43 | * @var string $value |
||
44 | */ |
||
45 | public $value; |
||
46 | |||
47 | /** |
||
48 | * @var string $filter |
||
49 | */ |
||
50 | public $filter; |
||
51 | |||
52 | /** |
||
53 | * @var string|Formattable $format |
||
54 | */ |
||
55 | public $format; |
||
56 | |||
57 | /** |
||
58 | * @var string $width |
||
59 | */ |
||
60 | public $width; |
||
61 | |||
62 | /** |
||
63 | * BaseColumn constructor. |
||
64 | * @param array $config |
||
65 | */ |
||
66 | public function __construct(array $config) |
||
67 | { |
||
68 | $this->loadConfig($config); |
||
69 | $this->buildFilter($config); |
||
70 | $this->buildFormatter(); |
||
71 | } |
||
72 | |||
73 | /** |
||
74 | * @param $row |
||
75 | * @return string |
||
76 | */ |
||
77 | public function getValue($row) |
||
78 | { |
||
79 | } |
||
80 | |||
81 | /** |
||
82 | * Render row attribute value. |
||
83 | * |
||
84 | * @param $row |
||
85 | * @return mixed |
||
86 | */ |
||
87 | public function render($row) |
||
88 | { |
||
89 | return $this->formatTo($this->getValue($row)); |
||
90 | } |
||
91 | |||
92 | /** |
||
93 | * Format value with formatter. |
||
94 | * |
||
95 | * @param $value |
||
96 | * @return mixed |
||
97 | */ |
||
98 | public function formatTo($value) |
||
99 | { |
||
100 | return $this->format->format($value); |
||
101 | } |
||
102 | |||
103 | /** |
||
104 | * Get title for grid head. |
||
105 | * |
||
106 | * @return string |
||
107 | */ |
||
108 | public function getLabel(): string |
||
109 | { |
||
110 | return $this->label ?? ucfirst($this->attribute); |
||
111 | } |
||
112 | |||
113 | /** |
||
114 | * Get attribute. |
||
115 | * |
||
116 | * @return string|null |
||
117 | */ |
||
118 | public function getAttribute() |
||
119 | { |
||
120 | return $this->attribute; |
||
121 | } |
||
122 | |||
123 | /** |
||
124 | * Build filter for grid. |
||
125 | * |
||
126 | * @return void |
||
127 | */ |
||
128 | protected function buildFilter($config) |
||
129 | { |
||
130 | if ($this->filter === false) { |
||
131 | $this->filter = new StubFilter([]); |
||
132 | return; |
||
133 | } |
||
134 | |||
135 | if (is_null($this->filter)) { |
||
136 | $this->filter = new TextFilter([ |
||
137 | 'name' => $this->getAttribute(), |
||
138 | ]); |
||
139 | return; |
||
140 | } |
||
141 | |||
142 | if (is_string($this->filter)) { |
||
143 | $this->filter = new $this->filter([ |
||
144 | 'name' => $this->getAttribute(), |
||
145 | 'value' => $config['filterData'], |
||
146 | ]); |
||
147 | $this->format = new StubFormatter(); |
||
148 | } |
||
149 | } |
||
150 | |||
151 | /** |
||
152 | * Build row data formatter. |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | protected function buildFormatter() |
||
157 | { |
||
158 | if (is_null($this->format)) { |
||
159 | $this->format = new TextFormatter(); |
||
160 | |||
161 | return; |
||
162 | } |
||
163 | |||
164 | if (is_object($this->format) && ($this->format instanceof Formattable)) { |
||
165 | return; |
||
166 | } else { |
||
167 | //todo:add another filters. |
||
168 | } |
||
169 | |||
170 | if (is_string($this->format)) { |
||
171 | switch ($this->format) { |
||
172 | case 'text': |
||
173 | $this->format = new TextFormatter(); |
||
174 | break; |
||
175 | case 'html': |
||
176 | $this->format = new HtmlFormatter(); |
||
177 | break; |
||
178 | case 'image': |
||
179 | $this->format = new ImageFormatter(); |
||
180 | break; |
||
181 | case 'url': |
||
182 | $this->format = new UrlFormatter(); |
||
183 | break; |
||
184 | default: |
||
185 | $this->format = new TextFormatter(); |
||
186 | break; |
||
187 | } |
||
188 | |||
189 | return; |
||
190 | } |
||
191 | } |
||
192 | |||
193 | /** |
||
194 | * @return string |
||
195 | */ |
||
196 | public function renderHtmlOptions(): string |
||
197 | { |
||
198 | if (!is_null($this->width)) { |
||
0 ignored issues
–
show
introduced
by
![]() |
|||
199 | return "width='{$this->width}'%"; |
||
200 | } |
||
201 | |||
202 | return ''; |
||
203 | } |
||
204 | } |
||
205 |