Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
4 / 4
CRAP
100.00% covered (success)
100.00%
1 / 1
LinkElement
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
4 / 4
5
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 __toString
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 uniqueKey
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 formatOptionalAttributes
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\Elements;
6
7use Hyde\Framework\Features\Metadata\MetadataElementContract;
8
9use function collect;
10use function sprintf;
11use function e;
12
13class LinkElement implements MetadataElementContract
14{
15    protected string $rel;
16    protected string $href;
17    protected array $attr = [];
18
19    public function __construct(string $rel, string $href, array $attr = [])
20    {
21        $this->rel = $rel;
22        $this->href = $href;
23        $this->attr = $attr;
24    }
25
26    public function __toString(): string
27    {
28        return sprintf('<link rel="%s" href="%s"%s>', e($this->rel), e($this->href), $this->formatOptionalAttributes());
29    }
30
31    public function uniqueKey(): string
32    {
33        return $this->rel;
34    }
35
36    protected function formatOptionalAttributes(): string
37    {
38        if (empty($this->attr)) {
39            return '';
40        }
41
42        return sprintf(' %s', collect($this->attr)->map(function (string $value, string $key): string {
43            return e($key).'="'.e($value).'"';
44        })->implode(' '));
45    }
46}