1 | <?php |
||||||
2 | |||||||
3 | namespace SleepingOwl\Admin\Display\Column; |
||||||
4 | |||||||
5 | use Closure; |
||||||
6 | use Illuminate\Database\Eloquent\Collection; |
||||||
7 | use Illuminate\Database\Eloquent\Model; |
||||||
8 | use Illuminate\Support\Arr; |
||||||
9 | use Illuminate\Support\Collection as SuportCollection; |
||||||
10 | use SleepingOwl\Admin\Contracts\Display\NamedColumnInterface; |
||||||
11 | use SleepingOwl\Admin\Contracts\Display\OrderByClauseInterface; |
||||||
12 | use SleepingOwl\Admin\Display\TableColumn; |
||||||
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 | * @return TableColumn |
|||||
110 | */ |
||||||
111 | public function setOrderable($orderable = true) |
||||||
112 | { |
||||||
113 | if ($orderable !== false && ! $orderable instanceof OrderByClauseInterface) { |
||||||
114 | if (! is_string($orderable) && ! $orderable instanceof Closure) { |
||||||
0 ignored issues
–
show
introduced
by
![]() |
|||||||
115 | $orderable = $this->getName(); |
||||||
116 | 7 | } |
|||||
117 | } |
||||||
118 | |||||||
119 | return parent::setOrderable($orderable); |
||||||
120 | } |
||||||
121 | |||||||
122 | /** |
||||||
123 | * Get the instance as an array. |
||||||
124 | * |
||||||
125 | * @return array |
||||||
126 | */ |
||||||
127 | public function toArray() |
||||||
128 | { |
||||||
129 | return parent::toArray() + [ |
||||||
130 | 'name' => $this->getName(), |
||||||
131 | ]; |
||||||
132 | } |
||||||
133 | 7 | ||||||
134 | 7 | /** |
|||||
135 | * Get column value from instance. |
||||||
136 | 7 | * |
|||||
137 | 1 | * @param Collection|Model|Closure $instance |
|||||
138 | 7 | * @param string $name |
|||||
139 | * |
||||||
140 | * @return mixed |
||||||
141 | */ |
||||||
142 | protected function getValueFromObject($instance, $name) |
||||||
143 | { |
||||||
144 | if ($name instanceof Closure) { |
||||||
0 ignored issues
–
show
|
|||||||
145 | return $name($instance); |
||||||
146 | } |
||||||
147 | 7 | ||||||
148 | 7 | /* |
|||||
149 | 7 | * Implement json parsing |
|||||
150 | */ |
||||||
151 | 7 | if (strpos($name, '.') === false && strpos($name, '->') !== false) { |
|||||
152 | 2 | $casts = collect($instance->getCasts()); |
|||||
0 ignored issues
–
show
The method
getCasts() does not exist on Closure .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
153 | $jsonParts = collect(explode('->', $name)); |
||||||
154 | |||||||
155 | 7 | $jsonAttr = $instance->{$jsonParts->first()}; |
|||||
156 | |||||||
157 | $cast = $casts->get($jsonParts->first(), false); |
||||||
158 | |||||||
159 | if ($cast == 'object') { |
||||||
160 | $jsonAttr = json_decode(json_encode($jsonAttr), true); |
||||||
161 | } elseif ($cast != 'array') { |
||||||
162 | $jsonAttr = json_decode($jsonAttr); |
||||||
163 | } |
||||||
164 | |||||||
165 | return Arr::get($jsonAttr, $jsonParts->slice(1)->implode('.')); |
||||||
166 | } |
||||||
167 | |||||||
168 | $parts = explode('.', $name); |
||||||
169 | $part = array_shift($parts); |
||||||
170 | |||||||
171 | if ($instance instanceof Collection) { |
||||||
172 | $instance = $instance->pluck($part); |
||||||
173 | } elseif ($instance instanceof SuportCollection) { |
||||||
0 ignored issues
–
show
|
|||||||
174 | $instance = $instance->first(); |
||||||
175 | if ($instance instanceof Collection) { |
||||||
176 | $instance = $instance->pluck($part); |
||||||
177 | } |
||||||
178 | |||||||
179 | if ($instance === null) { |
||||||
180 | $instance = collect(); |
||||||
181 | } |
||||||
182 | } elseif (! is_null($instance)) { |
||||||
183 | $instance = $instance->getAttribute($part); |
||||||
0 ignored issues
–
show
The method
getAttribute() does not exist on Closure .
(
Ignorable by Annotation
)
If this is a false-positive, you can also ignore this issue in your code via the
This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces. This is most likely a typographical error or the method has been renamed. ![]() |
|||||||
184 | } |
||||||
185 | |||||||
186 | if (! empty($parts) && ! is_null($instance)) { |
||||||
187 | return $this->getValueFromObject($instance, implode('.', $parts)); |
||||||
188 | } |
||||||
189 | |||||||
190 | return $instance; |
||||||
191 | } |
||||||
192 | } |
||||||
193 |