|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Hyde\Framework\Hyde; |
|
6
|
|
|
use Hyde\Framework\Services\RssFeedService; |
|
7
|
|
|
|
|
8
|
|
|
/** |
|
9
|
|
|
* Helpers to fluently declare HTML meta tags. |
|
10
|
|
|
* |
|
11
|
|
|
* @see \Hyde\Framework\Testing\Feature\MetadataHelperTest |
|
12
|
|
|
*/ |
|
13
|
|
|
class Meta |
|
14
|
|
|
{ |
|
15
|
|
|
public static function name(string $name, string $content): string |
|
16
|
|
|
{ |
|
17
|
|
|
return '<meta name="'.e($name).'" content="'.e($content).'">'; |
|
18
|
|
|
} |
|
19
|
|
|
|
|
20
|
|
|
public static function property(string $property, string $content): string |
|
21
|
|
|
{ |
|
22
|
|
|
$property = static::formatOpenGraphProperty($property); |
|
23
|
|
|
|
|
24
|
|
|
return '<meta property="'.e($property).'" content="'.e($content).'">'; |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
public static function link(string $rel, string $href, array $attr = []): string |
|
28
|
|
|
{ |
|
29
|
|
|
if (! $attr) { |
|
|
|
|
|
|
30
|
|
|
return '<link rel="'.e($rel).'" href="'.e($href).'">'; |
|
31
|
|
|
} |
|
32
|
|
|
|
|
33
|
|
|
$attributes = collect($attr)->map(function ($value, $key) { |
|
|
|
|
|
|
34
|
|
|
return e($key).'="'.e($value).'"'; |
|
35
|
|
|
})->implode(' '); |
|
36
|
|
|
|
|
37
|
|
|
return '<link rel="'.e($rel).'" href="'.e($href).'" '.$attributes.'>'; |
|
38
|
|
|
} |
|
39
|
|
|
|
|
40
|
|
|
public static function render(array $withMergedData = []): string |
|
41
|
|
|
{ |
|
42
|
|
|
return implode( |
|
43
|
|
|
"\n", |
|
44
|
|
|
static::filterUnique( |
|
45
|
|
|
array_merge( |
|
46
|
|
|
static::getGlobalMeta(), |
|
47
|
|
|
$withMergedData |
|
48
|
|
|
) |
|
49
|
|
|
) |
|
50
|
|
|
); |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
protected static function filterUnique(array $meta): array |
|
54
|
|
|
{ |
|
55
|
|
|
$array = []; |
|
56
|
|
|
$existing = []; |
|
57
|
|
|
|
|
58
|
|
|
foreach (array_reverse($meta) as $metaItem) { |
|
59
|
|
|
$substring = substr($metaItem, 6, strpos($metaItem, ' content="') - 6); |
|
60
|
|
|
|
|
61
|
|
|
if (! in_array($substring, $existing)) { |
|
62
|
|
|
$array[] = $metaItem; |
|
63
|
|
|
$existing[] = $substring; |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
return array_reverse($array); |
|
68
|
|
|
} |
|
69
|
|
|
|
|
70
|
|
|
public static function getGlobalMeta(): array |
|
71
|
|
|
{ |
|
72
|
|
|
return array_merge( |
|
73
|
|
|
static::getDynamicMeta(), |
|
74
|
|
|
static::getConfiguredMeta() |
|
75
|
|
|
); |
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
protected static function getDynamicMeta(): array |
|
79
|
|
|
{ |
|
80
|
|
|
$array = []; |
|
81
|
|
|
|
|
82
|
|
|
if (Features::sitemap()) { |
|
83
|
|
|
$array[] = Meta::link('sitemap', Hyde::url('sitemap.xml'), [ |
|
84
|
|
|
'type' => 'application/xml', 'title' => 'Sitemap', |
|
85
|
|
|
]); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
if (Features::rss()) { |
|
89
|
|
|
$array[] = Meta::link('alternate', Hyde::url(RssFeedService::getDefaultOutputFilename()), [ |
|
90
|
|
|
'type' => 'application/rss+xml', 'title' => RssFeedService::getDescription(), |
|
91
|
|
|
]); |
|
92
|
|
|
} |
|
93
|
|
|
|
|
94
|
|
|
return $array; |
|
95
|
|
|
} |
|
96
|
|
|
|
|
97
|
|
|
protected static function getConfiguredMeta(): array |
|
98
|
|
|
{ |
|
99
|
|
|
return config('hyde.meta', []); |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
protected static function formatOpenGraphProperty(string $property): string |
|
103
|
|
|
{ |
|
104
|
|
|
return str_starts_with($property, 'og:') ? $property : 'og:'.$property; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.
Consider making the comparison explicit by using
empty(..)or! empty(...)instead.