Passed
Branch v2 (aaaa8e)
by Brent
03:38
created

Meta::property()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.4285
c 0
b 0
f 0
cc 2
eloc 6
nc 2
nop 2
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
    final public function __construct(string $charset = 'UTF-8') {
23
        $this->charset($charset);
24
        $this->name('viewport', 'width=device-width, initial-scale=1');
25
26
        $this->socialMeta = [
27
            new GooglePlusMeta($this),
28
            new TwitterMeta($this),
29
            new OpenGraphMeta($this),
30
        ];
31
    }
32
33
    public static function create(string $charset = 'UTF-8') : Meta {
34
        return new self($charset);
35
    }
36
37
    public function render() : string {
38
        $html = '';
39
40
        /**
41
         * @var string     $type
42
         * @var MetaItem[] $metaItems
43
         */
44
        foreach ($this->meta as $type => $metaItems) {
45
            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
                $html .= $metaItem->render() . "\n";
47
            }
48
        }
49
50
        return $html;
51
    }
52
53
    public function charset(string $charset) : Meta {
54
        $item = CharsetMeta::create($charset);
55
        $this->meta['charset'][] = $item;
56
57
        return $this;
58
    }
59
60
    public function name(string $name, ?string $content) : Meta {
61
        if (!$content) {
62
            return $this;
63
        }
64
65
        $item = NameMeta::create($name, $content);
66
        $this->meta['name'][$name] = $item;
67
68
        return $this;
69
    }
70
71
    public function itemprop(string $name, ?string $content) : Meta {
72
        if (!$content) {
73
            return $this;
74
        }
75
76
        $item = ItemPropMeta::create($name, $content);
77
        $this->meta['itemprop'][$name] = $item;
78
79
        return $this;
80
    }
81
82
    public function property(string $property, ?string $content) : Meta {
83
        if (!$content) {
84
            return $this;
85
        }
86
87
        $item = PropertyMeta::create($property, $content);
88
        $this->meta['property'][$property] = $item;
89
90
        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
    public function link(string $rel, ?string $href) : Meta {
105
        if (!$href) {
106
            return $this;
107
        }
108
109
        $item = LinkMeta::create($rel, $href);
110
        $this->meta['link'][$rel] = $item;
111
112
        return $this;
113
    }
114
115
    public function title(?string $content) : Meta {
116
        if (!$content) {
117
            return $this;
118
        }
119
120
        $this->name('title', $content);
121
122
        foreach ($this->socialMeta as $socialMeta) {
123
            $socialMeta->title($content);
124
        }
125
126
        return $this;
127
    }
128
129
    public function description(?string $content) : Meta {
130
        if (!$content) {
131
            return $this;
132
        }
133
134
        $this->name('description', $content);
135
136
        foreach ($this->socialMeta as $socialMeta) {
137
            $socialMeta->description($content);
138
        }
139
140
        return $this;
141
    }
142
143
    public function image(?string $content) : Meta {
144
        if (!$content) {
145
            return $this;
146
        }
147
148
        $this->name('image', $content);
149
150
        foreach ($this->socialMeta as $socialMeta) {
151
            $socialMeta->image($content);
152
        }
153
154
        return $this;
155
    }
156
}
157