1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace hamburgscleanest\DataTables\Models\Column; |
4
|
|
|
|
5
|
|
|
use hamburgscleanest\DataTables\Models\Header; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class ColumnCollection |
11
|
|
|
* @package hamburgscleanest\DataTables\Models\Column |
12
|
|
|
*/ |
13
|
|
|
class ColumnCollection extends Collection { |
14
|
|
|
|
15
|
|
|
/** @var Model */ |
16
|
|
|
private $_model; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Create a new collection of columns. |
20
|
|
|
* |
21
|
|
|
* @param mixed $items |
22
|
|
|
* @param Model $model |
23
|
|
|
*/ |
24
|
70 |
|
public function __construct($items = [], Model $model) |
|
|
|
|
25
|
|
|
{ |
26
|
70 |
|
$this->_model = $model; |
27
|
|
|
|
28
|
70 |
|
parent::__construct($this->_fetchColumns($items)); |
29
|
70 |
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Returns an array of Column objects which may be bound to a formatter. |
33
|
|
|
* |
34
|
|
|
* @param array $columns |
35
|
|
|
* @return array |
36
|
|
|
*/ |
37
|
70 |
|
private function _fetchColumns(array $columns) : array |
38
|
|
|
{ |
39
|
70 |
|
$columnModels = []; |
40
|
70 |
|
foreach ($columns as $column => $formatter) |
41
|
|
|
{ |
42
|
57 |
|
[$column, $formatter] = $this->_setColumnFormatter($column, $formatter); |
43
|
57 |
|
$columnModels[] = new Column($column, $formatter, $this->_model); |
44
|
|
|
} |
45
|
|
|
|
46
|
70 |
|
return $columnModels; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string|int $column |
51
|
|
|
* @param $formatter |
52
|
|
|
* @return array |
53
|
|
|
*/ |
54
|
57 |
|
private function _setColumnFormatter($column, $formatter) : array |
55
|
|
|
{ |
56
|
57 |
|
if (\is_int($column)) |
57
|
|
|
{ |
58
|
55 |
|
$column = $formatter; |
59
|
55 |
|
$formatter = null; |
60
|
|
|
} |
61
|
|
|
|
62
|
57 |
|
return [$column, $formatter]; |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Push a column onto the end of the collection. |
67
|
|
|
* |
68
|
|
|
* @param mixed $value |
69
|
|
|
* @return ColumnCollection |
70
|
|
|
*/ |
71
|
2 |
|
public function push($value) : ColumnCollection |
72
|
|
|
{ |
73
|
2 |
|
$this->items += $this->_fetchColumns(\array_wrap($value)); |
74
|
|
|
|
75
|
2 |
|
return $this; |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
51 |
|
public function getUnmutatedColumns() : array |
82
|
|
|
{ |
83
|
|
|
return \array_filter($this->items, function($column) { |
84
|
|
|
/** @var Column $column */ |
85
|
46 |
|
return !$column->isMutated(); |
86
|
51 |
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
/** |
90
|
|
|
* @return array |
91
|
|
|
*/ |
92
|
46 |
|
public function getHeaders() : array |
93
|
|
|
{ |
94
|
46 |
|
return \array_map( |
95
|
46 |
|
function($column) { |
96
|
|
|
/** @var Column $column */ |
97
|
44 |
|
return new Header($column->getKey()); |
98
|
46 |
|
}, |
99
|
46 |
|
$this->items |
100
|
|
|
); |
101
|
|
|
} |
102
|
|
|
} |
If you place a parameter with a default value before a parameter with a default value, the default value of the first parameter will never be used as it will always need to be passed anyway: