1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace Pfilsx\DataGrid\Grid\Columns; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Pfilsx\DataGrid\DataGridException; |
8
|
|
|
|
9
|
|
|
class DataColumn extends AbstractColumn |
10
|
|
|
{ |
11
|
|
|
protected $attribute; |
12
|
|
|
protected $format = 'raw'; |
13
|
|
|
protected $sort = true; |
14
|
|
|
protected $emptyValue = ''; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @param mixed $attribute |
18
|
|
|
*/ |
19
|
|
|
protected function setAttribute($attribute): void |
20
|
|
|
{ |
21
|
|
|
$this->attribute = $attribute; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @return null|string |
26
|
|
|
*/ |
27
|
|
|
public function getAttribute() |
28
|
|
|
{ |
29
|
|
|
return $this->attribute; |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function getLabel(): string |
36
|
|
|
{ |
37
|
|
|
return $this->label === false |
|
|
|
|
38
|
|
|
? '' |
39
|
|
|
: (!empty($this->label) ? $this->label : $this->attribute); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return string |
44
|
|
|
*/ |
45
|
|
|
public function getFormat(): string |
46
|
|
|
{ |
47
|
|
|
return $this->format; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $format |
52
|
|
|
*/ |
53
|
|
|
protected function setFormat(string $format): void |
54
|
|
|
{ |
55
|
|
|
$this->format = strtolower($format); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
public function getEmptyValue() |
59
|
|
|
{ |
60
|
|
|
return $this->emptyValue; |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
protected function setEmptyValue($value): void |
64
|
|
|
{ |
65
|
|
|
$this->emptyValue = $value; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
protected function checkConfiguration() |
69
|
|
|
{ |
70
|
|
|
if ((!is_string($this->attribute) || empty($this->attribute)) && $this->value === null) { |
71
|
|
|
throw new DataGridException('attribute or value property must be set for ' . static::class); |
72
|
|
|
} |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
public function hasFilter() |
76
|
|
|
{ |
77
|
|
|
return parent::hasFilter() && !empty($this->attribute); |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
public function getHeadContent() |
81
|
|
|
{ |
82
|
|
|
$label = $this->getLabel(); |
83
|
|
|
if (($translator = $this->container->getTranslator()) !== null) { |
84
|
|
|
$label = $translator->trans($label, [], $this->translationDomain); |
85
|
|
|
} |
86
|
|
|
return ucfirst($label); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function getFilterContent() |
90
|
|
|
{ |
91
|
|
|
if ($this->hasFilter()) { |
92
|
|
|
return $this->filter->render($this->attribute, $this->filterValue); |
93
|
|
|
} |
94
|
|
|
return ''; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
public function getCellContent($entity) |
98
|
|
|
{ |
99
|
|
|
$result = (string)$this->getCellValue($entity); |
100
|
|
|
return empty($result) |
101
|
|
|
? $this->emptyValue |
102
|
|
|
: ($this->format === 'html' ? $result : htmlspecialchars($result)); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
protected function getCellValue($entity) |
106
|
|
|
{ |
107
|
|
|
if (is_callable($this->value)) { |
108
|
|
|
return call_user_func_array($this->value, [$entity]); |
109
|
|
|
} elseif ($this->value !== null) { |
110
|
|
|
return $this->value; |
111
|
|
|
} else { |
112
|
|
|
return $entity->{$this->attribute}; |
113
|
|
|
} |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|