LinkElement::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 3
dl 0
loc 5
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 3
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Framework\Features\Metadata\Elements;
6
7
use Hyde\Framework\Features\Metadata\MetadataElementContract;
8
9
use function collect;
10
use function sprintf;
11
use function e;
12
13
class 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 {
0 ignored issues
show
Bug introduced by
$this->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

42
        return sprintf(' %s', collect(/** @scrutinizer ignore-type */ $this->attr)->map(function (string $value, string $key): string {
Loading history...
43
            return e($key).'="'.e($value).'"';
44
        })->implode(' '));
45
    }
46
}
47