HeadTagTrait::favicon()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php /** MicroHeadTagTrait */
2
3
namespace Micro\Web\Html;
4
5
6
/**
7
 * HeadTagTrait trait file.
8
 *
9
 * @author Oleg Lunegov <[email protected]>
10
 * @link https://github.com/linpax/microphp-framework
11
 * @copyright Copyright (c) 2013 Oleg Lunegov
12
 * @license https://github.com/linpax/microphp-framework/blob/master/LICENSE
13
 * @package Micro
14
 * @subpackage Web\Html
15
 * @version 1.0
16
 * @since 1.0
17
 */
18
trait HeadTagTrait
19
{
20
    use TagTrait;
21
22
23
    /**
24
     * @var array $docTypes Document types
25
     */
26
    private static $docTypes = [
27
        'xhtml11' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">',
28
        'xhtml1-trans' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
29
        'xhtml1-strict' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">',
30
        'xhtml1-frame' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Frameset//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-frameset.dtd">',
31
        'html4-trans' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">',
32
        'html4-strict' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">',
33
        'html4-frame' => '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Frameset//EN" "http://www.w3.org/TR/html4/frameset.dtd">',
34
        'html5' => '<!DOCTYPE html>'
35
    ];
36
37
38
    /**
39
     * Render meta tag
40
     *
41
     * @access public
42
     *
43
     * @param  string $name name of element
44
     * @param  string $content content of element
45
     * @param  array $attributes attributes tag
46
     *
47
     * @return string
48
     * @static
49
     */
50
    public static function meta($name, $content, array $attributes = [])
51
    {
52
        return static::tag('meta', array_merge($attributes, [
53
            'name'=> $name,
54
            'content' => $content
55
        ]));
56
    }
57
58
    /**
59
     * Render link tag
60
     *
61
     * @access public
62
     *
63
     * @param  string $name name of element
64
     * @param  string $url url path
65
     * @param  array $attributes attributes tag
66
     *
67
     * @return string
68
     * @static
69
     */
70
    public static function link($name, $url, array $attributes = [])
71
    {
72
        return static::openTag('link', array_merge($attributes, ['href' => $url])).
73
            $name.
74
            static::closeTag('link');
75
    }
76
77
    /**
78
     * Render favicon file
79
     *
80
     * @access public
81
     *
82
     * @param string $url path to favicon
83
     *
84
     * @return string
85
     * @static
86
     */
87
    public static function favicon($url)
88
    {
89
        return static::tag('link', ['href' => $url, 'rel' => 'shortcut icon', 'type' => 'image/x-icon']);
90
    }
91
92
    /**
93
     * Render css file
94
     *
95
     * @access public
96
     *
97
     * @param  string $file path to css
98
     *
99
     * @return string
100
     * @static
101
     */
102
    public static function cssFile($file)
103
    {
104
        return static::tag('link', ['href' => $file, 'rel' => 'stylesheet']);
105
    }
106
107
    /**
108
     * Render script file
109
     *
110
     * @access public
111
     *
112
     * @param  string $file path to script
113
     *
114
     * @return string
115
     * @static
116
     */
117
    public static function scriptFile($file)
118
    {
119
        return static::openTag('script', ['src' => $file, 'type' => 'text/javascript']).static::closeTag('script');
120
    }
121
122
    /**
123
     * Render style source
124
     *
125
     * @access public
126
     *
127
     * @param  string $text style
128
     * @param  array $attributes attributes tag
129
     *
130
     * @return string
131
     * @static
132
     */
133
    public static function css($text, array $attributes = [])
134
    {
135
        return static::openTag('style', array_merge($attributes, ['type' => 'text/css'])).
136
            $text.
137
            static::closeTag('style');
138
    }
139
140
    /**
141
     * Render script source
142
     *
143
     * @access public
144
     *
145
     * @param string $text script
146
     * @param array $attributes attributes tag
147
     * @param string $type type of script
148
     *
149
     * @return string
150
     * @static
151
     */
152
    public static function script($text, array $attributes = [], $type = 'text/javascript')
153
    {
154
        return static::openTag('script',
155
            array_merge($attributes, ['type' => $type])).
156
            ' /*<![CDATA[*/ '.$text.' /*]]>*/ '.
157
            static::closeTag('script');
158
    }
159
160
    /**
161
     * Render docType tag
162
     *
163
     * @access public
164
     *
165
     * @param  string $name doctype name
166
     *
167
     * @return string|boolean
168
     * @static
169
     */
170
    public static function doctype($name)
171
    {
172
        return empty(static::$docTypes[$name]) ? false : static::$docTypes[$name];
0 ignored issues
show
Bug introduced by
Since $docTypes is declared private, accessing it with static will lead to errors in possible sub-classes; consider using self, or increasing the visibility of $docTypes to at least protected.

Let’s assume you have a class which uses late-static binding:

class YourClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return static::$someVariable;
    }
}

The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the getSomeVariable() on that sub-class, you will receive a runtime error:

class YourSubClass extends YourClass { }

YourSubClass::getSomeVariable(); // Will cause an access error.

In the case above, it makes sense to update SomeClass to use self instead:

class SomeClass
{
    private static $someVariable;

    public static function getSomeVariable()
    {
        return self::$someVariable; // self works fine with private.
    }
}
Loading history...
173
    }
174
175
    /**
176
     * Render title tag
177
     *
178
     * @access public
179
     *
180
     * @param string $name title name
181
     *
182
     * @return string
183
     * @static
184
     */
185
    public static function title($name)
186
    {
187
        return static::openTag('title').$name.static::closeTag('title');
188
    }
189
}
190