|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Hyde\Framework\Helpers; |
|
4
|
|
|
|
|
5
|
|
|
use Hyde\Framework\Models\Metadata\LinkItem; |
|
6
|
|
|
use Hyde\Framework\Models\Metadata\MetadataItem; |
|
7
|
|
|
use Hyde\Framework\Models\Metadata\OpenGraphItem; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* Helpers to fluently declare HTML meta tags. |
|
11
|
|
|
* |
|
12
|
|
|
* @see \Hyde\Framework\Testing\Feature\MetadataHelperTest |
|
13
|
|
|
*/ |
|
14
|
|
|
class Meta |
|
15
|
|
|
{ |
|
16
|
|
|
public static function name(string $name, string $content): MetadataItem |
|
17
|
|
|
{ |
|
18
|
|
|
return new MetadataItem($name, $content); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
public static function property(string $property, string $content): OpenGraphItem |
|
22
|
|
|
{ |
|
23
|
|
|
return new OpenGraphItem($property, $content); |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
public static function link(string $rel, string $href, array $attr = []): LinkItem |
|
27
|
|
|
{ |
|
28
|
|
|
return new LinkItem($rel, $href, $attr); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
public static function get(array $withMergedData = []): array |
|
32
|
|
|
{ |
|
33
|
|
|
return static::filterUnique( |
|
34
|
|
|
array_merge( |
|
35
|
|
|
static::getGlobalMeta(), |
|
36
|
|
|
$withMergedData |
|
37
|
|
|
) |
|
38
|
|
|
); |
|
39
|
|
|
} |
|
40
|
|
|
|
|
41
|
|
|
public static function render(array $withMergedData = []): string |
|
42
|
|
|
{ |
|
43
|
|
|
return implode( |
|
44
|
|
|
"\n", |
|
45
|
|
|
static::get($withMergedData) |
|
46
|
|
|
); |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
protected static function filterUnique(array $meta): array |
|
50
|
|
|
{ |
|
51
|
|
|
$array = []; |
|
52
|
|
|
$existing = []; |
|
53
|
|
|
|
|
54
|
|
|
foreach (array_reverse($meta) as $metaItem) { |
|
55
|
|
|
$substring = substr($metaItem, 6, strpos($metaItem, ' content="') - 6); |
|
56
|
|
|
|
|
57
|
|
|
if (! in_array($substring, $existing)) { |
|
58
|
|
|
$array[] = $metaItem; |
|
59
|
|
|
$existing[] = $substring; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return array_reverse($array); |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
public static function getGlobalMeta(): array |
|
67
|
|
|
{ |
|
68
|
|
|
return array_merge( |
|
69
|
|
|
static::getDynamicMeta(), |
|
70
|
|
|
static::getConfiguredMeta() |
|
71
|
|
|
); |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
protected static function getDynamicMeta(): array |
|
75
|
|
|
{ |
|
76
|
|
|
$array = []; |
|
77
|
|
|
|
|
78
|
|
|
return $array; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
protected static function getConfiguredMeta(): array |
|
82
|
|
|
{ |
|
83
|
|
|
return config('hyde.meta', []); |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|