|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace hamburgscleanest\DataTables\Models; |
|
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 |
|
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 Relation */ |
|
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
|
51 |
|
public function __construct(string $name, ? ColumnFormatter $columnFormatter = null, ? Model $sourceModel = null) |
|
40
|
|
|
{ |
|
41
|
51 |
|
$this->_setName($name); |
|
42
|
51 |
|
$this->_formatter = $columnFormatter; |
|
43
|
51 |
|
if ($sourceModel !== null) |
|
44
|
|
|
{ |
|
45
|
47 |
|
$this->_table = $sourceModel->getTable(); |
|
46
|
47 |
|
$this->_mutated = \in_array($name, $sourceModel->getMutatedAttributes(), true); |
|
47
|
|
|
} |
|
48
|
51 |
|
} |
|
49
|
|
|
|
|
50
|
|
|
/** |
|
51
|
|
|
* @param string $name |
|
52
|
|
|
*/ |
|
53
|
51 |
|
private function _setName(string $name) : void |
|
54
|
|
|
{ |
|
55
|
51 |
|
$posDivider = \mb_strpos($name, '.'); |
|
56
|
51 |
|
if ($posDivider === false) |
|
57
|
|
|
{ |
|
58
|
48 |
|
$this->_name = $this->_key = $name; |
|
59
|
|
|
|
|
60
|
48 |
|
return; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
6 |
|
$this->_name = \str_replace(')', '', \mb_substr($name, $posDivider + 1)); |
|
64
|
|
|
|
|
65
|
6 |
|
$this->_relation = new Relation($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
|
35 |
|
public function isMutated() : bool |
|
82
|
|
|
{ |
|
83
|
35 |
|
return $this->_mutated; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @return null|Relation |
|
88
|
|
|
*/ |
|
89
|
3 |
|
public function getRelation() : ? Relation |
|
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
|
35 |
|
public function getFormattedValue(Model $rowModel) : ? string |
|
112
|
|
|
{ |
|
113
|
35 |
|
$value = $this->getValue($rowModel); |
|
114
|
|
|
|
|
115
|
35 |
|
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
|
35 |
|
public function getValue(Model $rowModel) : ? string |
|
125
|
|
|
{ |
|
126
|
35 |
|
return $rowModel->{$this->getKey()}; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
|
|
/** |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
35 |
|
public function getKey() : string |
|
133
|
|
|
{ |
|
134
|
35 |
|
return $this->_key; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* @return string |
|
139
|
|
|
*/ |
|
140
|
33 |
|
public function getIdentifier() : string |
|
141
|
|
|
{ |
|
142
|
33 |
|
return $this->getAttributeName() . ' AS ' . $this->_key; |
|
143
|
|
|
} |
|
144
|
|
|
|
|
145
|
|
|
/** |
|
146
|
|
|
* @return string |
|
147
|
|
|
*/ |
|
148
|
33 |
|
public function getAttributeName() : string |
|
149
|
|
|
{ |
|
150
|
33 |
|
if ($this->_relation !== null) |
|
151
|
|
|
{ |
|
152
|
4 |
|
return $this->_relation->attributeName; |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
32 |
|
if ($this->_mutated) |
|
156
|
|
|
{ |
|
157
|
|
|
return $this->_name; |
|
158
|
|
|
} |
|
159
|
|
|
|
|
160
|
32 |
|
return $this->_table . '.' . $this->_name; |
|
161
|
|
|
} |
|
162
|
|
|
} |
|
163
|
|
|
|