Completed
Push — master ( 5b5c65...0266b3 )
by Revin
02:48 queued 52s
created

Stack::text()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 10
c 0
b 0
f 0
ccs 5
cts 5
cp 1
rs 9.9332
cc 1
nc 1
nop 2
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 rmrevin\yii\fontawesome\FontAwesome;
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
     * @var string
22
     */
23
    private $iconCssPrefix = 'fa';
24
25
    /**
26
     * @var array
27
     */
28
    private $options = [];
29
30
    /**
31
     * @var Icon
32
     */
33
    private $icon_front;
34
35
    /**
36
     * @var string
37
     */
38
    private $text_front = null;
39
40
    /**
41
     * @var Icon
42
     */
43
    private $icon_back;
44
45
    /**
46
     * @param string $iconCssPrefix
47
     * @param array $options
48
     */
49 3
    public function __construct($iconCssPrefix, $options = [])
50
    {
51 3
        $this->iconCssPrefix = $iconCssPrefix;
52
53 3
        Html::addCssClass($options, FontAwesome::$basePrefix . '-stack');
54
55 3
        $this->options = $options;
56 3
    }
57
58
    /**
59
     * @return string
60
     * @throws \yii\base\InvalidConfigException
61
     */
62 2
    public function __toString()
63
    {
64 2
        $options = $this->options;
65
66 2
        $tag = ArrayHelper::remove($options, 'tag', 'span');
67
68 2
        $template = ArrayHelper::remove($options, 'template', '{back}{front}');
69
70 2
        $iconBack = $this->icon_back instanceof Icon
71 2
            ? $this->icon_back->addCssClass(FontAwesome::$basePrefix . '-stack-2x')
72 2
            : null;
73
74 2
        if ($this->text_front !== null) {
75 1
            $contentFront = $this->text_front;
76 1
        } else {
77 2
            $contentFront = $this->icon_front instanceof Icon
78 2
                ? $this->icon_front->addCssClass(FontAwesome::$basePrefix . '-stack-1x')
79 2
                : null;
80
        }
81
82 2
        $content = str_replace(['{back}', '{front}'], [$iconBack, $contentFront], $template);
83
84 2
        return Html::tag($tag, $content, $options);
85
    }
86
87
    /**
88
     * @param string|Icon $icon
89
     * @param array $options
90
     * @return \rmrevin\yii\fontawesome\component\Stack
91
     */
92 2 View Code Duplication
    public function icon($icon, $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
93
    {
94 2
        if (is_string($icon)) {
95 1
            $icon = new Icon($this->iconCssPrefix, $icon, $options);
96 1
        }
97
98 2
        $this->icon_front = $icon;
99
100 2
        return $this;
101
    }
102
103
    /**
104
     * @param string $text
105
     * @param array $options
106
     * @return \rmrevin\yii\fontawesome\component\Stack
107
     */
108 1
    public function text($text = '', $options = [])
109
    {
110 1
        $tag = ArrayHelper::remove($options, 'tag', 'span');
111
112 1
        Html::addCssClass($options, FontAwesome::$basePrefix . '-stack-1x');
113
114 1
        $this->text_front = Html::tag($tag, $text, $options);
115
116 1
        return $this;
117
    }
118
119
    /**
120
     * @param string|Icon $icon
121
     * @param array $options
122
     * @return \rmrevin\yii\fontawesome\component\Stack
123
     */
124 2 View Code Duplication
    public function on($icon, $options = [])
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
125
    {
126 2
        if (is_string($icon)) {
127 1
            $icon = new Icon($this->iconCssPrefix, $icon, $options);
128 1
        }
129
130 2
        $this->icon_back = $icon;
131
132 2
        return $this;
133
    }
134
}
135