Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
GlobalMetadataBag
100.00% covered (success)
100.00%
18 / 18
100.00% covered (success)
100.00%
2 / 2
7
100.00% covered (success)
100.00%
1 / 1
 make
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
5
 filterDuplicateMetadata
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2
3declare(strict_types=1);
4
5namespace Hyde\Framework\Features\Metadata;
6
7use Hyde\Hyde;
8use Hyde\Facades\Meta;
9use Hyde\Facades\Config;
10use Hyde\Facades\Features;
11use Hyde\Pages\Concerns\HydePage;
12use Hyde\Support\Facades\Render;
13use Hyde\Framework\Features\XmlGenerators\RssFeedGenerator;
14use Hyde\Framework\Features\Metadata\MetadataElementContract as Element;
15
16use function array_filter;
17use function array_map;
18use function in_array;
19
20class GlobalMetadataBag extends MetadataBag
21{
22    public static function make(): static
23    {
24        $metadata = new static();
25
26        /** @var MetadataElementContract $item */
27        foreach (Config::getArray('hyde.meta', []) as $item) {
28            $metadata->add($item);
29        }
30
31        if (Features::sitemap()) {
32            $metadata->add(Meta::link('sitemap', Hyde::url('sitemap.xml'), [
33                'type' => 'application/xml', 'title' => 'Sitemap',
34            ]));
35        }
36
37        if (Features::rss()) {
38            $metadata->add(Meta::link('alternate', Hyde::url(RssFeedGenerator::getFilename()), [
39                'type' => 'application/rss+xml', 'title' => RssFeedGenerator::getDescription(),
40            ]));
41        }
42
43        if (Render::getPage() !== null) {
44            static::filterDuplicateMetadata($metadata, Render::getPage());
45        }
46
47        return $metadata;
48    }
49
50    protected static function filterDuplicateMetadata(GlobalMetadataBag $global, HydePage $page): void
51    {
52        // Reject any metadata from the global metadata bag that is already present in the page metadata bag.
53
54        foreach (['links', 'metadata', 'properties', 'generics'] as $type) {
55            $global->$type = array_filter((array) $global->$type, fn (Element $element): bool => ! in_array($element->uniqueKey(),
56                array_map(fn (Element $element): string => $element->uniqueKey(), (array) $page->metadata->$type)
57            ));
58        }
59    }
60}