-
Notifications
You must be signed in to change notification settings - Fork 461
/
Copy pathHtmlHelper.php
executable file
·142 lines (122 loc) · 3.48 KB
/
HtmlHelper.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
<?php
namespace App\Helpers\General;
use Illuminate\Contracts\Routing\UrlGenerator;
use Illuminate\Support\HtmlString;
/**
* Class HtmlHelper.
*/
class HtmlHelper
{
/**
* The URL generator instance.
*
* @var \Illuminate\Contracts\Routing\UrlGenerator
*/
protected $url;
/**
* HtmlHelper constructor.
*
* @param UrlGenerator|null $url
*/
public function __construct(UrlGenerator $url = null)
{
$this->url = $url;
}
/**
* @param $url
* @param array $attributes
* @param null $secure
* @return mixed
*/
public function style($url, $attributes = [], $secure = null)
{
$defaults = ['media' => 'all', 'type' => 'text/css', 'rel' => 'stylesheet'];
$attributes = $attributes + $defaults;
$attributes['href'] = $this->url->asset($url, $secure);
return $this->toHtmlString('<link'.$this->attributes($attributes).'>'.PHP_EOL);
}
/**
* Generate a link to a JavaScript file.
*
* @param string $url
* @param array $attributes
* @param bool $secure
* @return \Illuminate\Support\HtmlString
*/
public function script($url, $attributes = [], $secure = null)
{
$attributes['src'] = $this->url->asset($url, $secure);
return $this->toHtmlString('<script'.$this->attributes($attributes).'></script>'.PHP_EOL);
}
/**
* @param $cancel_to
* @param $title
* @param string $classes
* @return HtmlString
*/
public function formCancel($cancel_to, $title, $classes = 'btn btn-danger btn-sm')
{
return html()->a($cancel_to, $title)->class($classes);
}
/**
* @param $title
* @param string $classes
* @return HtmlString
*/
public function formSubmit($title, $classes = 'btn btn-success btn-sm pull-right')
{
return html()->submit($title)->class($classes);
}
/**
* Build an HTML attribute string from an array.
*
* @param array $attributes
* @return string
*/
public function attributes($attributes)
{
$html = [];
foreach ((array) $attributes as $key => $value) {
$element = $this->attributeElement($key, $value);
if (! is_null($element)) {
$html[] = $element;
}
}
return count($html) > 0 ? ' '.implode(' ', $html) : '';
}
/**
* Build a single attribute element.
*
* @param string $key
* @param string $value
* @return string
*/
protected function attributeElement($key, $value)
{
// For numeric keys we will assume that the value is a boolean attribute
// where the presence of the attribute represents a true value and the
// absence represents a false value.
// This will convert HTML attributes such as "required" to a correct
// form instead of using incorrect numerics.
if (is_numeric($key)) {
return $value;
}
// Treat boolean attributes as HTML properties
if (is_bool($value) && $key != 'value') {
return $value ? $key : '';
}
if (! is_null($value)) {
return $key.'="'.e($value).'"';
}
}
/**
* Transform the string to an Html serializable object.
*
* @param $html
* @return \Illuminate\Support\HtmlString
*/
protected function toHtmlString($html)
{
return new HtmlString($html);
}
}