Completed
Push — master ( 04698e...371829 )
by Revin
03:28
created

Stack::render()   B

Complexity

Conditions 5
Paths 16

Size

Total Lines 24
Code Lines 16

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 30

Importance

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

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
124
125 1
        $this->options['tag'] = $tag;
126
127 1
        return $this;
128
    }
129
130
    /**
131
     * @deprecated
132
     * @param string|null $tag
133
     * @param array $options
134
     * @return string
135
     * @throws \yii\base\InvalidConfigException
136
     */
137
    public function render($tag = null, $options = [])
138
    {
139
        $tag = empty($tag)
140
            ? (empty($this->tag) ? static::$defaultTag : $this->tag)
0 ignored issues
show
Deprecated Code introduced by
The property rmrevin\yii\fontawesome\component\Stack::$tag has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
Deprecated Code introduced by
The property rmrevin\yii\fontawesome\...nent\Stack::$defaultTag has been deprecated.

This property has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the property will be removed from the class and what other property to use instead.

Loading history...
141
            : $tag;
142
143
        $options = array_merge($this->options, $options);
144
145
        $template = ArrayHelper::remove($options, 'template', '{back}{front}');
146
147
        $icon_back = $this->icon_back instanceof Icon
148
            ? $this->icon_back->addCssClass(FA::$cssPrefix . '-stack-2x')
149
            : null;
150
151
        $icon_front = $this->icon_front instanceof Icon
152
            ? $this->icon_front->addCssClass(FA::$cssPrefix . '-stack-1x')
153
            : null;
154
155
        return Html::tag(
156
            $tag,
157
            str_replace(['{back}', '{front}'], [$icon_back, $icon_front], $template),
158
            $options
159
        );
160
    }
161
}
162