Passed
Push — develop ( ec1e42...80559e )
by Brent
03:22
created

Meta::name()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 2.0185

Importance

Changes 0
Metric Value
cc 2
eloc 6
nc 2
nop 2
dl 0
loc 10
ccs 5
cts 6
cp 0.8333
crap 2.0185
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
namespace Pageon\Html\Meta;
4
5
use Pageon\Html\Meta\Item\CharsetMeta;
6
use Pageon\Html\Meta\Item\HttpEquivMeta;
7
use Pageon\Html\Meta\Item\ItemPropMeta;
8
use Pageon\Html\Meta\Item\LinkMeta;
9
use Pageon\Html\Meta\Item\NameMeta;
10
use Pageon\Html\Meta\Item\PropertyMeta;
11
use Pageon\Html\Meta\Social\GooglePlusMeta;
12
use Pageon\Html\Meta\Social\OpenGraphMeta;
13
use Pageon\Html\Meta\Social\TwitterMeta;
14
15
class Meta
16
{
17
    /** @var MetaItem[] */
18
    private $meta = [];
19
    /** @var SocialMeta[] */
20
    private $socialMeta = [];
21
22 34
    final public function __construct(string $charset = 'UTF-8') {
23 34
        $this->charset($charset);
24 34
        $this->name('viewport', 'width=device-width, initial-scale=1');
25
26 34
        $this->socialMeta = [
27 34
            new GooglePlusMeta($this),
28 34
            new TwitterMeta($this),
29 34
            new OpenGraphMeta($this),
30
        ];
31 34
    }
32
33 25
    public static function create(string $charset = 'UTF-8') : Meta {
34 25
        return new self($charset);
35
    }
36
37 28
    public function render() : string {
38 28
        $html = '';
39
40
        /**
41
         * @var string     $type
42
         * @var MetaItem[] $metaItems
43
         */
44 28
        foreach ($this->meta as $type => $metaItems) {
45 28
            foreach ($metaItems as $metaItem) {
0 ignored issues
show
Bug introduced by
The expression $metaItems of type object<Pageon\Html\Meta\MetaItem> is not traversable.
Loading history...
46 28
                $html .= $metaItem->render() . "\n";
47
            }
48
        }
49
50 28
        return $html;
51
    }
52
53 34
    public function charset(string $charset) : Meta {
54 34
        $item = CharsetMeta::create($charset);
55 34
        $this->meta['charset'][] = $item;
56
57 34
        return $this;
58
    }
59
60 34
    public function name(string $name, ?string $content) : Meta {
61 34
        if (!$content) {
62
            return $this;
63
        }
64
65 34
        $item = NameMeta::create($name, $content);
66 34
        $this->meta['name'][$name] = $item;
67
68 34
        return $this;
69
    }
70
71 17
    public function itemprop(string $name, ?string $content) : Meta {
72 17
        if (!$content) {
73
            return $this;
74
        }
75
76 17
        $item = ItemPropMeta::create($name, $content);
77 17
        $this->meta['itemprop'][$name] = $item;
78
79 17
        return $this;
80
    }
81
82 34
    public function property(string $property, ?string $content) : Meta {
83 34
        if (!$content) {
84
            return $this;
85
        }
86
87 34
        $item = PropertyMeta::create($property, $content);
88 34
        $this->meta['property'][$property] = $item;
89
90 34
        return $this;
91
    }
92
93
    public function httpEquiv(string $httpEquiv, ?string $content) : Meta {
94
        if (!$content) {
95
            return $this;
96
        }
97
98
        $item = HttpEquivMeta::create($httpEquiv, $content);
99
        $this->meta['httpEquiv'][$httpEquiv] = $item;
100
101
        return $this;
102
    }
103
104 21
    public function link(string $rel, ?string $href) : Meta {
105 21
        if (!$href) {
106 21
            return $this;
107
        }
108
109 6
        $item = LinkMeta::create($rel, $href);
110 6
        $this->meta['link'][$rel] = $item;
111
112 6
        return $this;
113
    }
114
115 22
    public function title(?string $content) : Meta {
116 22
        if (!$content) {
117 14
            return $this;
118
        }
119
120 12
        $this->name('title', $content);
121
122 12
        foreach ($this->socialMeta as $socialMeta) {
123 12
            $socialMeta->title($content);
124
        }
125
126 12
        return $this;
127
    }
128
129 22
    public function description(?string $content) : Meta {
130 22
        if (!$content) {
131 16
            return $this;
132
        }
133
134 10
        $this->name('description', $content);
135
136 10
        foreach ($this->socialMeta as $socialMeta) {
137 10
            $socialMeta->description($content);
138
        }
139
140 10
        return $this;
141
    }
142
143 1
    public function image(?string $content) : Meta {
144 1
        if (!$content) {
145
            return $this;
146
        }
147
148 1
        $this->name('image', $content);
149
150 1
        foreach ($this->socialMeta as $socialMeta) {
151 1
            $socialMeta->image($content);
152
        }
153
154 1
        return $this;
155
    }
156
}
157