|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Pageon\Html\Meta; |
|
4
|
|
|
|
|
5
|
|
|
use Pageon\Html\Meta\Item\CharsetMeta; |
|
6
|
|
|
use Pageon\Html\Meta\Item\HttpEquivMeta; |
|
7
|
|
|
use Pageon\Html\Meta\Item\ItemPropMeta; |
|
8
|
|
|
use Pageon\Html\Meta\Item\LinkMeta; |
|
9
|
|
|
use Pageon\Html\Meta\Item\NameMeta; |
|
10
|
|
|
use Pageon\Html\Meta\Item\PropertyMeta; |
|
11
|
|
|
use Pageon\Html\Meta\Social\GooglePlusMeta; |
|
12
|
|
|
use Pageon\Html\Meta\Social\OpenGraphMeta; |
|
13
|
|
|
use Pageon\Html\Meta\Social\TwitterMeta; |
|
14
|
|
|
|
|
15
|
|
|
class Meta |
|
16
|
|
|
{ |
|
17
|
|
|
/** @var MetaItem[] */ |
|
18
|
|
|
private $meta = []; |
|
19
|
|
|
/** @var SocialMeta[] */ |
|
20
|
|
|
private $socialMeta; |
|
21
|
|
|
|
|
22
|
18 |
|
final public function __construct(string $charset = 'UTF-8') { |
|
23
|
18 |
|
$this->charset($charset); |
|
24
|
18 |
|
$this->name('viewport', 'width=device-width, initial-scale=1'); |
|
25
|
|
|
|
|
26
|
18 |
|
$this->socialMeta = [ |
|
27
|
18 |
|
new GooglePlusMeta($this), |
|
28
|
18 |
|
new TwitterMeta($this), |
|
29
|
18 |
|
new OpenGraphMeta($this), |
|
30
|
|
|
]; |
|
31
|
18 |
|
} |
|
32
|
|
|
|
|
33
|
9 |
|
public static function create(string $charset = 'UTF-8') : Meta { |
|
34
|
9 |
|
return new self($charset); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
15 |
|
public function render(array $extra = []) : string { |
|
38
|
15 |
|
$html = ''; |
|
39
|
|
|
|
|
40
|
|
|
/** |
|
41
|
|
|
* @var string $type |
|
42
|
|
|
* @var MetaItem[] $metaItems |
|
43
|
|
|
*/ |
|
44
|
15 |
|
foreach ($this->meta as $type => $metaItems) { |
|
45
|
15 |
|
foreach ($metaItems as $metaItem) { |
|
|
|
|
|
|
46
|
15 |
|
$html .= $metaItem->render($extra) . "\n"; |
|
47
|
|
|
} |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
15 |
|
return $html; |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
18 |
|
public function charset(string $charset) : Meta { |
|
54
|
18 |
|
$item = CharsetMeta::create($charset); |
|
55
|
18 |
|
$this->meta['charset'][] = $item; |
|
56
|
|
|
|
|
57
|
18 |
|
return $this; |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
18 |
|
public function name(string $name, ?string $content) : Meta { |
|
61
|
18 |
|
if (!$content) { |
|
62
|
|
|
return $this; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
18 |
|
$item = NameMeta::create($name, $content); |
|
66
|
18 |
|
$this->meta['name'][$name] = $item; |
|
67
|
|
|
|
|
68
|
18 |
|
return $this; |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
9 |
|
public function itemprop(string $name, ?string $content) : Meta { |
|
72
|
9 |
|
if (!$content) { |
|
73
|
|
|
return $this; |
|
74
|
|
|
} |
|
75
|
|
|
|
|
76
|
9 |
|
$item = ItemPropMeta::create($name, $content); |
|
77
|
9 |
|
$this->meta['itemprop'][$name] = $item; |
|
78
|
|
|
|
|
79
|
9 |
|
return $this; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
18 |
|
public function property(string $property, ?string $content) : Meta { |
|
83
|
18 |
|
if (!$content) { |
|
84
|
|
|
return $this; |
|
85
|
|
|
} |
|
86
|
|
|
|
|
87
|
18 |
|
$item = PropertyMeta::create($property, $content); |
|
88
|
18 |
|
$this->meta['property'][$property] = $item; |
|
89
|
|
|
|
|
90
|
18 |
|
return $this; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
|
|
public function httpEquiv(string $httpEquiv, ?string $content) : Meta { |
|
94
|
|
|
if (!$content) { |
|
95
|
|
|
return $this; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$item = HttpEquivMeta::create($httpEquiv, $content); |
|
99
|
|
|
$this->meta['httpEquiv'][$httpEquiv] = $item; |
|
100
|
|
|
|
|
101
|
|
|
return $this; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
4 |
|
public function link(string $rel, ?string $href) : Meta { |
|
105
|
4 |
|
if (!$href) { |
|
106
|
4 |
|
return $this; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
|
|
$item = LinkMeta::create($rel, $href); |
|
110
|
|
|
$this->meta['link'][$rel] = $item; |
|
111
|
|
|
|
|
112
|
|
|
return $this; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
6 |
|
public function title(?string $content) : Meta { |
|
116
|
6 |
|
if (!$content) { |
|
117
|
2 |
|
return $this; |
|
118
|
|
|
} |
|
119
|
|
|
|
|
120
|
4 |
|
$this->name('title', $content); |
|
121
|
|
|
|
|
122
|
4 |
|
foreach ($this->socialMeta as $socialMeta) { |
|
123
|
4 |
|
$socialMeta->title($content); |
|
124
|
|
|
} |
|
125
|
|
|
|
|
126
|
4 |
|
return $this; |
|
127
|
|
|
} |
|
128
|
|
|
|
|
129
|
5 |
|
public function description(?string $content) : Meta { |
|
130
|
5 |
|
if (!$content) { |
|
131
|
2 |
|
return $this; |
|
132
|
|
|
} |
|
133
|
|
|
|
|
134
|
3 |
|
$this->name('description', $content); |
|
135
|
|
|
|
|
136
|
3 |
|
foreach ($this->socialMeta as $socialMeta) { |
|
137
|
3 |
|
$socialMeta->description($content); |
|
138
|
|
|
} |
|
139
|
|
|
|
|
140
|
3 |
|
return $this; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
1 |
|
public function image(?string $content) : Meta { |
|
144
|
1 |
|
if (!$content) { |
|
145
|
|
|
return $this; |
|
146
|
|
|
} |
|
147
|
|
|
|
|
148
|
1 |
|
$this->name('image', $content); |
|
149
|
|
|
|
|
150
|
1 |
|
foreach ($this->socialMeta as $socialMeta) { |
|
151
|
1 |
|
$socialMeta->image($content); |
|
152
|
|
|
} |
|
153
|
|
|
|
|
154
|
1 |
|
return $this; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|