Completed
Push — master ( 4cc8d8...e56a52 )
by Arjay
04:43
created

Column::parseRender()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 12
Code Lines 7

Duplication

Lines 0
Ratio 0 %

Importance

Changes 6
Bugs 1 Features 3
Metric Value
dl 0
loc 12
c 6
b 1
f 3
rs 9.2
cc 4
eloc 7
nc 4
nop 1
1
<?php
2
3
namespace Yajra\Datatables\Html;
4
5
use Illuminate\Support\Fluent;
6
7
/**
8
 * Class Column
9
 *
10
 * @package Yajra\Datatables\Html
11
 * @see     https://datatables.net/reference/option/ for possible columns option
12
 */
13
class Column extends Fluent
14
{
15
    /**
16
     * @param array $attributes
17
     */
18
    public function __construct($attributes = [])
19
    {
20
        $attributes['orderable']  = isset($attributes['orderable']) ? $attributes['orderable'] : true;
21
        $attributes['searchable'] = isset($attributes['searchable']) ? $attributes['searchable'] : true;
22
23
        // Allow methods override attribute value
24
        foreach ($attributes as $attribute => $value) {
25
            $method = 'parse' . ucfirst(strtolower($attribute));
26
            if (method_exists($this, $method)) {
27
                $attributes[$attribute] = $this->$method($value);
28
            }
29
        }
30
31
        parent::__construct($attributes);
32
    }
33
34
    /**
35
     * Parse render attribute.
36
     *
37
     * @param \Closure|string $value
38
     * @return string|null
39
     */
40
    public function parseRender($value)
41
    {
42
        $view = app('view');
43
44
        if (is_callable($value)) {
45
            return value($value);
46
        } elseif ($view->exists($value)) {
47
            return $view->make($value)->render();
48
        }
49
50
        return $value ? $this->parseRenderAsString($value) : null;
0 ignored issues
show
Bug introduced by
It seems like $value defined by parameter $value on line 40 can also be of type object<Closure>; however, Yajra\Datatables\Html\Co...::parseRenderAsString() does only seem to accept string, maybe add an additional type check?

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.

Loading history...
51
    }
52
53
    /**
54
     * Display render value as is.
55
     *
56
     * @param string $value
57
     * @return string
58
     */
59
    private function parseRenderAsString($value)
60
    {
61
        return "function(data,type,full,meta){return $value;}";
62
    }
63
}
64