Completed
Push — master ( 77fd36...f39186 )
by Song
02:52
created

Field::formatLabel()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
nc 2
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Show;
4
5
use Encore\Admin\Show;
6
use Illuminate\Contracts\Support\Arrayable;
7
use Illuminate\Contracts\Support\Renderable;
8
use Illuminate\Database\Eloquent\Model;
9
use Illuminate\Support\Collection;
10
use Illuminate\Support\Facades\Storage;
11
12
class Field implements Renderable
13
{
14
    /**
15
     * @var string
16
     */
17
    protected $view = 'admin::show.field';
18
19
    /**
20
     * Name of column.
21
     *
22
     * @var string
23
     */
24
    protected $name;
25
26
    /**
27
     * Label of column.
28
     *
29
     * @var string
30
     */
31
    protected $label;
32
33
    /**
34
     * Field value.
35
     *
36
     * @var mixed
37
     */
38
    protected $value;
39
40
    /**
41
     * @var Collection
42
     */
43
    protected $showAs = [];
44
45
    /**
46
     * Parent show instance.
47
     *
48
     * @var Show
49
     */
50
    protected $parent;
51
52
    /**
53
     * Relation name.
54
     *
55
     * @var string
56
     */
57
    protected $relation;
58
59
    /**
60
     * If show contents in box.
61
     *
62
     * @var bool
63
     */
64
    public $wrapped = true;
65
66
    /**
67
     * Field constructor.
68
     *
69
     * @param string $name
70
     * @param string $label
71
     */
72
    public function __construct($name = '', $label = '')
73
    {
74
        $this->name = $name;
75
76
        $this->label = $this->formatLabel($label);
77
78
        $this->showAs = new Collection();
79
    }
80
81
    /**
82
     * Set parent show instance.
83
     *
84
     * @param Show $show
85
     *
86
     * @return $this
87
     */
88
    public function setParent(Show $show)
89
    {
90
        $this->parent = $show;
91
92
        return $this;
93
    }
94
95
    /**
96
     * Get name of this column.
97
     *
98
     * @return mixed
99
     */
100
    public function getName()
101
    {
102
        return $this->name;
103
    }
104
105
    /**
106
     * Format label.
107
     *
108
     * @param $label
109
     *
110
     * @return mixed
111
     */
112
    protected function formatLabel($label)
113
    {
114
        $label = $label ?: ucfirst($this->name);
115
116
        return str_replace(['.', '_'], ' ', $label);
117
    }
118
119
    /**
120
     * Get label of the column.
121
     *
122
     * @return mixed
123
     */
124
    public function getLabel()
125
    {
126
        return $this->label;
127
    }
128
129
    /**
130
     * Field display callback.
131
     *
132
     * @param callable $callable
133
     *
134
     * @return $this
135
     */
136
    public function as(callable $callable)
137
    {
138
        $this->showAs->push($callable);
139
140
        return $this;
141
    }
142
143
    /**
144
     * Display field using array value map.
145
     *
146
     * @param array $values
147
     * @param null  $default
148
     *
149
     * @return $this
150
     */
151 View Code Duplication
    public function using(array $values, $default = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
152
    {
153
        return $this->as(function ($value) use ($values, $default) {
154
            if (is_null($value)) {
155
                return $default;
156
            }
157
158
            return array_get($values, $value, $default);
159
        });
160
    }
161
162
    /**
163
     * Show field as a image.
164
     *
165
     * @param string $server
166
     * @param int    $width
167
     * @param int    $height
168
     *
169
     * @return $this
170
     */
171
    public function image($server = '', $width = 200, $height = 200)
172
    {
173 View Code Duplication
        return $this->as(function ($path) use ($server, $width, $height) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

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.

Loading history...
174
            if (url()->isValidUrl($path)) {
175
                $src = $path;
176
            } elseif ($server) {
177
                $src = $server.$path;
178
            } else {
179
                $src = Storage::disk(config('admin.upload.disk'))->url($path);
180
            }
181
182
            return "<img src='$src' style='max-width:{$width}px;max-height:{$height}px' class='img' />";
183
        });
184
    }
185
186
    /**
187
     * Show field as a link.
188
     *
189
     * @param string $href
190
     * @param string $target
191
     *
192
     * @return Field
193
     */
194
    public function link($href = '', $target = '_blank')
195
    {
196
        return $this->as(function ($link) use ($href, $target) {
197
            $href = $href ?: $link;
0 ignored issues
show
Bug introduced by
Consider using a different name than the imported variable $href, or did you forget to import by reference?

It seems like you are assigning to a variable which was imported through a use statement which was not imported by reference.

For clarity, we suggest to use a different name or import by reference depending on whether you would like to have the change visibile in outer-scope.

Change not visible in outer-scope

$x = 1;
$callable = function() use ($x) {
    $x = 2; // Not visible in outer scope. If you would like this, how
            // about using a different variable name than $x?
};

$callable();
var_dump($x); // integer(1)

Change visible in outer-scope

$x = 1;
$callable = function() use (&$x) {
    $x = 2;
};

$callable();
var_dump($x); // integer(2)
Loading history...
198
199
            return "<a href='$href' target='{$target}'>{$link}</a>";
200
        });
201
    }
202
203
    /**
204
     * Show field as labels.
205
     *
206
     * @param string $style
207
     *
208
     * @return Field
209
     */
210 View Code Duplication
    public function label($style = 'success')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
211
    {
212
        return $this->as(function ($value) use ($style) {
213
            if ($value instanceof Arrayable) {
214
                $value = $value->toArray();
215
            }
216
217
            return collect((array) $value)->map(function ($name) use ($style) {
218
                return "<span class='label label-{$style}'>$name</span>";
219
            })->implode('&nbsp;');
220
        });
221
    }
222
223
    /**
224
     * Show field as badges.
225
     *
226
     * @param string $style
227
     *
228
     * @return Field
229
     */
230 View Code Duplication
    public function badge($style = 'blue')
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

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.

Loading history...
231
    {
232
        return $this->as(function ($value) use ($style) {
233
            if ($value instanceof Arrayable) {
234
                $value = $value->toArray();
235
            }
236
237
            return collect((array) $value)->map(function ($name) use ($style) {
238
                return "<span class='badge bg-{$style}'>$name</span>";
239
            })->implode('&nbsp;');
240
        });
241
    }
242
243
    /**
244
     * Show field as json code.
245
     *
246
     * @return Field
247
     */
248
    public function json()
249
    {
250
        $field = $this;
251
252
        return $this->as(function ($value) use ($field) {
253
254
            $content = json_decode($value, true);
255
256
            if (json_last_error() == 0) {
257
258
                $field->wrapped = false;
259
260
                return '<pre><code>'.json_encode($content, JSON_PRETTY_PRINT).'</code></pre>';
261
            }
262
263
            return $value;
264
        });
265
    }
266
267
    /**
268
     * Set value for this field.
269
     *
270
     * @param Model $model
271
     *
272
     * @return $this
273
     */
274
    public function setValue(Model $model)
275
    {
276
        if ($this->relation) {
277
            if (!$model->{$this->relation}) {
278
                return $this;
279
            }
280
281
            $this->value = $model->{$this->relation}->getAttribute($this->name);
282
        } else {
283
            $this->value = $model->getAttribute($this->name);
284
        }
285
286
        return $this;
287
    }
288
289
    /**
290
     * Set relation name for this field.
291
     *
292
     * @param string $relation
293
     *
294
     * @return $this
295
     */
296
    public function setRelation($relation)
297
    {
298
        $this->relation = $relation;
299
300
        return $this;
301
    }
302
303
    /**
304
     * @param string $method
305
     * @param array  $arguments
306
     *
307
     * @return $this
308
     */
309
    public function __call($method, $arguments = [])
310
    {
311
        if ($this->relation) {
312
            $this->name = $method;
313
            $this->label = $this->formatLabel(array_get($arguments, 0));
314
        }
315
316
        return $this;
317
    }
318
319
    /**
320
     * Get all variables passed to field view.
321
     *
322
     * @return array
323
     */
324
    protected function variables()
325
    {
326
        return [
327
            'content'   => $this->value,
328
            'label'     => $this->getLabel(),
329
            'wrapped'   => $this->wrapped,
330
        ];
331
    }
332
333
    /**
334
     * Render this field.
335
     *
336
     * @return string
337
     */
338
    public function render()
339
    {
340
        if ($this->showAs->isNotEmpty()) {
341
            $this->showAs->each(function ($callable) {
342
                $this->value = $callable->call(
343
                    $this->parent->getModel(),
344
                    $this->value
345
                );
346
            });
347
        }
348
349
        return view($this->view, $this->variables());
0 ignored issues
show
Bug Compatibility introduced by
The expression view($this->view, $this->variables()); of type Illuminate\View\View|Ill...\Contracts\View\Factory adds the type Illuminate\Contracts\View\Factory to the return on line 349 which is incompatible with the return type declared by the interface Illuminate\Contracts\Support\Renderable::render of type string.
Loading history...
350
    }
351
}
352