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
|
|
|
use Illuminate\Database\Eloquent\Collection; |
9
|
|
|
use SleepingOwl\Admin\Contracts\NamedColumnInterface; |
10
|
|
|
use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface; |
11
|
|
|
|
12
|
|
|
abstract class NamedColumn extends TableColumn implements NamedColumnInterface |
13
|
|
|
{ |
14
|
|
|
/** |
15
|
|
|
* Column field name. |
16
|
|
|
* @var string |
17
|
|
|
*/ |
18
|
|
|
protected $name; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @var bool |
22
|
|
|
*/ |
23
|
|
|
protected $orderable = true; |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param Closure|null|string $name |
27
|
|
|
* @param null|string $label |
28
|
|
|
*/ |
29
|
|
|
public function __construct($name, $label = null) |
30
|
|
|
{ |
31
|
|
|
parent::__construct($label); |
32
|
|
|
$this->setName($name); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class()))); |
35
|
|
|
|
36
|
|
|
if ($this->orderable) { |
37
|
|
|
$this->setOrderable(); |
38
|
|
|
} |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
/** |
42
|
|
|
* @return string |
43
|
|
|
*/ |
44
|
|
|
public function getName() |
45
|
|
|
{ |
46
|
|
|
return $this->name; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @param string $name |
51
|
|
|
* |
52
|
|
|
* @return $this |
53
|
|
|
*/ |
54
|
|
|
public function setName($name) |
55
|
|
|
{ |
56
|
|
|
$this->name = $name; |
57
|
|
|
|
58
|
|
|
return $this; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @return mixed |
63
|
|
|
*/ |
64
|
|
|
public function getModelValue() |
65
|
|
|
{ |
66
|
|
|
return $this->getValueFromObject($this->getModel(), $this->getName()); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @param OrderByClauseInterface|bool $orderable |
71
|
|
|
* |
72
|
|
|
* @return $this |
73
|
|
|
*/ |
74
|
|
|
public function setOrderable($orderable = true) |
75
|
|
|
{ |
76
|
|
|
if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) { |
77
|
|
|
if (! is_string($orderable) && ! $orderable instanceof Closure) { |
78
|
|
|
$orderable = $this->getName(); |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
$orderable = new OrderByClause($orderable); |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
$this->orderByClause = $orderable; |
|
|
|
|
85
|
|
|
$this->getHeader()->setOrderable($this->isOrderable()); |
86
|
|
|
|
87
|
|
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Get the instance as an array. |
92
|
|
|
* |
93
|
|
|
* @return array |
94
|
|
|
*/ |
95
|
|
|
public function toArray() |
96
|
|
|
{ |
97
|
|
|
return parent::toArray() + [ |
98
|
|
|
'name' => $this->getName(), |
99
|
|
|
]; |
100
|
|
|
} |
101
|
|
|
|
102
|
|
|
/** |
103
|
|
|
* Get column value from instance. |
104
|
|
|
* |
105
|
|
|
* @param Collection|Model|Closure $instance |
106
|
|
|
* @param string $name |
107
|
|
|
* |
108
|
|
|
* @return mixed |
109
|
|
|
*/ |
110
|
|
|
protected function getValueFromObject($instance, $name) |
111
|
|
|
{ |
112
|
|
|
if ($name instanceof Closure) { |
113
|
|
|
return $name($instance); |
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
$parts = explode('.', $name); |
117
|
|
|
$part = array_shift($parts); |
118
|
|
|
|
119
|
|
|
if ($instance instanceof Collection) { |
120
|
|
|
$instance = $instance->pluck($part); |
121
|
|
|
} elseif (! is_null($instance)) { |
122
|
|
|
$instance = $instance->getAttribute($part); |
|
|
|
|
123
|
|
|
} |
124
|
|
|
|
125
|
|
|
if (! empty($parts) && ! is_null($instance)) { |
126
|
|
|
return $this->getValueFromObject($instance, implode('.', $parts)); |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
return $instance; |
130
|
|
|
} |
131
|
|
|
} |
132
|
|
|
|
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.