|
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
|
|
|
* Generate all tags. |
|
32
|
|
|
* |
|
33
|
|
|
* @return array |
|
34
|
|
|
*/ |
|
35
|
|
|
abstract protected function generateTags(); |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* {@inheritdoc} |
|
39
|
|
|
*/ |
|
40
|
|
|
public function addMeta($name, $content) |
|
41
|
|
|
{ |
|
42
|
|
|
$content = $this->trim($name, $content); |
|
43
|
|
|
|
|
44
|
|
|
$this[$name] = '<meta '.static::META_ATTRIBUTE_NAME.'="'.static::META_NAME_PREFIX.static::escape($name).'" content="'.static::escape($content).'">'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* {@inheritdoc} |
|
49
|
|
|
*/ |
|
50
|
|
|
public function addLink($rel, $href) |
|
51
|
|
|
{ |
|
52
|
|
|
$this[$rel] = '<link rel="'.static::escape($rel).'" href="'.static::escape($href).'">'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Adds an array of metas. |
|
57
|
|
|
* |
|
58
|
|
|
* @param array $metas |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function addMetas(array $metas) |
|
61
|
|
|
{ |
|
62
|
|
|
foreach ($metas as $name => $content) { |
|
63
|
|
|
if (!empty($content)) { |
|
64
|
|
|
$this->addMeta($name, $content); |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Adds an array of links. |
|
71
|
|
|
* |
|
72
|
|
|
* @param array $links |
|
73
|
|
|
*/ |
|
74
|
|
|
protected function addLinks(array $links) |
|
75
|
|
|
{ |
|
76
|
|
|
foreach ($links as $rel => $href) { |
|
77
|
|
|
if (!empty($href)) { |
|
78
|
|
|
$this->addLink($rel, $href); |
|
79
|
|
|
} |
|
80
|
|
|
} |
|
81
|
|
|
} |
|
82
|
|
|
|
|
83
|
|
|
/** |
|
84
|
|
|
* Escapes the value of an attribute. |
|
85
|
|
|
* |
|
86
|
|
|
* @param string $value |
|
87
|
|
|
* |
|
88
|
|
|
* @return string |
|
89
|
|
|
*/ |
|
90
|
|
|
protected static function escape($value) |
|
91
|
|
|
{ |
|
92
|
|
|
return htmlspecialchars($value, ENT_QUOTES, 'UTF-8'); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Filters attribute values to trim by length. |
|
97
|
|
|
* |
|
98
|
|
|
* @param string $value |
|
|
|
|
|
|
99
|
|
|
* |
|
100
|
|
|
* @return string |
|
101
|
|
|
*/ |
|
102
|
|
|
protected static function trim($name, $content) |
|
103
|
|
|
{ |
|
104
|
|
|
$limit = isset(self::$characterLimits[$name]) ? self::$characterLimits[$name] : null; |
|
105
|
|
|
|
|
106
|
|
|
if ($limit && strlen($content) > $limit) { |
|
107
|
|
|
$content = substr($content, 0, $limit - 3).'...'; |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
return $content; |
|
111
|
|
|
} |
|
112
|
|
|
} |
|
113
|
|
|
|
This check looks for PHPDoc comments describing methods or function parameters that do not exist on the corresponding method or function.
Consider the following example. The parameter
$italyis not defined by the methodfinale(...).The most likely cause is that the parameter was removed, but the annotation was not.