Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
6 / 6
CRAP
100.00% covered (success)
100.00%
1 / 1
MetadataBag
100.00% covered (success)
100.00%
19 / 19
100.00% covered (success)
100.00%
6 / 6
7
100.00% covered (success)
100.00%
1 / 1
 toHtml
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 render
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get
100.00% covered (success)
100.00%
6 / 6
100.00% covered (success)
100.00%
1 / 1
1
 add
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 addGenericElement
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getPrefixedArray
100.00% covered (success)
100.00%
5 / 5
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\Framework\Features\Metadata\Elements\LinkElement;
8use Hyde\Framework\Features\Metadata\Elements\MetadataElement;
9use Hyde\Framework\Features\Metadata\Elements\OpenGraphElement;
10use Illuminate\Contracts\Support\Htmlable;
11
12use function array_merge;
13use function implode;
14
15/**
16 * Holds the metadata tags for a page or the site model.
17 *
18 * @see \Hyde\Framework\Features\Metadata\PageMetadataBag
19 * @see \Hyde\Framework\Features\Metadata\GlobalMetadataBag
20 */
21class MetadataBag implements Htmlable
22{
23    /** @var array<string, MetadataElementContract> */
24    protected array $links = [];
25
26    /** @var array<string, MetadataElementContract> */
27    protected array $metadata = [];
28
29    /** @var array<string, MetadataElementContract> */
30    protected array $properties = [];
31
32    /** @var array<string> */
33    protected array $generics = [];
34
35    public function toHtml(): string
36    {
37        return $this->render();
38    }
39
40    public function render(): string
41    {
42        return implode("\n", $this->get());
43    }
44
45    public function get(): array
46    {
47        return array_merge(
48            $this->getPrefixedArray('links'),
49            $this->getPrefixedArray('metadata'),
50            $this->getPrefixedArray('properties'),
51            $this->getPrefixedArray('generics')
52        );
53    }
54
55    public function add(MetadataElementContract|string $element): static
56    {
57        match (true) {
58            $element instanceof LinkElement => $this->links[$element->uniqueKey()] = $element,
59            $element instanceof MetadataElement => $this->metadata[$element->uniqueKey()] = $element,
60            $element instanceof OpenGraphElement => $this->properties[$element->uniqueKey()] = $element,
61            default => $this->addGenericElement((string) $element),
62        };
63
64        return $this;
65    }
66
67    protected function addGenericElement(string $element): void
68    {
69        $this->generics[] = $element;
70    }
71
72    /** @return array<string, MetadataElementContract> */
73    protected function getPrefixedArray(string $type): array
74    {
75        /** @var array<string, MetadataElementContract> $bag */
76        $bag = $this->{$type};
77
78        $array = [];
79
80        foreach ($bag as $key => $element) {
81            $array["$type:$key"] = $element;
82        }
83
84        return $array;
85    }
86}