Passed
Push — master ( 8ab35d...c4a5b0 )
by Caen
02:53 queued 11s
created

Meta::render()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 6
nc 1
nop 1
dl 0
loc 8
rs 10
c 0
b 0
f 0
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) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $attr of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

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.

Loading history...
30
            return '<link rel="'.e($rel).'" href="'.e($href).'">';
31
        }
32
33
        $attributes = collect($attr)->map(function ($value, $key) {
0 ignored issues
show
Bug introduced by
$attr of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $value of collect(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

33
        $attributes = collect(/** @scrutinizer ignore-type */ $attr)->map(function ($value, $key) {
Loading history...
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