Passed
Push — master ( a35795...ec8de8 )
by Revin
02:05
created

Stack::__toString()   A

Complexity

Conditions 4
Paths 6

Size

Total Lines 24

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 13
CRAP Score 4.0378

Importance

Changes 0
Metric Value
dl 0
loc 24
ccs 13
cts 15
cp 0.8667
rs 9.536
c 0
b 0
f 0
cc 4
nc 6
nop 0
crap 4.0378
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
        $icon_back = $this->icon_back instanceof Icon
71 2
            ? $this->icon_back->addCssClass(FontAwesome::$basePrefix . '-stack-2x')
72 2
            : null;
73
74 2
        if ( !is_null( $this->text_front ) ) {
75
            $content_front = $this->text_front;
76
        } else {
77 2
            $content_front = $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}'], [$icon_back, $content_front], $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
    public function text($text = '', $options = [])
109
    {
110
111
        $tag = ArrayHelper::remove($options, 'tag', 'span');
112
113
        Html::addCssClass( $options , FontAwesome::$basePrefix . '-stack-1x');
114
115
        $this->text_front = Html::tag( $tag , $text , $options );
116
117
        return $this;
118
    }
119
120
    /**
121
     * @param string|Icon $icon
122
     * @param array $options
123
     * @return \rmrevin\yii\fontawesome\component\Stack
124
     */
125 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...
126
    {
127 2
        if (is_string($icon)) {
128 1
            $icon = new Icon($this->iconCssPrefix, $icon, $options);
129 1
        }
130
131 2
        $this->icon_back = $icon;
132
133 2
        return $this;
134
    }
135
}
136