Completed
Push — master ( 9792a6...aaaf86 )
by recca
10:22
created

Template::setAttributes()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
ccs 3
cts 3
cp 1
crap 1
1
<?php
2
3
namespace Recca0120\LaravelTracy;
4
5
class Template
6
{
7
    /**
8
     * $attributes.
9
     *
10
     * @var array
11
     */
12
    protected $attributes = [];
13
14
    /**
15
     * $minify.
16
     *
17
     * @var bool
18
     */
19
    protected $minify = true;
20
21
    /**
22
     * setAttributes.
23
     *
24
     * @param array $attributes
25
     * @return $this
26
     */
27 3
    public function setAttributes($attributes)
28
    {
29 3
        $this->attributes = $attributes;
30
31 3
        return $this;
32
    }
33
34
    /**
35
     * minify.
36
     *
37
     * @param bool $minify
38
     * @return $this
39
     */
40 2
    public function minify($minify)
41
    {
42 2
        $this->minify = $minify;
43
44 2
        return $this;
45
    }
46
47
    /**
48
     * render.
49
     *
50
     * @param string $view
51
     * @return string
52
     */
53 3
    public function render($view)
54
    {
55 3
        extract($this->attributes);
56
57 3
        ob_start();
58 3
        require $view;
59
60 3
        return $this->minify === true
61 2
            ? $this->min(ob_get_clean())
62 3
            : ob_get_clean();
63
    }
64
65
    /**
66
     * min.
67
     *
68
     * if need min style and script, refrence
69
     * https://gist.github.com/recca0120/5930842de4e0a43a48b8bf027ab058f9
70
     *
71
     * @param string $buffer
72
     * @return string
73
     */
74 2
    protected function min($buffer)
75
    {
76 2
        return preg_replace(
77 2
            ['/<!--(.*)-->/Uis', '/[[:blank:]]+/'],
78 2
            ['', ' '],
79 2
            str_replace(["\n", "\r", "\t"], '', $buffer)
80
        );
81
    }
82
}
83