Blame view

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