HtmlBuilder::script()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 2
nc 2
nop 3
1
<?php
2
3
namespace Botble\Assets;
4
5
use Illuminate\Contracts\Routing\UrlGenerator;
6
use Illuminate\Support\HtmlString;
7
8
class HtmlBuilder
9
{
10
    /**
11
     * The URL generator instance.
12
     *
13
     * @var \Illuminate\Contracts\Routing\UrlGenerator
14
     */
15
    protected $url;
16
17
    /**
18
     * HtmlBuilder constructor.
19
     * @param UrlGenerator $urlGenerator
20
     */
21
    public function __construct(UrlGenerator $urlGenerator)
22
    {
23
        $this->url = $urlGenerator;
24
    }
25
26
    /**
27
     * Generate a link to a JavaScript file.
28
     *
29
     * @param string $url
30
     * @param array $attributes
31
     * @param bool $secure
32
     *
33
     * @return HtmlString|string
34
     */
35
    public function script($url, $attributes = [], $secure = null)
36
    {
37
        if (!$url) {
38
            return '';
39
        }
40
41
        $attributes['src'] = $this->url->asset($url, $secure);
42
43
        return $this->toHtmlString('<script' . $this->attributes($attributes) . '></script>');
44
    }
45
46
    /**
47
     * Generate a link to a CSS file.
48
     *
49
     * @param string $url
50
     * @param array $attributes
51
     * @param bool $secure
52
     *
53
     * @return HtmlString|string
54
     */
55
    public function style($url, $attributes = [], $secure = null)
56
    {
57
        if (!$url) {
58
            return '';
59
        }
60
61
        $defaults = [
62
            'media' => 'all',
63
            'type'  => 'text/css',
64
            'rel'   => 'stylesheet',
65
        ];
66
67
        $attributes = array_merge($defaults, $attributes);
68
69
        $attributes['href'] = $this->url->asset($url, $secure);
70
71
        return $this->toHtmlString('<link' . $this->attributes($attributes) . '>');
72
    }
73
74
    /**
75
     * Build an HTML attribute string from an array.
76
     *
77
     * @param array $attributes
78
     *
79
     * @return string
80
     */
81
    public function attributes($attributes)
82
    {
83
        $html = [];
84
85
        foreach ((array)$attributes as $key => $value) {
86
            $element = is_numeric($key) ? $key : $this->attributeElement($key, $value);
87
88
            if (empty($element)) {
89
                continue;
90
            }
91
92
            $html[] = $element;
93
        }
94
95
        return count($html) > 0 ? ' ' . implode(' ', $html) : '';
96
    }
97
98
    /**
99
     * Transform the string to an Html serializable object.
100
     *
101
     * @param $html
102
     *
103
     * @return \Illuminate\Support\HtmlString
104
     */
105
    protected function toHtmlString($html)
106
    {
107
        return new HtmlString($html);
108
    }
109
110
    /**
111
     * Build a single attribute element.
112
     *
113
     * @param string $key
114
     * @param string $value
115
     *
116
     * @return string
117
     */
118
    protected function attributeElement($key, $value)
119
    {
120
        // Treat boolean attributes as HTML properties
121
        if (is_bool($value) && $key !== 'value') {
122
            return $value ? $key : '';
123
        }
124
125
        if (is_array($value) && $key === 'class') {
126
            return 'class="' . implode(' ', $value) . '"';
127
        }
128
129
        if (!empty($value)) {
130
            return $key . '="' . e($value, false) . '"';
131
        }
132
133
        return $value;
134
    }
135
}
136