Completed
Push — master ( e6e660...2d183f )
by Pavel
02:25
created

Column::getName()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

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 2
nc 1
nop 0
1
<?php
2
3
/**
4
 * @copyright   Copyright (c) 2015 ublaboo <[email protected]>
5
 * @author      Pavel Janda <[email protected]>
6
 * @package     Ublaboo
7
 */
8
9
namespace Ublaboo\DataGrid\Column;
10
11
use Ublaboo\DataGrid\DataGridException;
12
use Ublaboo;
13
use Ublaboo\DataGrid\Row;
14
15
abstract class Column extends Ublaboo\DataGrid\Object
16
{
17
18
	/**
19
	 * @var string
20
	 */
21
	protected $column;
22
23
	/**
24
	 * @var string
25
	 */
26
	protected $name;
27
28
	/**
29
	 * @var array
30
	 */
31
	protected $replacements = [];
32
33
	/**
34
	 * @var Renderer|NULL
35
	 */
36
	protected $renderer;
37
38
	/**
39
	 * @var string
40
	 */
41
	protected $template;
42
43
	/**
44
	 * @var boolean
45
	 */
46
	protected $is_sortable = FALSE;
47
48
	/**
49
	 * @var array
50
	 */
51
	protected $sort;
52
53
	/**
54
	 * @var bool
55
	 */
56
	protected $template_escaping = TRUE;
57
58
	/**
59
	 * @var string
60
	 */
61
	protected $align;
62
63
	/**
64
	 * @var array
65
	 */
66
	protected $template_variables;
67
68
	/**
69
	 * @var callable
70
	 */
71
	protected $editable_callback;
72
73
74
	/**
75
	 * @param string $column
76
	 * @param string $name
77
	 */
78
	public function __construct($column, $name)
79
	{
80
		$this->column = $column;
81
		$this->name = $name;
82
	}
83
84
85
	/**
86
	 * Render row item into template
87
	 * @param  Row   $row
88
	 * @return mixed
89
	 */
90
	public function render(Row $row)
91
	{
92
		/**
93
		 * Renderer function may be used
94
		 */
95 View Code Duplication
		if ($renderer = $this->getRenderer()) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96
			if (!$renderer->getConditionCallback()) {
97
				return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
98
			}
99
100
			if (call_user_func_array($renderer->getConditionCallback(), [$row->getItem()])) {
101
				return call_user_func_array($renderer->getCallback(), [$row->getItem()]);
102
			}
103
		}
104
105
		/**
106
		 * Or replacements may be applied
107
		 */
108
		list($do_replace, $replaced) = $this->applyReplacements($row);
109
		if ($do_replace) {
110
			return $replaced;
111
		}
112
113
		return $this->getColumnValue($row);
114
	}
115
116
117
	/**
118
	 * Should be column values escaped in latte?
119
	 * @param boolean $template_escaping
120
	 */
121
	public function setTemplateEscaping($template_escaping = TRUE)
122
	{
123
		$this->template_escaping = (bool) $template_escaping;
124
125
		return $this;
126
	}
127
128
129
	public function isTemplateEscaped()
130
	{
131
		return $this->template_escaping;
132
	}
133
134
135
	/**
136
	 * Set column sortable or not
137
	 * @param bool $sortable
138
	 */
139
	public function setSortable($sortable = TRUE)
140
	{
141
		$this->is_sortable = (bool) $sortable;
142
143
		return $this;
144
	}
145
146
147
	/**
148
	 * Tell whether column is sortable
149
	 * @return boolean
150
	 */
151
	public function isSortable()
152
	{
153
		return $this->is_sortable;
154
	}
155
156
157
	/**
158
	 * Get column name
159
	 * @return string
160
	 */
161
	public function getColumnName()
162
	{
163
		return $this->column;
164
	}
165
166
167
	/**
168
	 * Get column value of row item
169
	 * @param  Row   $row
170
	 * @return mixed
171
	 */
172
	public function getColumnValue(Row $row)
173
	{
174
		return $row->getValue($this->column);
175
	}
176
177
178
	public function getName()
179
	{
180
		return $this->name;
181
	}
182
183
184
	/**
185
	 * Set column replacements
186
	 * @param  array $replacements
187
	 * @return Column
188
	 */
189
	public function setReplacement(array $replacements)
190
	{
191
		$this->replacements = $replacements;
192
193
		return $this;
194
	}
195
196
197
	/**
198
	 * Tell whether columns has replacements
199
	 * @return boolean
200
	 */
201
	public function hasReplacements()
202
	{
203
		return (bool) $this->replacements;
204
	}
205
206
207
	/**
208
	 * Apply replacements
209
	 * @param  Row   $row
210
	 * @return array
211
	 */
212
	public function applyReplacements(Row $row)
213
	{
214
		$value = $row->getValue($this->column);
215
216
		if ((is_scalar($value) || is_null($value)) && isset($this->replacements[$value])) {
217
			return [TRUE, $this->replacements[$value]];
218
		}
219
220
		return [FALSE, NULL];
221
	}
222
223
224
	/**
225
	 * Set renderer callback and (it may be optional - the condition callback will decide)
226
	 * @param callable $renderer
227
	 */
228
	public function setRenderer($renderer, $condition_callback = NULL)
229
	{
230
		if ($this->hasReplacements()) {
231
			throw new DataGridException(
232
				"Use either Column::setReplacement() or Column::setRenderer, not both."
233
			);
234
		}
235
236
		if (!is_callable($renderer)) {
237
			throw new DataGridException(
238
				"Renderer (method Column::setRenderer()) must be callable."
239
			);
240
		}
241
242
		if (NULL != $condition_callback && !is_callable($condition_callback)) {
243
			throw new DataGridException(
244
				"Renderer (method Column::setRenderer()) must be callable."
245
			);
246
		}
247
248
		$this->renderer = new Renderer($renderer, $condition_callback);
249
250
		return $this;
251
	}
252
253
254
	/**
255
	 * Set renderer callback just if condition is truthy
256
	 * @param callable $renderer
257
	 */
258
	public function setRendererOnCondition($renderer, $condition_callback)
259
	{
260
		return $this->setRenderer($renderer, $condition_callback);
261
	}
262
263
264
	/**
265
	 * Return custom renderer callback
266
	 * @return Renderer|null
267
	 */
268
	public function getRenderer()
269
	{
270
		return $this->renderer;
271
	}
272
273
274
	/**
275
	 * Column may have its own template
276
	 * @param string $template
277
	 */
278
	public function setTemplate($template, array $template_variables = [])
279
	{
280
		$this->template = $template;
281
		$this->template_variables = $template_variables;
282
283
		return $this;
284
	}
285
286
287
	/**
288
	 * Column can have variables that will be passed to custom template scope
289
	 * @return array
290
	 */
291
	public function getTemplateVariables()
292
	{
293
		return $this->template_variables;
294
	}
295
296
297
	/**
298
	 * Tell whether column has its owntemplate
299
	 * @return bool
300
	 */
301
	public function hasTemplate()
302
	{
303
		return (bool) $this->template;
304
	}
305
306
307
	/**
308
	 * Get column template path
309
	 * @return string
310
	 */
311
	public function getTemplate()
312
	{
313
		return $this->template;
314
	}
315
316
317
	/**
318
	 * Tell whether data source is sorted by this collumn
319
	 * @return boolean
320
	 */
321
	public function isSortedBy()
322
	{
323
		return (bool) $this->sort;
324
	}
325
326
327
	/**
328
	 * Tell column his sorting options
329
	 * @param array $sort
330
	 */
331
	public function setSort(array $sort)
332
	{
333
		$this->sort = $sort[$this->column];
334
335
		return $this;
336
	}
337
338
339
	/**
340
	 * What sorting will be applied after next click?
341
	 * @return array
342
	 */
343
	public function getSortNext()
344
	{
345
		if ($this->sort == 'ASC') {
346
			return [$this->column => 'DESC'];
347
		} else if ($this->sort == 'DESC') {
348
			return [$this->column => NULL];
349
		}
350
351
		return [$this->column => 'ASC'];
352
	}
353
354
355
	/**
356
	 * Is sorting ascending?
357
	 * @return boolean
358
	 */
359
	public function isSortAsc()
360
	{
361
		return $this->sort == 'ASC';
362
	}
363
364
365
	/**
366
	 * Set column alignment
367
	 * @param string $align
368
	 */
369
	public function setAlign($align)
370
	{
371
		$this->align = (string) $align;
372
373
		return $this;
374
	}
375
376
377
	/**
378
	 * Has column some alignment?
379
	 * @return boolean [description]
380
	 */
381
	public function hasAlign()
382
	{
383
		return (bool) $this->align;
384
	}
385
386
387
	/**
388
	 * Get column alignment
389
	 * @return string
390
	 */
391
	public function getAlign()
392
	{
393
		return $this->align;
394
	}
395
396
397
	/**
398
	 * Set callback that will be called after inline editing
399
	 * @param callable $editable_callback
400
	 */
401
	public function setEditableCallback(callable $editable_callback)
402
	{
403
		$this->editable_callback = $editable_callback;
404
405
		return $this;
406
	}
407
408
409
	/**
410
	 * Return callback that is used after inline editing
411
	 * @return callable
412
	 */
413
	public function getEditableCallback()
414
	{
415
		return $this->editable_callback;
416
	}
417
418
419
	/**
420
	 * Is column editable?
421
	 * @return boolean
422
	 */
423
	public function isEditable()
424
	{
425
		return (bool) $this->getEditableCallback();
426
	}
427
428
}
429