1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Botble\Assets; |
4
|
|
|
|
5
|
|
|
use Illuminate\Support\HtmlString; |
6
|
|
|
use Illuminate\Contracts\Routing\UrlGenerator; |
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 \Illuminate\Support\HtmlString |
34
|
|
|
*/ |
35
|
|
|
public function script($url, $attributes = [], $secure = null) |
36
|
|
|
{ |
37
|
|
|
$attributes['src'] = $this->url->asset($url, $secure); |
38
|
|
|
|
39
|
|
|
return $this->toHtmlString('<script' . $this->attributes($attributes) . '></script>'); |
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Generate a link to a CSS file. |
44
|
|
|
* |
45
|
|
|
* @param string $url |
46
|
|
|
* @param array $attributes |
47
|
|
|
* @param bool $secure |
48
|
|
|
* |
49
|
|
|
* @return \Illuminate\Support\HtmlString |
50
|
|
|
*/ |
51
|
|
|
public function style($url, $attributes = [], $secure = null) |
52
|
|
|
{ |
53
|
|
|
$defaults = [ |
54
|
|
|
'media' => 'all', |
55
|
|
|
'type' => 'text/css', |
56
|
|
|
'rel' => 'stylesheet', |
57
|
|
|
]; |
58
|
|
|
|
59
|
|
|
$attributes = array_merge($defaults, $attributes); |
60
|
|
|
|
61
|
|
|
$attributes['href'] = $this->url->asset($url, $secure); |
62
|
|
|
|
63
|
|
|
return $this->toHtmlString('<link' . $this->attributes($attributes) . '>'); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Transform the string to an Html serializable object. |
68
|
|
|
* |
69
|
|
|
* @param $html |
70
|
|
|
* |
71
|
|
|
* @return \Illuminate\Support\HtmlString |
72
|
|
|
*/ |
73
|
|
|
protected function toHtmlString($html) |
74
|
|
|
{ |
75
|
|
|
return new HtmlString($html); |
76
|
|
|
} |
77
|
|
|
|
78
|
|
|
/** |
79
|
|
|
* Build an HTML attribute string from an array. |
80
|
|
|
* |
81
|
|
|
* @param array $attributes |
82
|
|
|
* |
83
|
|
|
* @return string |
84
|
|
|
*/ |
85
|
|
|
public function attributes($attributes) |
86
|
|
|
{ |
87
|
|
|
$html = []; |
88
|
|
|
|
89
|
|
|
foreach ((array)$attributes as $key => $value) { |
90
|
|
|
$element = $this->attributeElement($key, $value); |
91
|
|
|
|
92
|
|
|
if (!empty($element)) { |
93
|
|
|
$html[] = $element; |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return count($html) > 0 ? ' ' . implode(' ', $html) : ''; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Build a single attribute element. |
102
|
|
|
* |
103
|
|
|
* @param string $key |
104
|
|
|
* @param string $value |
105
|
|
|
* |
106
|
|
|
* @return string |
107
|
|
|
*/ |
108
|
|
|
protected function attributeElement($key, $value) |
109
|
|
|
{ |
110
|
|
|
if (is_numeric($key)) { |
111
|
|
|
return $value; |
112
|
|
|
} |
113
|
|
|
|
114
|
|
|
// Treat boolean attributes as HTML properties |
115
|
|
|
if (is_bool($value) && $key !== 'value') { |
116
|
|
|
return $value ? $key : ''; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
if (is_array($value) && $key === 'class') { |
120
|
|
|
return 'class="' . implode(' ', $value) . '"'; |
121
|
|
|
} |
122
|
|
|
|
123
|
|
|
if (!empty($value)) { |
124
|
|
|
return $key . '="' . e($value, false) . '"'; |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
return $value; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|