Completed
Push — master ( 00fc20...13fe35 )
by
unknown
16:01 queued 10:37
created

LaravelFontAwesome::getOptions()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
3
namespace PendoNL\LaravelFontAwesome;
4
5
class LaravelFontAwesome
6
{
7
    public function icons($version = null)
8
    {
9
        if (is_null($version)) {
10
            $version = config('laravel-fontawesome.default_version');
11
        }
12
13
        return include __DIR__.'/versions/v'.$version.'/icons.php';
14
    }
15
16
    public function icon($icon, $options = [])
17
    {
18
        $options = $this->getOptions($options);
19
20
        $classes = $this->buildIconClasses($icon, (isset($options['class']) ? $options['class'] : ''));
21
22
        $attributes = $this->buildAttributes($options, $classes);
23
24
        return $this->buildIcon($attributes);
25
    }
26
27
    /**
28
     * Strip the default fa- prefix from the icon name if provider.
29
     *
30
     * @param $icon
31
     *
32
     * @return string
33
     */
34
    protected function getIcon($icon)
35
    {
36
        return 'fa-'.str_replace('fa-', '', $icon);
37
    }
38
39
    /**
40
     * Get the icon options. If no array is given cast the option to a
41
     * string and assume it is an extra class name.
42
     *
43
     * @param $options
44
     *
45
     * @return array
46
     */
47
    protected function getOptions($options)
48
    {
49
        if (!is_array($options)) {
50
            $options = [
51
                'class' => (string) $options,
52
            ];
53
        }
54
55
        return $options;
56
    }
57
58
    /**
59
     * Gets the list of class names to add to the icon element we are about to generate.
60
     *
61
     * @param $icon
62
     * @param $extraClasses
63
     *
64
     * @return string
65
     */
66
    protected function buildIconClasses($icon, $extraClasses)
67
    {
68
        return 'fa '.$this->getIcon($icon).($extraClasses != '' ? ' '.$extraClasses : '');
69
    }
70
71
    /**
72
     * Build the attribute list to add to the icon we are about to generate.
73
     *
74
     * @param $options
75
     * @param $classes
76
     *
77
     * @return string
78
     */
79
    protected function buildAttributes($options, $classes)
80
    {
81
        $attributes = [];
82
        $attributes[] = 'class="'.$classes.'"';
83
84
        unset($options['class']);
85
86
        if (is_array($options)) {
87
            foreach ($options as $attribute => $value) {
88
                $attributes[] = $this->createAttribute($attribute, $value);
89
            }
90
        }
91
92
        return (count($attributes) > 0) ? implode(' ', $attributes) : '';
93
    }
94
95
    /**
96
     * Build the attribute.
97
     *
98
     * @param $attribute
99
     * @param $value
100
     *
101
     * @return string
102
     */
103
    protected function createAttribute($attribute, $value)
104
    {
105
        return $attribute.'="'.$value.'"';
106
    }
107
108
    /**
109
     * Build the icon with the correct attributes.
110
     *
111
     * @param $attributes
112
     *
113
     * @return string
114
     */
115
    protected function buildIcon($attributes)
116
    {
117
        return '<i '.$attributes.'></i>';
118
    }
119
}
120