Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
CRAP | |
100.00% |
1 / 1 |
| GlobalMetadataBag | |
100.00% |
18 / 18 |
|
100.00% |
2 / 2 |
7 | |
100.00% |
1 / 1 |
| make | |
100.00% |
14 / 14 |
|
100.00% |
1 / 1 |
5 | |||
| filterDuplicateMetadata | |
100.00% |
4 / 4 |
|
100.00% |
1 / 1 |
2 | |||
| 1 | <?php |
| 2 | |
| 3 | declare(strict_types=1); |
| 4 | |
| 5 | namespace Hyde\Framework\Features\Metadata; |
| 6 | |
| 7 | use Hyde\Hyde; |
| 8 | use Hyde\Facades\Meta; |
| 9 | use Hyde\Facades\Config; |
| 10 | use Hyde\Facades\Features; |
| 11 | use Hyde\Pages\Concerns\HydePage; |
| 12 | use Hyde\Support\Facades\Render; |
| 13 | use Hyde\Framework\Features\XmlGenerators\RssFeedGenerator; |
| 14 | use Hyde\Framework\Features\Metadata\MetadataElementContract as Element; |
| 15 | |
| 16 | use function array_filter; |
| 17 | use function array_map; |
| 18 | use function in_array; |
| 19 | |
| 20 | class 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 | } |