Completed
Pull Request — master (#5)
by ARCANEDEV
05:05
created

HtmlBuilder::image()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

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