Completed
Pull Request — master (#2637)
by
unknown
02:21
created

Column::build()   A

Complexity

Conditions 4
Paths 3

Size

Total Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
nc 3
nop 0
dl 0
loc 14
rs 9.7998
c 0
b 0
f 0
1
<?php
2
3
namespace Encore\Admin\Layout;
4
5
use Encore\Admin\Grid;
6
use Illuminate\Contracts\Support\Renderable;
7
8
class Column implements Buildable
9
{
10
    /**
11
     * grid system prefix width
12
     * @var array
13
     */
14
    protected $width = [];
15
16
    /**
17
     * @var array
18
     */
19
    protected $contents = [];
20
21
    /**
22
     * Column constructor.
23
     *
24
     * @param $content
25
     * @param int $width
26
     */
27
    public function __construct($content, $width = 12)
28
    {
29
        if ($content instanceof \Closure) {
30
            call_user_func($content, $this);
31
        } else {
32
            $this->append($content);
33
        }
34
35
        ///// set width.
36
        // if null, or $this->width is empty array, set as "md" => "12"
37
        if(is_null($width) || (is_array($width) && count($width) === 0)){
38
            $this->width["md"] = 12;
39
        }
40
        // $this->width is number(old version), set as "md" => $width
41
        elseif(is_numeric($width)){
42
            $this->width["md"] = $width;
43
        }
44
        else{
45
            $this->width = $width;
46
        }
47
    }
48
49
    /**
50
     * Append content to column.
51
     *
52
     * @param $content
53
     *
54
     * @return $this
55
     */
56
    public function append($content)
57
    {
58
        $this->contents[] = $content;
59
60
        return $this;
61
    }
62
63
    /**
64
     * Add a row for column.
65
     *
66
     * @param $content
67
     *
68
     * @return Column
69
     */
70
    public function row($content)
71
    {
72
        if (!$content instanceof \Closure) {
73
            $row = new Row($content);
74
        } else {
75
            $row = new Row();
76
77
            call_user_func($content, $row);
78
        }
79
80
        ob_start();
81
82
        $row->build();
83
84
        $contents = ob_get_contents();
85
86
        ob_end_clean();
87
88
        return $this->append($contents);
89
    }
90
91
    /**
92
     * Build column html.
93
     */
94
    public function build()
95
    {
96
        $this->startColumn();
97
98
        foreach ($this->contents as $content) {
99
            if ($content instanceof Renderable || $content instanceof Grid) {
100
                echo $content->render();
101
            } else {
102
                echo (string) $content;
103
            }
104
        }
105
106
        $this->endColumn();
107
    }
108
109
    /**
110
     * Start column.
111
     */
112
    protected function startColumn()
113
    {
114
        // get classname using width array
115
        $classname = implode(" ", collect($this->width)->map(function($value, $key){
116
            return "col-$key-$value";
117
        })->toArray());
118
        echo "<div class=\"{$classname}\">";
119
    }
120
121
    /**
122
     * End column.
123
     */
124
    protected function endColumn()
125
    {
126
        echo '</div>';
127
    }
128
}
129