1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Column; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use SleepingOwl\Admin\Display\TableColumn; |
8
|
|
|
|
9
|
|
|
class Custom extends TableColumn |
10
|
|
|
{ |
11
|
|
|
/** |
12
|
|
|
* Callback to render column contents. |
13
|
|
|
* @var Closure |
14
|
|
|
*/ |
15
|
|
|
protected $callback; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $view = 'column.custom'; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* A field that can be ordered |
24
|
|
|
* @var string |
25
|
|
|
*/ |
26
|
|
|
protected $orderField; |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Custom constructor. |
30
|
|
|
* |
31
|
|
|
* @param null|string $label |
32
|
|
|
* @param Closure $callback |
33
|
|
|
*/ |
34
|
|
View Code Duplication |
public function __construct($label = null, Closure $callback = null) |
|
|
|
|
35
|
|
|
{ |
36
|
|
|
parent::__construct($label); |
37
|
|
|
if (! is_null($callback)) { |
38
|
|
|
$this->setCallback($callback); |
39
|
|
|
} |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* @return Closure |
44
|
|
|
*/ |
45
|
|
|
public function getCallback() |
46
|
|
|
{ |
47
|
|
|
return $this->callback; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* @param Closure $callback |
52
|
|
|
* |
53
|
|
|
* @return $this |
54
|
|
|
*/ |
55
|
|
|
public function setCallback(Closure $callback) |
56
|
|
|
{ |
57
|
|
|
$this->callback = $callback; |
58
|
|
|
|
59
|
|
|
return $this; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $field |
64
|
|
|
* |
65
|
|
|
* @return $this |
66
|
|
|
*/ |
67
|
|
|
public function setOrderField($field) |
68
|
|
|
{ |
69
|
|
|
$this->orderField = $field; |
70
|
|
|
|
71
|
|
|
return $this; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @return string |
76
|
|
|
*/ |
77
|
|
|
public function getOrderField() |
78
|
|
|
{ |
79
|
|
|
return $this->orderField; |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
/** |
83
|
|
|
* Get value from callback. |
84
|
|
|
* |
85
|
|
|
* @param Model $model |
86
|
|
|
* |
87
|
|
|
* @return mixed |
88
|
|
|
* @throws \Exception |
89
|
|
|
*/ |
90
|
|
|
protected function getValue(Model $model) |
91
|
|
|
{ |
92
|
|
|
if (! is_callable($callback = $this->getCallback())) { |
93
|
|
|
throw new \Exception('Invalid custom column callback'); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return call_user_func($callback, $model); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return array |
101
|
|
|
* @throws \Exception |
102
|
|
|
*/ |
103
|
|
|
public function toArray() |
104
|
|
|
{ |
105
|
|
|
return parent::toArray() + [ |
106
|
|
|
'value' => $this->getValue($this->getModel()), |
107
|
|
|
'append' => $this->getAppends(), |
108
|
|
|
]; |
109
|
|
|
} |
110
|
|
|
} |
111
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.