1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hamburgscleanest\DataTables\Models\Column; |
4
|
|
|
|
5
|
|
|
use hamburgscleanest\DataTables\Interfaces\ColumnFormatter; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
|
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Header |
11
|
|
|
* @package hamburgscleanest\hamburgscleanest\DataTables\Models\Column |
12
|
|
|
*/ |
13
|
|
|
class Column { |
14
|
|
|
|
15
|
|
|
/** @var string */ |
16
|
|
|
private $_key; |
17
|
|
|
|
18
|
|
|
/** @var string */ |
19
|
|
|
private $_name; |
20
|
|
|
|
21
|
|
|
/** @var bool */ |
22
|
|
|
private $_mutated = false; |
23
|
|
|
|
24
|
|
|
/** @var string */ |
25
|
|
|
private $_table; |
26
|
|
|
|
27
|
|
|
/** @var ColumnRelation */ |
28
|
|
|
private $_relation; |
29
|
|
|
|
30
|
|
|
/** @var ColumnFormatter */ |
31
|
|
|
private $_formatter; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Column constructor. |
35
|
|
|
* @param string $name |
36
|
|
|
* @param ColumnFormatter|null $columnFormatter |
37
|
|
|
* @param Model|null $sourceModel |
38
|
|
|
*/ |
39
|
49 |
|
public function __construct(string $name, ? ColumnFormatter $columnFormatter = null, ? Model $sourceModel = null) |
40
|
|
|
{ |
41
|
49 |
|
$this->_setName($name); |
42
|
49 |
|
$this->_formatter = $columnFormatter; |
43
|
49 |
|
if ($sourceModel !== null) |
44
|
|
|
{ |
45
|
45 |
|
$this->_table = $sourceModel->getTable(); |
46
|
45 |
|
$this->_mutated = \in_array($name, $sourceModel->getMutatedAttributes(), true); |
47
|
|
|
} |
48
|
49 |
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param string $name |
52
|
|
|
*/ |
53
|
49 |
|
private function _setName(string $name) : void |
54
|
|
|
{ |
55
|
49 |
|
$posDivider = \mb_strpos($name, '.'); |
56
|
49 |
|
if ($posDivider === false) |
57
|
|
|
{ |
58
|
46 |
|
$this->_name = $this->_key = $name; |
59
|
|
|
|
60
|
46 |
|
return; |
61
|
|
|
} |
62
|
|
|
|
63
|
6 |
|
$this->_name = \str_replace(')', '', \mb_substr($name, $posDivider + 1)); |
64
|
|
|
|
65
|
6 |
|
$this->_relation = new ColumnRelation($name); |
66
|
6 |
|
$aggregate = $this->_relation->aggregate; |
67
|
6 |
|
$this->_key = ($aggregate !== 'first' ? ($aggregate . '_') : '') . $this->_relation->name; |
68
|
6 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return string |
72
|
|
|
*/ |
73
|
5 |
|
public function getName() : string |
74
|
|
|
{ |
75
|
5 |
|
return $this->_name; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return bool |
80
|
|
|
*/ |
81
|
36 |
|
public function isMutated() : bool |
82
|
|
|
{ |
83
|
36 |
|
return $this->_mutated; |
84
|
|
|
} |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* @return null|ColumnRelation |
88
|
|
|
*/ |
89
|
3 |
|
public function getRelation() : ? ColumnRelation |
90
|
|
|
{ |
91
|
3 |
|
return $this->_relation; |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param ColumnFormatter $columnFormatter |
96
|
|
|
* @return Column |
97
|
|
|
*/ |
98
|
2 |
|
public function setFormatter(ColumnFormatter $columnFormatter) : Column |
99
|
|
|
{ |
100
|
2 |
|
$this->_formatter = $columnFormatter; |
101
|
|
|
|
102
|
2 |
|
return $this; |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Get the formatted column value. |
107
|
|
|
* |
108
|
|
|
* @param Model $rowModel |
109
|
|
|
* @return string|null |
110
|
|
|
*/ |
111
|
34 |
|
public function getFormattedValue(Model $rowModel) : ? string |
112
|
|
|
{ |
113
|
34 |
|
$value = $this->getValue($rowModel); |
114
|
|
|
|
115
|
34 |
|
return $this->_formatter !== null ? $this->_formatter->format($value) : $value; |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Get the value of this column for the given row. |
120
|
|
|
* |
121
|
|
|
* @param Model $rowModel |
122
|
|
|
* @return string|null |
123
|
|
|
*/ |
124
|
34 |
|
public function getValue(Model $rowModel) : ? string |
125
|
|
|
{ |
126
|
34 |
|
return $rowModel->{$this->getKey()}; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* @return string |
131
|
|
|
*/ |
132
|
34 |
|
public function getKey() : string |
133
|
|
|
{ |
134
|
34 |
|
return $this->_key; |
135
|
|
|
} |
136
|
|
|
|
137
|
|
|
/** |
138
|
|
|
* @return string |
139
|
|
|
*/ |
140
|
34 |
|
public function getIdentifier() : string |
141
|
|
|
{ |
142
|
34 |
|
return $this->getAttributeName() . ' AS ' . $this->_key; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* @return string |
147
|
|
|
*/ |
148
|
35 |
|
public function getAttributeName() : string |
149
|
|
|
{ |
150
|
35 |
|
if ($this->_relation !== null) |
151
|
|
|
{ |
152
|
4 |
|
return $this->_relation->attributeName; |
153
|
|
|
} |
154
|
|
|
|
155
|
34 |
|
if ($this->_mutated) |
156
|
|
|
{ |
157
|
1 |
|
return $this->_name; |
158
|
|
|
} |
159
|
|
|
|
160
|
33 |
|
return $this->_table . '.' . $this->_name; |
161
|
|
|
} |
162
|
|
|
} |
163
|
|
|
|