Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
100.00% |
17 / 17 |
|
100.00% |
6 / 6 |
CRAP | |
100.00% |
1 / 1 |
Meta | |
100.00% |
17 / 17 |
|
100.00% |
6 / 6 |
9 | |
100.00% |
1 / 1 |
name | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
property | |
100.00% |
2 / 2 |
|
100.00% |
1 / 1 |
1 | |||
render | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
1 | |||
filterUnique | |
100.00% |
8 / 8 |
|
100.00% |
1 / 1 |
3 | |||
getGlobalMeta | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
formatOpenGraphProperty | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
2 |
1 | <?php |
2 | |
3 | namespace Hyde\Framework\Helpers; |
4 | |
5 | /** |
6 | * Helpers to fluently declare HTML meta tags. |
7 | * |
8 | * @see \Tests\Feature\MetadataHelperTest |
9 | */ |
10 | class Meta |
11 | { |
12 | public static function name(string $name, string $content): string |
13 | { |
14 | return '<meta name="'.e($name).'" content="'.e($content).'">'; |
15 | } |
16 | |
17 | public static function property(string $property, string $content): string |
18 | { |
19 | $property = static::formatOpenGraphProperty($property); |
20 | |
21 | return '<meta property="'.e($property).'" content="'.e($content).'">'; |
22 | } |
23 | |
24 | public static function render(array $overridesGlobalMeta = []): string |
25 | { |
26 | return implode("\n", |
27 | static::filterUnique( |
28 | array_merge( |
29 | static::getGlobalMeta(), |
30 | $overridesGlobalMeta |
31 | ) |
32 | ) |
33 | ); |
34 | } |
35 | |
36 | protected static function filterUnique(array $meta): array |
37 | { |
38 | $array = []; |
39 | $existing = []; |
40 | |
41 | foreach (array_reverse($meta) as $metaItem) { |
42 | $substring = substr($metaItem, 6, strpos($metaItem, ' content="') - 6); |
43 | |
44 | if (! in_array($substring, $existing)) { |
45 | $array[] = $metaItem; |
46 | $existing[] = $substring; |
47 | } |
48 | } |
49 | |
50 | return array_reverse($array); |
51 | } |
52 | |
53 | public static function getGlobalMeta(): array |
54 | { |
55 | return config('hyde.meta', []); |
56 | } |
57 | |
58 | protected static function formatOpenGraphProperty(string $property): string |
59 | { |
60 | return str_starts_with($property, 'og:') ? $property : 'og:'.$property; |
61 | } |
62 | } |