|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hamburgscleanest\DataTables\Models; |
|
4
|
|
|
|
|
5
|
|
|
use hamburgscleanest\DataTables\Interfaces\ColumnFormatter; |
|
6
|
|
|
|
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Class Header |
|
10
|
|
|
* @package hamburgscleanest\hamburgscleanest\DataTables\Models |
|
11
|
|
|
*/ |
|
12
|
|
|
class Column { |
|
13
|
|
|
|
|
14
|
|
|
/** @var string */ |
|
15
|
|
|
private $_name; |
|
16
|
|
|
|
|
17
|
|
|
/** @var string */ |
|
18
|
|
|
private $_relation; |
|
19
|
|
|
|
|
20
|
|
|
/** @var string */ |
|
21
|
|
|
private $_aggregate = 'first'; |
|
22
|
|
|
|
|
23
|
|
|
/** @var ColumnFormatter */ |
|
24
|
|
|
private $_formatter; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Column constructor. |
|
28
|
|
|
* @param string $name |
|
29
|
|
|
* @param ColumnFormatter|null $columnFormatter |
|
30
|
|
|
*/ |
|
31
|
38 |
|
public function __construct(string $name, ? ColumnFormatter $columnFormatter = null) |
|
32
|
|
|
{ |
|
33
|
38 |
|
$this->setName($this->_extractAggregate($name)); |
|
34
|
38 |
|
$this->_formatter = $columnFormatter; |
|
35
|
38 |
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* @param string $name |
|
39
|
|
|
* @return string |
|
40
|
|
|
*/ |
|
41
|
38 |
|
private function _extractAggregate(string $name): string |
|
42
|
|
|
{ |
|
43
|
38 |
|
$replaced = 0; |
|
44
|
38 |
|
$name = preg_replace("/\((.*?)\)/", '#$1', $name, 1, $replaced); |
|
|
|
|
|
|
45
|
38 |
|
if ($replaced === 1) |
|
46
|
|
|
{ |
|
47
|
2 |
|
$parts = \explode('#', $name); |
|
48
|
2 |
|
$this->_aggregate = \mb_strtolower($parts[0]); |
|
49
|
2 |
|
$name = $parts[1]; |
|
|
|
|
|
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
38 |
|
return $name; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
3 |
|
public function getAggregate(): string |
|
59
|
|
|
{ |
|
60
|
3 |
|
return $this->_aggregate; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
31 |
|
public function getName(): string |
|
67
|
|
|
{ |
|
68
|
31 |
|
return $this->_name; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param string $name |
|
73
|
|
|
*/ |
|
74
|
38 |
|
public function setName(string $name) |
|
75
|
|
|
{ |
|
76
|
38 |
|
$posDivider = \mb_strpos($name, '.'); |
|
77
|
38 |
|
if ($posDivider === false) |
|
78
|
|
|
{ |
|
79
|
36 |
|
$this->_name = $name; |
|
80
|
|
|
|
|
81
|
36 |
|
return; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
4 |
|
$this->_name = \mb_substr($name, $posDivider + 1); |
|
85
|
4 |
|
$this->_relation = \mb_substr($name, 0, $posDivider); |
|
86
|
4 |
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* @return string |
|
90
|
|
|
*/ |
|
91
|
4 |
|
public function getRelation(): string |
|
92
|
|
|
{ |
|
93
|
4 |
|
return $this->_relation; |
|
94
|
|
|
} |
|
95
|
|
|
|
|
96
|
|
|
/** |
|
97
|
|
|
* @return bool |
|
98
|
|
|
*/ |
|
99
|
31 |
|
public function isRelation(): bool |
|
100
|
|
|
{ |
|
101
|
31 |
|
return !empty($this->_relation); |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* @param ColumnFormatter $columnFormatter |
|
106
|
|
|
* @return Column |
|
107
|
|
|
*/ |
|
108
|
2 |
|
public function setFormatter(ColumnFormatter $columnFormatter): Column |
|
109
|
|
|
{ |
|
110
|
2 |
|
$this->_formatter = $columnFormatter; |
|
111
|
|
|
|
|
112
|
2 |
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* Formats the column data. |
|
117
|
|
|
* |
|
118
|
|
|
* @param string $data |
|
119
|
|
|
* @return string |
|
120
|
|
|
*/ |
|
121
|
27 |
|
public function format(string $data): string |
|
122
|
|
|
{ |
|
123
|
27 |
|
return $this->_formatter !== null ? $this->_formatter->format($data) : $data; |
|
124
|
|
|
} |
|
125
|
|
|
} |