HtmlHelper::removeCss()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
nc 1
nop 1
dl 0
loc 6
rs 10
c 0
b 0
f 0
1
<?php
2
namespace Fwlib\Web;
3
4
use Fwlib\Base\SingleInstanceTrait;
5
6
/**
7
 * Helper class for generate html output
8
 *
9
 *
10
 * Css and js reference(path) are stored here, for various class to access
11
 * shared css and js collection, then output them in result html. This class
12
 * only provide accessors, no html generation here.
13
 *
14
 * Root path should set at application beginning, is a path to public root,
15
 * used for generate path of other resources, these path usually used in url.
16
 *
17
 *
18
 * @copyright   Copyright 2015 Fwolf
19
 * @license     http://www.gnu.org/licenses/lgpl.html LGPL-3.0+
20
 */
21
class HtmlHelper
22
{
23
    use SingleInstanceTrait;
24
25
26
    /**
27
     * Css references
28
     *
29
     * Format: {name: {href, media}}
30
     *
31
     * For media type and query:
32
     * @see https://developer.mozilla.org/en-US/docs/Web/CSS/@media
33
     * @see https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Media_queries
34
     *
35
     * @var array
36
     */
37
    protected $css = [];
38
39
    /**
40
     * Javascript references
41
     *
42
     * @var string[]
43
     */
44
    protected $javascript = [];
45
46
    /**
47
     * @var string
48
     */
49
    protected $rootPath = '';
50
51
52
    /**
53
     * Add a css reference
54
     *
55
     * @param   string  $name
56
     * @param   string  $href
57
     * @param   string  $media
58
     * @return  static
59
     */
60
    public function addCss($name, $href, $media = 'all')
61
    {
62
        $this->css[$name] = [
63
            'href'  => $href,
64
            'media' => $media,
65
        ];
66
67
        return $this;
68
    }
69
70
71
    /**
72
     * Add a js reference
73
     *
74
     * @param   string  $name
75
     * @param   string  $path
76
     * @return  static
77
     */
78
    public function addJs($name, $path)
79
    {
80
        $this->javascript[$name] = $path;
81
82
        return $this;
83
    }
84
85
86
    /**
87
     * Clear all css reference
88
     *
89
     * @return  static
90
     */
91
    public function clearCss()
92
    {
93
        $this->css = [];
94
95
        return $this;
96
    }
97
98
99
    /**
100
     * Clear all js reference
101
     *
102
     * @return  static
103
     */
104
    public function clearJs()
105
    {
106
        $this->javascript = [];
107
108
        return $this;
109
    }
110
111
112
    /**
113
     * Getter of css reference
114
     *
115
     * @param   string  $name   Use '*' for all, or string name for single.
116
     * @return  array
117
     */
118 View Code Duplication
    public function getCss($name = '*')
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...
119
    {
120
        if ('*' == $name) {
121
            return $this->css;
122
123
        } elseif (array_key_exists($name, $this->css)) {
124
            return $this->css[$name];
125
126
        } else {
127
            return null;
128
        }
129
    }
130
131
132
    /**
133
     * Getter of js reference
134
     *
135
     * @param   string  $name   Use '*' for all, or string name for single.
136
     * @return  array|string
137
     */
138 View Code Duplication
    public function getJs($name = '*')
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...
139
    {
140
        if ('*' == $name) {
141
            return $this->javascript;
142
143
        } elseif (array_key_exists($name, $this->javascript)) {
144
            return $this->javascript[$name];
145
146
        } else {
147
            return null;
148
        }
149
    }
150
151
152
    /**
153
     * @return  string
154
     */
155
    public function getRootPath()
156
    {
157
        return $this->rootPath;
158
    }
159
160
161
    /**
162
     * Remove a css reference
163
     *
164
     * @param   string  $name
165
     * @return  static
166
     */
167
    public function removeCss($name)
168
    {
169
        unset($this->css[$name]);
170
171
        return $this;
172
    }
173
174
175
    /**
176
     * Remove a js reference
177
     *
178
     * @param   string  $name
179
     * @return  static
180
     */
181
    public function removeJs($name)
182
    {
183
        unset($this->javascript[$name]);
184
185
        return $this;
186
    }
187
188
189
    /**
190
     * @param   string  $rootPath
191
     * @return  static
192
     */
193
    public function setRootPath($rootPath)
194
    {
195
        if (!empty($rootPath) &&
196
            DIRECTORY_SEPARATOR != substr($rootPath, -1)
197
        ) {
198
            $rootPath .= DIRECTORY_SEPARATOR;
199
        }
200
201
        $this->rootPath = $rootPath;
202
203
        return $this;
204
    }
205
}
206