1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SocialLinks\Metas; |
4
|
|
|
|
5
|
|
|
use SocialLinks\Page; |
6
|
|
|
use ArrayObject; |
7
|
|
|
|
8
|
|
|
/** |
9
|
|
|
* Base class extended by all metas. |
10
|
|
|
*/ |
11
|
|
|
abstract class MetaBase extends ArrayObject |
12
|
|
|
{ |
13
|
|
|
const META_ATTRIBUTE_NAME = 'name'; |
14
|
|
|
const META_NAME_PREFIX = ''; |
15
|
|
|
|
16
|
|
|
protected $page; |
17
|
|
|
protected static $characterLimits = array(); |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Constructor. |
21
|
|
|
* |
22
|
|
|
* @param Page $page |
23
|
|
|
*/ |
24
|
|
|
public function __construct(Page $page) |
25
|
|
|
{ |
26
|
|
|
$this->page = $page; |
27
|
|
|
$this->generateTags(); |
28
|
|
|
} |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* Convert all tags to html |
32
|
|
|
* |
33
|
|
|
* @return string |
34
|
|
|
*/ |
35
|
|
|
public function __toString() |
36
|
|
|
{ |
37
|
|
|
$html = []; |
38
|
|
|
|
39
|
|
|
foreach ($this as $tag) { |
40
|
|
|
if (is_array($tag)) { |
41
|
|
|
$html = array_merge($html, $tag); |
42
|
|
|
} else { |
43
|
|
|
$html[] = $tag; |
44
|
|
|
} |
45
|
|
|
} |
46
|
|
|
|
47
|
|
|
return implode("\n", $html); |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Generate all tags. |
52
|
|
|
* |
53
|
|
|
* @return array |
54
|
|
|
*/ |
55
|
|
|
abstract protected function generateTags(); |
56
|
|
|
|
57
|
|
|
/** |
58
|
|
|
* {@inheritdoc} |
59
|
|
|
*/ |
60
|
|
View Code Duplication |
public function addMeta($name, $content) |
|
|
|
|
61
|
|
|
{ |
62
|
|
|
if (is_array($content)) { |
63
|
|
|
$content = array_map(function ($content) use ($name) { |
64
|
|
|
return static::getHtmlMeta($name, $content); |
65
|
|
|
}, array_filter($content)); |
66
|
|
|
} elseif (!empty($content)) { |
67
|
|
|
$content = static::getHtmlMeta($name, $content); |
68
|
|
|
} else { |
69
|
|
|
return; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
$this[$name] = $content; |
73
|
|
|
} |
74
|
|
|
|
75
|
|
|
/** |
76
|
|
|
* {@inheritdoc} |
77
|
|
|
*/ |
78
|
|
View Code Duplication |
public function addLink($rel, $href) |
|
|
|
|
79
|
|
|
{ |
80
|
|
|
if (is_array($href)) { |
81
|
|
|
$href = array_map(function ($href) use ($rel) { |
82
|
|
|
return static::getHtmlLink($rel, $href); |
83
|
|
|
}, array_filter($href)); |
84
|
|
|
} elseif (!empty($href)) { |
85
|
|
|
$href = static::getHtmlLink($rel, $href); |
86
|
|
|
} else { |
87
|
|
|
return; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
$this[$rel] = $href; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Adds an array of metas. |
95
|
|
|
* |
96
|
|
|
* @param array $metas |
97
|
|
|
*/ |
98
|
|
|
protected function addMetas(array $metas) |
99
|
|
|
{ |
100
|
|
|
foreach ($metas as $name => $content) { |
101
|
|
|
if (!empty($content)) { |
102
|
|
|
$this->addMeta($name, $content); |
103
|
|
|
} |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Adds an array of links. |
109
|
|
|
* |
110
|
|
|
* @param array $links |
111
|
|
|
*/ |
112
|
|
|
protected function addLinks(array $links) |
113
|
|
|
{ |
114
|
|
|
foreach ($links as $rel => $href) { |
115
|
|
|
if (!empty($href)) { |
116
|
|
|
$this->addLink($rel, $href); |
117
|
|
|
} |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Escapes the value of an attribute. |
123
|
|
|
* |
124
|
|
|
* @param string $value |
125
|
|
|
* |
126
|
|
|
* @return string |
127
|
|
|
*/ |
128
|
|
|
protected static function escape($value) |
129
|
|
|
{ |
130
|
|
|
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
/** |
134
|
|
|
* Generates the html code of a link |
135
|
|
|
* |
136
|
|
|
* @param string $rel |
137
|
|
|
* @param string $href |
138
|
|
|
*/ |
139
|
|
|
protected static function getHtmlLink($rel, $href) |
140
|
|
|
{ |
141
|
|
|
return '<link rel="'.static::escape($rel).'" href="'.static::escape($href).'">'; |
142
|
|
|
} |
143
|
|
|
|
144
|
|
|
/** |
145
|
|
|
* Generates the html code of a meta |
146
|
|
|
* |
147
|
|
|
* @param string $name |
148
|
|
|
* @param string $content |
149
|
|
|
*/ |
150
|
|
|
protected static function getHtmlMeta($name, $content) |
151
|
|
|
{ |
152
|
|
|
$content = static::trim($name, $content); |
153
|
|
|
|
154
|
|
|
return '<meta '.static::META_ATTRIBUTE_NAME.'="'.static::META_NAME_PREFIX.static::escape($name).'" content="'.static::escape($content).'">'; |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Filters attribute values to trim by length. |
159
|
|
|
* |
160
|
|
|
* @param string $name |
161
|
|
|
* @param string $content |
162
|
|
|
* |
163
|
|
|
* @return string |
164
|
|
|
*/ |
165
|
|
|
protected static function trim($name, $content) |
166
|
|
|
{ |
167
|
|
|
$limit = isset(static::$characterLimits[$name]) ? static::$characterLimits[$name] : null; |
168
|
|
|
|
169
|
|
|
if ($limit && strlen($content) > $limit) { |
170
|
|
|
$content = substr($content, 0, $limit - 3).'...'; |
171
|
|
|
} |
172
|
|
|
|
173
|
|
|
return $content; |
174
|
|
|
} |
175
|
|
|
} |
176
|
|
|
|
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.