Completed
Push — master ( 110858...d54979 )
by Song
12s
created

Column::__construct()   B

Complexity

Conditions 6
Paths 6

Size

Total Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
nc 6
nop 2
dl 0
loc 20
rs 8.9777
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
     *
13
     * @var array
14
     */
15
    protected $width = [];
16
17
    /**
18
     * @var array
19
     */
20
    protected $contents = [];
21
22
    /**
23
     * Column constructor.
24
     *
25
     * @param $content
26
     * @param int $width
27
     */
28
    public function __construct($content, $width = 12)
29
    {
30
        if ($content instanceof \Closure) {
31
            call_user_func($content, $this);
32
        } else {
33
            $this->append($content);
34
        }
35
36
        ///// set width.
37
        // if null, or $this->width is empty array, set as "md" => "12"
38
        if (is_null($width) || (is_array($width) && count($width) === 0)) {
39
            $this->width['md'] = 12;
40
        }
41
        // $this->width is number(old version), set as "md" => $width
42
        elseif (is_numeric($width)) {
43
            $this->width['md'] = $width;
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