Icon::activeFieldAddon()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 2
dl 0
loc 7
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
 * Provides a quick and easy way to access icons.
11
 *
12
 * ```php
13
 * $icon = new \thoulah\fontawesome\Icon();
14
 * echo $icon->show('at');
15
 * echo $icon->show('github', ['style' => 'brands', 'fill' => '#003865']);
16
 * echo $icon->show('font-awesome', ['class' => 'yourClass', 'style' => 'brands']);
17
 * ```
18
 */
19
class Icon
20
{
21
    /** @var Defaults default settings */
22
    public $defaults;
23
24
    /** @var string fully qualified name of Html */
25
    private $html;
26
27
    /**
28
     * Creates a new Icon object.
29
     *
30
     * @param array|null $config configuration of the icon
31
     */
32
    public function __construct(array $config = [])
33
    {
34
        $this->defaults = new Defaults($config);
35
        $this->html = __NAMESPACE__ . "\\{$this->defaults->bootstrap}\\Html";
36
    }
37
38
    /**
39
     * Returns the ActiveField inputTemplate.
40
     *
41
     * @param string     $name    Name of the icon
42
     * @param array|null $options Options for the field and the icon
43
     *
44
     * @return string ActiveField addon with icon and proper code
45
     */
46
    public function activeFieldAddon(string $name, array $options = []): string
47
    {
48
        $groupSize = ArrayHelper::remove($options, 'groupSize', $this->defaults->groupSize);
49
        $append = ArrayHelper::getValue($options, 'append', $this->defaults->append);
50
        $icon = forward_static_call([$this->html, 'activeFieldAddon'], $groupSize, $append);
51
52
        return str_replace('{icon}', $this->activeFieldIcon($name, $options), $icon);
53
    }
54
55
    /**
56
     * Returns the ActiveField Icon.
57
     *
58
     * @param string     $name    Name of the icon
59
     * @param array|null $options Options for the field and the icon
60
     *
61
     * @return string ActiveField icon with proper code
62
     */
63
    public function activeFieldIcon(string $name, array $options = []): string
64
    {
65
        $append = ArrayHelper::remove($options, 'append', $this->defaults->append);
66
        $icon = forward_static_call([$this->html, 'activeFieldIcon'], $append);
67
68
        return str_replace('{icon}', $this->show($name, $options), $icon);
69
    }
70
71
    /**
72
     * Outputs the SVG string.
73
     *
74
     * @param string     $name    Name of the icon, or filename
75
     * @param array|null $options Options for the icon
76
     *
77
     * @return string The icon
78
     */
79
    public function show(string $name, array $options = []): string
80
    {
81
        ArrayHelper::setValue($options, 'name', $name);
82
83
        $image = new Image($this->defaults);
84
85
        return $image->get($options);
86
    }
87
}
88