Completed
Pull Request — master (#12)
by ARCANEDEV
14:58 queued 04:03
created

HtmlBuilder::linkAction()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 2
b 0
f 0
nc 1
nop 5
dl 0
loc 6
rs 9.4285
ccs 3
cts 3
cp 1
crap 1
1
<?php namespace Arcanedev\LaravelHtml;
2
3
use Arcanedev\LaravelHtml\Bases\Builder;
4
use Arcanedev\LaravelHtml\Contracts\HtmlBuilderInterface;
5
use Illuminate\Contracts\Routing\UrlGenerator;
6
7
/**
8
 * Class     HtmlBuilder
9
 *
10
 * @package  Arcanedev\LaravelHtml
11
 * @author   ARCANEDEV <[email protected]>
12
 */
13
class HtmlBuilder extends Builder implements HtmlBuilderInterface
14
{
15
    /* ------------------------------------------------------------------------------------------------
16
     |  Properties
17
     | ------------------------------------------------------------------------------------------------
18
     */
19
    /**
20
     * The URL generator instance.
21
     *
22
     * @var \Illuminate\Contracts\Routing\UrlGenerator
23
     */
24
    protected $url;
25
26
    /* ------------------------------------------------------------------------------------------------
27
     |  Constructor
28
     | ------------------------------------------------------------------------------------------------
29
     */
30
    /**
31
     * Create a new HTML builder instance.
32
     *
33
     * @param  \Illuminate\Contracts\Routing\UrlGenerator  $url
34
     */
35 1096
    public function __construct(UrlGenerator $url = null)
36
    {
37 1096
        $this->url = $url;
38 1096
    }
39
40
    /* ------------------------------------------------------------------------------------------------
41
     |  Main Functions
42
     | ------------------------------------------------------------------------------------------------
43
     */
44
    /**
45
     * Convert an HTML string to entities.
46
     *
47
     * @param  string  $value
48
     *
49
     * @return string
50
     */
51 102
    public function entities($value)
52
    {
53 102
        return htmlentities($value, ENT_QUOTES, 'UTF-8', false);
54
    }
55
56
    /**
57
     * Convert entities to HTML characters.
58
     *
59
     * @param  string  $value
60
     *
61
     * @return string
62
     */
63 16
    public function decode($value)
64
    {
65 16
        return html_entity_decode($value, ENT_QUOTES, 'UTF-8');
66
    }
67
68
    /**
69
     * Generate a link to a JavaScript file.
70
     *
71
     * @param  string  $url
72
     * @param  array   $attributes
73
     * @param  bool    $secure
74
     *
75
     * @return \Illuminate\Support\HtmlString
76
     */
77 16
    public function script($url, $attributes = [], $secure = null)
78
    {
79 16
        $attributes['src'] = $this->url->asset($url, $secure);
80
81 16
        return $this->toHtmlString(
82 16
            '<script' . $this->attributes($attributes) . '></script>' . PHP_EOL
83 12
        );
84
    }
85
86
    /**
87
     * Generate a link to a CSS file.
88
     *
89
     * @param  string  $url
90
     * @param  array   $attributes
91
     * @param  bool    $secure
92
     *
93
     * @return \Illuminate\Support\HtmlString
94
     */
95 16
    public function style($url, $attributes = [], $secure = null)
96
    {
97
        $attributes         = $attributes + [
98 16
            'media' => 'all',
99 12
            'type'  => 'text/css',
100
            'rel'   => 'stylesheet'
101 12
        ];
102 16
        $attributes['href'] = $this->url->asset($url, $secure);
103
104 16
        return $this->toHtmlString(
105 16
            '<link' . $this->attributes($attributes) . '>' . PHP_EOL
106 12
        );
107
    }
108
109
    /**
110
     * Generate an HTML image element.
111
     *
112
     * @param  string  $url
113
     * @param  string  $alt
114
     * @param  array   $attributes
115
     * @param  bool    $secure
116
     *
117
     * @return \Illuminate\Support\HtmlString
118
     */
119 16
    public function image($url, $alt = null, $attributes = [], $secure = null)
120
    {
121 16
        $attributes['alt'] = $alt;
122
123 16
        return $this->toHtmlString(
124 16
            '<img src="' . $this->url->asset($url, $secure) . '"' . $this->attributes($attributes) . '>'
125 12
        );
126
    }
127
128
    /**
129
     * Generate a link to a Favicon file.
130
     *
131
     * @param  string  $url
132
     * @param  array   $attributes
133
     * @param  bool    $secure
134
     *
135
     * @return \Illuminate\Support\HtmlString
136
     */
137 16
    public function favicon($url, $attributes = [], $secure = null)
138
    {
139 16
        $attributes = array_merge($attributes, [
140 16
            'rel'  => 'shortcut icon',
141 16
            'type' => 'image/x-icon',
142 16
            'href' => $this->url->asset($url, $secure)
143 12
        ]);
144
145 16
        return $this->toHtmlString(
146 16
            '<link' . $this->attributes($attributes) . '>' . PHP_EOL
147 12
        );
148
    }
149
150
    /**
151
     * Generate a HTML link.
152
     *
153
     * @param  string  $url
154
     * @param  string  $title
155
     * @param  array   $attributes
156
     * @param  bool    $secure
157
     * @param  bool    $escaped
158
     *
159
     * @return \Illuminate\Support\HtmlString
160
     */
161 72
    public function link($url, $title = null, $attributes = [], $secure = null, $escaped = true)
162
    {
163 72
        $url = $this->url->to($url, [], $secure);
164
165 72
        if (is_null($title) || $title === false)
166 62
            $title = $url;
167
168 72
        return $this->toHtmlString(
169 72
            '<a href="' . $url . '"' . $this->attributes($attributes) . '>' .
170 72
                ($escaped ? $this->entities($title) : $title).
171 18
            '</a>'
172 54
        );
173
    }
174
175
    /**
176
     * Generate a HTTPS HTML link.
177
     *
178
     * @param  string  $url
179
     * @param  string  $title
180
     * @param  array   $attributes
181
     * @param  bool    $escaped
182
     *
183
     * @return \Illuminate\Support\HtmlString
184
     */
185 16
    public function secureLink($url, $title = null, $attributes = [], $escaped = true)
186
    {
187 16
        return $this->link($url, $title, $attributes, true, $escaped);
188
    }
189
190
    /**
191
     * Generate a HTML link to an asset.
192
     *
193
     * @param  string  $url
194
     * @param  string  $title
195
     * @param  array   $attributes
196
     * @param  bool    $secure
197
     *
198
     * @return \Illuminate\Support\HtmlString
199
     */
200 24
    public function linkAsset($url, $title = null, $attributes = [], $secure = null)
201
    {
202 24
        $url = $this->url->asset($url, $secure);
203
204 24
        return $this->link($url, $title ?: $url, $attributes, $secure);
205
    }
206
207
    /**
208
     * Generate a HTTPS HTML link to an asset.
209
     *
210
     * @param  string  $url
211
     * @param  string  $title
212
     * @param  array   $attributes
213
     *
214
     * @return \Illuminate\Support\HtmlString
215
     */
216 8
    public function linkSecureAsset($url, $title = null, $attributes = [])
217
    {
218 8
        return $this->linkAsset($url, $title, $attributes, true);
219
    }
220
221
    /**
222
     * Generate a HTML link to a named route.
223
     *
224
     * @param  string  $name
225
     * @param  string  $title
226
     * @param  array   $parameters
227
     * @param  array   $attributes
228
     * @param  bool    $escaped
229
     *
230
     * @return \Illuminate\Support\HtmlString
231
     */
232 8
    public function linkRoute($name, $title = null, $parameters = [], $attributes = [], $escaped = true)
233
    {
234 8
        $url = $this->url->route($name, $parameters);
235
236 8
        return $this->link($url, $title, $attributes, null, $escaped);
237
    }
238
239
    /**
240
     * Generate a HTML link to a controller action.
241
     *
242
     * @param  string  $action
243
     * @param  string  $title
244
     * @param  array   $parameters
245
     * @param  array   $attributes
246
     * @param  bool    $escaped
247
     *
248
     * @return \Illuminate\Support\HtmlString
249
     */
250 8
    public function linkAction($action, $title = null, $parameters = [], $attributes = [], $escaped = true)
251
    {
252 8
        $url = $this->url->action($action, $parameters);
253
254 8
        return $this->link($url, $title, $attributes, null, $escaped);
255
    }
256
257
    /**
258
     * Generate a HTML link to an email address.
259
     *
260
     * @param  string  $email
261
     * @param  string  $title
262
     * @param  array   $attributes
263
     * @param  bool    $escaped
264
     *
265
     * @return \Illuminate\Support\HtmlString
266
     */
267 8
    public function mailto($email, $title = null, $attributes = [], $escaped = true)
268
    {
269 8
        $email = $this->email($email);
270 8
        $title = $title ?: $email;
271 8
        $email = $this->obfuscate('mailto:') . $email;
272
273 8
        return $this->toHtmlString(
274 8
            '<a href="' . $email . '"' . $this->attributes($attributes) . '>' .
275 8
                ($escaped ? $this->entities($title) : $title) .
276 2
            '</a>'
277 6
        );
278
    }
279
280
    /**
281
     * Obfuscate an e-mail address to prevent spam-bots from sniffing it.
282
     *
283
     * @param  string  $email
284
     *
285
     * @return string
286
     */
287 8
    public function email($email)
288
    {
289 8
        return str_replace('@', '&#64;', $this->obfuscate($email));
290
    }
291
292
    /**
293
     * Generate an ordered list of items.
294
     *
295
     * @param  array  $list
296
     * @param  array  $attributes
297
     *
298
     * @return \Illuminate\Support\HtmlString
299
     */
300 16
    public function ol(array $list, array $attributes = [])
301
    {
302 16
        return $this->toHtmlString(
303 16
            Helpers\Lister::make('ol', $list, $attributes)
304 12
        );
305
    }
306
307
    /**
308
     * Generate an un-ordered list of items.
309
     *
310
     * @param  array  $list
311
     * @param  array  $attributes
312
     *
313
     * @return \Illuminate\Support\HtmlString
314
     */
315 16
    public function ul(array $list, array $attributes = [])
316
    {
317 16
        return $this->toHtmlString(
318 16
            Helpers\Lister::make('ul', $list, $attributes)
319 12
        );
320
    }
321
322
    /**
323
     * Generate a description list of items.
324
     *
325
     * @param  array  $list
326
     * @param  array  $attributes
327
     *
328
     * @return \Illuminate\Support\HtmlString
329
     */
330 24
    public function dl(array $list, array $attributes = [])
331
    {
332 24
        $attributes = $this->attributes($attributes);
333 24
334 12
        $html = "<dl{$attributes}>";
335
336
        foreach ($list as $key => $value) {
337
            $value = (array) $value;
338
            $html .= "<dt>$key</dt>";
339
340
            foreach ($value as $vKey => $vValue) {
341
                $html .= "<dd>$vValue</dd>";
342
            }
343
        }
344 856
345
        $html .= '</dl>';
346 856
347
        return $this->toHtmlString($html);
348
    }
349
350
    /**
351
     * Generates non-breaking space entities based on a supplied multiplier.
352
     *
353
     * @param  int  $multiplier
354
     *
355
     * @return string
356 16
     */
357
    public function nbsp($multiplier = 1)
358 16
    {
359
        return str_repeat('&nbsp;', $multiplier);
360
    }
361
362
    /**
363
     * Build an HTML attribute string from an array.
364
     *
365
     * @param  array  $attributes
366
     *
367
     * @return string
368
     */
369
    public function attributes(array $attributes)
370 32
    {
371
        return Helpers\Attributes::make($attributes);
372 32
    }
373 32
374 24
    /**
375
     * Obfuscate a string to prevent spam-bots from sniffing it.
376
     *
377
     * @param  string  $value
378
     *
379
     * @return string
380
     */
381
    public function obfuscate($value)
382
    {
383
        return Helpers\Obfuscater::make($value);
384
    }
385
386
    /**
387
     * Generate a meta tag.
388
     *
389
     * @param  string  $name
390
     * @param  string  $content
391
     * @param  array   $attributes
392
     *
393
     * @return \Illuminate\Support\HtmlString
394
     */
395
    public function meta($name, $content, array $attributes = [])
396
    {
397
        return $this->toHtmlString(
398
            Helpers\Meta::make($name, $content, $attributes)
399
        );
400
    }
401
}
402