IconComponent::name()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 3
c 1
b 0
f 0
nc 1
nop 2
dl 0
loc 6
rs 10
1
<?php
2
3
namespace thoulah\fontawesome;
4
5
use thoulah\fontawesome\config\Defaults;
6
use thoulah\fontawesome\helpers\Image;
7
use yii\helpers\ArrayHelper;
8
9
/**
10
 * IconComponent provides an easy way to access Font Awesome icons throughout your project.
11
 * This allows you to override default settings once instead of per usage or file.
12
 *
13
 * Add IconComponent as component to your Yii config file:
14
 *
15
 * ```php
16
 * 'components' => [
17
 *     'fontawesome' => [
18
 *         'class' => thoulah\fontawesome\IconComponent::class,
19
 * //      'fontAwesomeFolder' => '@npm/fontawesome-pro/svgs',
20
 * //      'style' => 'regular',
21
 *     ]
22
 * ]
23
 * ```
24
 *
25
 * Now you can globally insert an icon:
26
 *
27
 * ```php
28
 * echo Yii::$app->fontawesome->name('at');
29
 * echo Yii::$app->fontawesome->name('github', 'brands')->fill->('#003865');
30
 * echo Yii::$app->fontawesome->name('font-awesome', 'brands')->class('yourClass');
31
 * ```
32
 *
33
 * @method self append() append(bool $append)             Whether to prepend or append the `input-group`
34
 * @method self class() class(string $class)              Additional custom classes
35
 * @method self css() css(array $css)                     Custom CSS style
36
 * @method self fill() fill(string $fill)                 Color of the icon
37
 * @method self fixedWidth() fixedWidth(bool $fixedWidth) Whether or not to have fixed width icons
38
 * @method self groupSize() groupSize(string $groupSize)  Set to `sm` for small or `lg` for large
39
 * @method self height() height(int $height)              The height of the icon, will override height and width classes
40
 * @method self id() id(string $id)                       ID for the SVG tag
41
 * @method self title() title(string $title)              Sets a title to the SVG output
42
 * @method self width() width(int $width)                 The width of the icon, will override height and width classes
43
 */
44
class IconComponent extends \yii\base\Component
45
{
46
    /** @var Defaults default settings */
47
    public $defaults;
48
49
    /** @var string fully qualified name of Html */
50
    private $html;
51
52
    /** @var array icon options */
53
    private $icon = [];
54
55
    /**
56
     * Creates a new IconComponent object.
57
     *
58
     * @param array|null $overrides Overrides of the default settings
59
     */
60
    public function __construct(array $overrides = [])
61
    {
62
        $this->defaults = new Defaults($overrides);
63
        $this->html = __NAMESPACE__ . "\\{$this->defaults->bootstrap}\\Html";
64
    }
65
66
    /**
67
     * Magic function, sets icon properties.
68
     *
69
     * Supported options are listed in @method, but
70
     * [no support](https://github.com/yiisoft/yii2-apidoc/issues/136) in the docs yet.
71
     *
72
     * @param string $name  property name
73
     * @param array  $value property value
74
     *
75
     * @return self updated object
76
     */
77
    public function __call($name, $value): self
78
    {
79
        $this->icon[$name] = $value[0];
80
81
        return $this;
82
    }
83
84
    /**
85
     * Magic function, returns the SVG string.
86
     *
87
     * @return string SVG data
88
     */
89
    public function __toString(): string
90
    {
91
        $image = new Image($this->defaults);
92
        $image->get($this->icon);
93
        $this->icon = [];
94
95
        return $image;
96
    }
97
98
    /**
99
     * Returns the ActiveField inputTemplate.
100
     *
101
     * @param string      $name  name of the icon, or filename
102
     * @param string|null $style style of the icon
103
     *
104
     * @return string ActiveField addon with icon and proper code
105
     */
106
    public function activeFieldAddon(string $name, string $style = null): string
107
    {
108
        $groupSize = ArrayHelper::remove($this->icon, 'groupSize', $this->defaults->groupSize);
109
        $append = ArrayHelper::getValue($this->icon, 'append', $this->defaults->append);
110
        $icon = forward_static_call([$this->html, 'activeFieldAddon'], $groupSize, $append);
111
112
        return str_replace('{icon}', $this->activeFieldIcon($name, $style), $icon);
113
    }
114
115
    /**
116
     * Returns the ActiveField Icon.
117
     *
118
     * @param string      $name  name of the icon, or filename
119
     * @param string|null $style style of the icon
120
     *
121
     * @return string ActiveField icon with proper code
122
     */
123
    public function activeFieldIcon(string $name, string $style = null): string
124
    {
125
        $this->name($name, $style);
126
        if (!isset($this->icon['fixedWidth'])) {
127
            ArrayHelper::setValue($this->icon, 'fixedWidth', $this->defaults->activeFormFixedWidth);
128
        }
129
130
        $append = ArrayHelper::remove($this->icon, 'append', $this->defaults->append);
131
        $icon = forward_static_call([$this->html, 'activeFieldIcon'], $append);
132
133
        return str_replace('{icon}', $this, $icon);
134
    }
135
136
    /**
137
     * Sets the name and style of the icon.
138
     *
139
     * @param string      $name  name of the icon, or filename
140
     * @param string|null $style style of the icon
141
     *
142
     * @return self component object
143
     */
144
    public function name(string $name, string $style = null): self
145
    {
146
        $this->icon['name'] = $name;
147
        $this->icon['style'] = $style ?? $this->defaults->style;
148
149
        return $this;
150
    }
151
}
152