1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SleepingOwl\Admin\Display\Column; |
4
|
|
|
|
5
|
|
|
use Closure; |
6
|
|
|
use Illuminate\Support\Arr; |
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
8
|
|
|
use SleepingOwl\Admin\Display\TableColumn; |
9
|
|
|
use Illuminate\Database\Eloquent\Collection; |
10
|
|
|
use Illuminate\Support\Collection as SuportCollection; |
11
|
|
|
use SleepingOwl\Admin\Contracts\Display\NamedColumnInterface; |
12
|
|
|
use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface; |
13
|
|
|
|
14
|
|
|
abstract class NamedColumn extends TableColumn implements NamedColumnInterface |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* Column field name. |
18
|
|
|
* @var string |
19
|
|
|
*/ |
20
|
|
|
protected $name; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var string |
24
|
|
|
*/ |
25
|
|
|
protected $small; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @var bool |
29
|
|
|
*/ |
30
|
|
|
protected $orderable = true; |
31
|
21 |
|
|
32
|
|
|
/** |
33
|
21 |
|
* NamedColumn constructor. |
34
|
21 |
|
* @param $name |
35
|
|
|
* @param $label string |
36
|
21 |
|
* @param $small string |
37
|
|
|
*/ |
38
|
21 |
|
public function __construct($name, $label = null, $small = null) |
39
|
15 |
|
{ |
40
|
15 |
|
parent::__construct($label); |
41
|
21 |
|
$this->setName($name); |
42
|
|
|
$this->setSmall($small); |
43
|
|
|
|
44
|
|
|
$this->setHtmlAttribute('class', 'row-'.strtolower(class_basename(get_called_class()))); |
45
|
|
|
|
46
|
17 |
|
if ($this->orderable) { |
47
|
|
|
$this->setOrderable(); |
|
|
|
|
48
|
17 |
|
} |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @return string |
53
|
|
|
*/ |
54
|
|
|
public function getName() |
55
|
|
|
{ |
56
|
21 |
|
return $this->name; |
57
|
|
|
} |
58
|
21 |
|
|
59
|
|
|
/** |
60
|
21 |
|
* @param string $name |
61
|
|
|
* |
62
|
|
|
* @return $this |
63
|
|
|
*/ |
64
|
|
|
public function setName($name) |
65
|
|
|
{ |
66
|
7 |
|
$this->name = $name; |
67
|
|
|
|
68
|
7 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @return string |
73
|
|
|
*/ |
74
|
|
|
public function getSmall() |
75
|
|
|
{ |
76
|
15 |
|
return $this->small; |
77
|
|
|
} |
78
|
15 |
|
|
79
|
15 |
|
/** |
80
|
15 |
|
* @param string $small |
81
|
15 |
|
* |
82
|
15 |
|
* @return $this |
83
|
|
|
*/ |
84
|
15 |
|
public function setSmall($small) |
85
|
|
|
{ |
86
|
|
|
$this->small = $small; |
87
|
|
|
|
88
|
|
|
return $this; |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
1 |
|
* @return mixed |
93
|
|
|
*/ |
94
|
1 |
|
public function getModelValue() |
95
|
1 |
|
{ |
96
|
1 |
|
return $this->getValueFromObject($this->getModel(), $this->getName()); |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
/** |
100
|
|
|
* @return mixed |
101
|
|
|
*/ |
102
|
|
|
public function getModelSmallValue() |
103
|
|
|
{ |
104
|
|
|
return $this->getValueFromObject($this->getModel(), $this->getSmall()); |
105
|
|
|
} |
106
|
|
|
|
107
|
7 |
|
/** |
108
|
|
|
* @param OrderByClauseInterface|bool $orderable |
109
|
7 |
|
* @deprecated |
110
|
|
|
* @return TableColumn |
111
|
|
|
*/ |
112
|
|
|
public function setOrderable($orderable = true) |
113
|
|
|
{ |
114
|
|
|
if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) { |
115
|
|
|
if (! is_string($orderable) && ! $orderable instanceof Closure) { |
|
|
|
|
116
|
7 |
|
$orderable = $this->getName(); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
return parent::setOrderable($orderable); |
|
|
|
|
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
/** |
124
|
|
|
* Get the instance as an array. |
125
|
|
|
* |
126
|
|
|
* @return array |
127
|
|
|
*/ |
128
|
|
|
public function toArray() |
129
|
|
|
{ |
130
|
|
|
return parent::toArray() + [ |
131
|
|
|
'name' => $this->getName(), |
132
|
|
|
]; |
133
|
7 |
|
} |
134
|
7 |
|
|
135
|
|
|
/** |
136
|
7 |
|
* Get column value from instance. |
137
|
1 |
|
* |
138
|
7 |
|
* @param Collection|Model|Closure $instance |
139
|
|
|
* @param string $name |
140
|
|
|
* |
141
|
|
|
* @return mixed |
142
|
|
|
*/ |
143
|
|
|
protected function getValueFromObject($instance, $name) |
144
|
|
|
{ |
145
|
|
|
if ($name instanceof Closure) { |
|
|
|
|
146
|
|
|
return $name($instance); |
147
|
7 |
|
} |
148
|
7 |
|
|
149
|
7 |
|
/* |
150
|
|
|
* Implement json parsing |
151
|
7 |
|
*/ |
152
|
2 |
|
if (strpos($name, '.') === false && strpos($name, '->') !== false) { |
153
|
|
|
$casts = collect($instance->getCasts()); |
|
|
|
|
154
|
|
|
$jsonParts = collect(explode('->', $name)); |
155
|
7 |
|
|
156
|
|
|
$jsonAttr = $instance->{$jsonParts->first()}; |
157
|
|
|
|
158
|
|
|
$cast = $casts->get($jsonParts->first(), false); |
159
|
|
|
|
160
|
|
|
if ($cast == 'object') { |
161
|
|
|
$jsonAttr = json_decode(json_encode($jsonAttr), true); |
162
|
|
|
} elseif ($cast != 'array') { |
163
|
|
|
$jsonAttr = json_decode($jsonAttr); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.')); |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
$parts = explode('.', $name); |
170
|
|
|
$part = array_shift($parts); |
171
|
|
|
|
172
|
|
|
if ($instance instanceof Collection) { |
173
|
|
|
$instance = $instance->pluck($part); |
174
|
|
|
} elseif ($instance instanceof SuportCollection) { |
|
|
|
|
175
|
|
|
$instance = $instance->first(); |
176
|
|
|
if ($instance instanceof Collection) { |
177
|
|
|
$instance = $instance->pluck($part); |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
if ($instance === null) { |
181
|
|
|
$instance = collect(); |
182
|
|
|
} |
183
|
|
|
} elseif (! is_null($instance)) { |
184
|
|
|
$instance = $instance->getAttribute($part); |
|
|
|
|
185
|
|
|
} |
186
|
|
|
|
187
|
|
|
if (! empty($parts) && ! is_null($instance)) { |
188
|
|
|
return $this->getValueFromObject($instance, implode('.', $parts)); |
189
|
|
|
} |
190
|
|
|
|
191
|
|
|
return $instance; |
192
|
|
|
} |
193
|
|
|
} |
194
|
|
|
|