Link.php
2.05 KB
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
<?php
/**
* Created by PhpStorm.
* User: Zip
* Date: 14/12/16
* Time: 下午12:58
*/
namespace Hood\Helper\View;
class Link extends ViewAbstract
{
protected $_mediaTypes = array(
'charset', 'href', 'hreflang', 'id', 'media', 'rel', 'rev', 'type', 'title', 'extras'
);
protected $_mediaList = array(
'rel="stylesheet"',
'type="text/css"',
'media="screen"'
);
public function offsetSetFile($index, $src, $conditional = '')
{
$this->_item[$index] = $src;
if (!empty($conditional)) {
$this->_target_conditional[$index] = $conditional;
}
return $this;
}
public function appendFile($src)
{
$this->_item[] = $src;
}
public function setMedia(array $media)
{
foreach ($media as $_media => $val) {
if (isset($this->_mediaTypes[$_media]) && !empty($val) && is_string($val)) {
$this->_mediaList[] = sprintf(' %s="%s"', $_media, htmlspecialchars($val, ENT_COMPAT, $this->enc));
}
}
return $this;
}
public function conditional($conditional = 'lt IE 9')
{
assert(is_string($conditional));
$this->conditional = $conditional;
return $this;
}
public function __toString()
{
$html = '';
$_mediaString = implode(' ', $this->_mediaList);
foreach ($this->_item as $key => $val) {
if (empty($val)) {
continue;
}
$linkString = '<link href="' . $val . '" ' . $_mediaString . ' />' . PHP_EOL;
if (isset($this->_target_conditional[$key])) {
$html .= '<!--[if ' . $this->_target_conditional[$key] . ']> ' . PHP_EOL . $linkString . '<![endif]-->' . PHP_EOL;
} else {
$html .= $linkString;
}
}
if (!empty($this->conditional) && empty($this->_target_conditional)) {
$html = '<!--[if ' . $this->conditional . ']>' . PHP_EOL . $html . ' <![endif]-->' . PHP_EOL;
}
return $html;
}
}