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