Completed
Push — master ( 2efbfa...97fde8 )
by Revin
03:44
created

Stack::__toString()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

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