Completed
Push — master ( 7e2aaa...d03d67 )
by Revin
02:22
created

Stack::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 3
Bugs 0 Features 0
Metric Value
c 3
b 0
f 0
dl 0
loc 6
rs 9.4285
ccs 4
cts 4
cp 1
cc 1
eloc 3
nc 1
nop 1
crap 1
1
<?php
2
/**
3
 * Stack.php
4
 * @author Revin Roman
5
 * @link https://rmrevin.ru
6
 */
7
8
namespace rmrevin\yii\fontawesome\component;
9
10
use yii\helpers\ArrayHelper;
11
use yii\helpers\Html;
12
13
/**
14
 * Class Stack
15
 * @package rmrevin\yii\fontawesome\component
16
 */
17
class Stack
18
{
19
20
    /** @var string */
21
    public static $defaultTag = 'span';
22
23
    /** @var string */
24
    private $tag;
25
26
    /** @var array */
27
    private $options = [];
28
29
    /** @var Icon */
30
    private $icon_front;
31
32
    /** @var Icon */
33
    private $icon_back;
34
35
    /**
36
     * @param array $options
37
     */
38 2
    public function __construct($options = [])
39
    {
40 2
        Html::addCssClass($options, 'fa-stack');
41
42 2
        $this->options = $options;
43 2
    }
44
45
    /**
46
     * @return string
47
     */
48 1
    public function __toString()
49
    {
50 1
        return $this->render();
51
    }
52
53
    /**
54
     * @param string|Icon $icon
55
     * @param array $options
56
     * @return self
57
     */
58 1
    public function icon($icon, $options = [])
59
    {
60 1
        if (is_string($icon)) {
61 1
            $icon = new Icon($icon, $options);
62 1
        }
63
64 1
        $this->icon_front = $icon;
65
66 1
        return $this;
67
    }
68
69
    /**
70
     * @param string|Icon $icon
71
     * @param array $options
72
     * @return self
73
     */
74 1
    public function on($icon, $options = [])
75
    {
76 1
        if (is_string($icon)) {
77 1
            $icon = new Icon($icon, $options);
78 1
        }
79
80 1
        $this->icon_back = $icon;
81
82 1
        return $this;
83
    }
84
85
    /**
86
     * Change html tag.
87
     * @param string $tag
88
     * @return static
89
     * @throws \yii\base\InvalidParamException
90
     */
91 1
    public function tag($tag)
92
    {
93 1
        $this->tag = $tag;
94
95 1
        return $this;
96
    }
97
98
    /**
99
     * @param string|null $tag
100
     * @param array $options
101
     * @return string
102
     * @throws \yii\base\InvalidConfigException
103
     */
104 1
    public function render($tag = null, $options = [])
105
    {
106 1
        $tag = empty($tag)
107 1
            ? (empty($this->tag) ? static::$defaultTag : $this->tag)
108 1
            : $tag;
109
110 1
        $template = ArrayHelper::remove($options, 'template', '{back}{front}');
111
112 1
        $options = array_merge($this->options, $options);
113
114 1
        $icon_back = $this->icon_back instanceof Icon
115 1
            ? $this->icon_back->addCssClass('fa-stack-2x')
116 1
            : null;
117
118 1
        $icon_front = $this->icon_front instanceof Icon
119 1
            ? $this->icon_front->addCssClass('fa-stack-1x')
120 1
            : null;
121
122 1
        return Html::tag(
123 1
            $tag,
124 1
            str_replace(['{back}', '{front}'], [$icon_back, $icon_front], $template),
125
            $options
126 1
        );
127
    }
128
}