Completed
Push — master ( b3bd44...a008aa )
by Nikita
01:51
created

Meta::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace LaraComponents\Seo;
4
5
class Meta
6
{
7
    protected $attributes = [];
8
9
    public function __construct(array $attributes = [])
10
    {
11
        $this->setAttributes($attributes);
12
    }
13
14
    public static function make(array $attributes = [])
15
    {
16
        return new self($attributes);
17
    }
18
19
    public function setAttributes(array $attributes)
20
    {
21
        foreach ($attributes as $key => $value) {
22
            $this->setAttribute($key, $value);
23
        }
24
    }
25
26
    public function getAttributes()
27
    {
28
        return $this->attributes;
29
    }
30
31
    public function setAttribute($key, $value)
32
    {
33
        $this->attributes[$key] = $value;
34
    }
35
36
    public function getAttribute($key)
37
    {
38
        if (array_key_exists($key, $this->attributes)) {
39
            return $this->attributes[$key];
40
        }
41
    }
42
43
    public function toString()
44
    {
45
        if (count($this->attributes)) {
46
            return '<meta'.$this->getHtmlAttributes().'/>';
47
        }
48
49
        return '';
50
    }
51
52
    protected function getHtmlAttributes()
53
    {
54
        $html = [];
55
        foreach ($this->attributes as $key => $value) {
56
            $attribute = $key.'="'.e($value).'"';
57
58
            if (! is_null($attribute)) {
59
                $html[] = $attribute;
60
            }
61
        }
62
63
        return count($html) > 0 ? ' '.implode(' ', $html) : '';
64
    }
65
66
    public function __toString()
67
    {
68
        return $this->toString();
69
    }
70
}
71