Passed
Pull Request — master (#4)
by
unknown
04:09
created

LaravelFontAwesome::buildIcon()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

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