1
|
|
|
<?php namespace Arcanedev\SeoHelper\Bases; |
2
|
|
|
|
3
|
|
|
use Arcanedev\SeoHelper\Contracts\Entities\MetaCollection as MetaCollectionContract; |
4
|
|
|
use Arcanedev\SeoHelper\Contracts\Helpers\Meta as MetaContract; |
5
|
|
|
use Arcanedev\SeoHelper\Contracts\Renderable; |
6
|
|
|
use Arcanedev\SeoHelper\Helpers\Meta; |
7
|
|
|
use Illuminate\Support\Collection; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class MetaCollection |
11
|
|
|
* |
12
|
|
|
* @package Arcanedev\SeoHelper\Bases |
13
|
|
|
* @author ARCANEDEV <[email protected]> |
14
|
|
|
*/ |
15
|
|
|
abstract class MetaCollection extends Collection implements MetaCollectionContract |
16
|
|
|
{ |
17
|
|
|
/* ----------------------------------------------------------------- |
18
|
|
|
| Properties |
19
|
|
|
| ----------------------------------------------------------------- |
20
|
|
|
*/ |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* Meta tag prefix. |
24
|
|
|
* |
25
|
|
|
* @var string |
26
|
|
|
*/ |
27
|
|
|
protected $prefix = ''; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* Meta tag name property. |
31
|
|
|
* |
32
|
|
|
* @var string |
33
|
|
|
*/ |
34
|
|
|
protected $nameProperty = 'name'; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* The items contained in the collection. |
38
|
|
|
* |
39
|
|
|
* @var array |
40
|
|
|
*/ |
41
|
|
|
protected $items = []; |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* Ignored tags, they have dedicated class. |
45
|
|
|
* |
46
|
|
|
* @var array |
47
|
|
|
*/ |
48
|
|
|
protected $ignored = []; |
49
|
|
|
|
50
|
|
|
/* ----------------------------------------------------------------- |
51
|
|
|
| Getters & Setters |
52
|
|
|
| ----------------------------------------------------------------- |
53
|
|
|
*/ |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Set meta prefix name. |
57
|
|
|
* |
58
|
|
|
* @param string $prefix |
59
|
|
|
* |
60
|
|
|
* @return \Arcanedev\SeoHelper\Bases\MetaCollection |
61
|
|
|
*/ |
62
|
210 |
|
public function setPrefix($prefix) |
63
|
|
|
{ |
64
|
210 |
|
$this->prefix = $prefix; |
65
|
|
|
|
66
|
210 |
|
return $this->refresh(); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/* ----------------------------------------------------------------- |
70
|
|
|
| Main Functions |
71
|
|
|
| ----------------------------------------------------------------- |
72
|
|
|
*/ |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Add many meta tags. |
76
|
|
|
* |
77
|
|
|
* @param array $metas |
78
|
|
|
* |
79
|
|
|
* @return \Arcanedev\SeoHelper\Bases\MetaCollection |
80
|
|
|
*/ |
81
|
261 |
|
public function addMany(array $metas) |
82
|
|
|
{ |
83
|
261 |
|
foreach ($metas as $name => $content) { |
84
|
126 |
|
$this->add($name, $content); |
85
|
|
|
} |
86
|
|
|
|
87
|
261 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* Add a meta to collection. |
92
|
|
|
* |
93
|
|
|
* @param string $name |
94
|
|
|
* @param string|array $content |
95
|
|
|
* |
96
|
|
|
* @return \Arcanedev\SeoHelper\Bases\MetaCollection |
97
|
|
|
*/ |
98
|
210 |
|
public function add($name, $content) |
99
|
|
|
{ |
100
|
210 |
|
if (empty($name) || empty($content)) return $this; |
101
|
|
|
|
102
|
210 |
|
return $this->addMeta($name, $content); |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
/** |
106
|
|
|
* Make a meta and add it to collection. |
107
|
|
|
* |
108
|
|
|
* @param string $name |
109
|
|
|
* @param string|array $content |
110
|
|
|
* |
111
|
|
|
* @return \Arcanedev\SeoHelper\Bases\MetaCollection |
112
|
|
|
*/ |
113
|
210 |
|
protected function addMeta($name, $content) |
114
|
|
|
{ |
115
|
210 |
|
$meta = Meta::make($name, $content, $this->nameProperty, $this->prefix); |
116
|
|
|
|
117
|
210 |
|
$this->put($meta->key(), $meta); |
118
|
|
|
|
119
|
210 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Remove a meta from the collection by key. |
124
|
|
|
* |
125
|
|
|
* @param array|string $names |
126
|
|
|
* |
127
|
|
|
* @return \Arcanedev\SeoHelper\Bases\MetaCollection |
128
|
|
|
*/ |
129
|
9 |
|
public function remove($names) |
130
|
|
|
{ |
131
|
9 |
|
$names = $this->prepareName($names); |
132
|
|
|
|
133
|
9 |
|
return $this->forget($names); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Render the tag. |
138
|
|
|
* |
139
|
|
|
* @return string |
140
|
|
|
*/ |
141
|
|
|
public function render() |
142
|
|
|
{ |
143
|
234 |
|
$output = $this->map(function (Renderable $meta) { |
144
|
231 |
|
return $meta->render(); |
145
|
234 |
|
})->toArray(); |
146
|
|
|
|
147
|
234 |
|
return implode(PHP_EOL, array_filter($output)); |
148
|
|
|
} |
149
|
|
|
|
150
|
|
|
/** |
151
|
|
|
* Reset the collection. |
152
|
|
|
* |
153
|
|
|
* @return \Arcanedev\SeoHelper\Bases\MetaCollection |
154
|
|
|
*/ |
155
|
12 |
|
public function reset() |
156
|
|
|
{ |
157
|
12 |
|
$this->items = []; |
158
|
|
|
|
159
|
12 |
|
return $this; |
160
|
|
|
} |
161
|
|
|
|
162
|
|
|
/** |
163
|
|
|
* Render the tag. |
164
|
|
|
* |
165
|
|
|
* @return string |
166
|
|
|
*/ |
167
|
3 |
|
public function __toString() |
168
|
|
|
{ |
169
|
3 |
|
return $this->render(); |
170
|
|
|
} |
171
|
|
|
|
172
|
|
|
/* ----------------------------------------------------------------- |
173
|
|
|
| Check Functions |
174
|
|
|
| ----------------------------------------------------------------- |
175
|
|
|
*/ |
176
|
|
|
|
177
|
|
|
/** |
178
|
|
|
* Check if meta is ignored. |
179
|
|
|
* |
180
|
|
|
* @param string $name |
181
|
|
|
* |
182
|
|
|
* @return bool |
183
|
|
|
*/ |
184
|
138 |
|
protected function isIgnored($name) |
185
|
|
|
{ |
186
|
138 |
|
return in_array($name, $this->ignored); |
187
|
|
|
} |
188
|
|
|
|
189
|
|
|
/* ----------------------------------------------------------------- |
190
|
|
|
| Other Functions |
191
|
|
|
| ----------------------------------------------------------------- |
192
|
|
|
*/ |
193
|
|
|
|
194
|
|
|
/** |
195
|
|
|
* Remove an item from the collection by key. |
196
|
|
|
* |
197
|
|
|
* @param string|array $keys |
198
|
|
|
* |
199
|
|
|
* @return \Arcanedev\SeoHelper\Bases\MetaCollection |
200
|
|
|
*/ |
201
|
9 |
|
public function forget($keys) |
202
|
|
|
{ |
203
|
9 |
|
foreach ((array) $keys as $key) { |
204
|
9 |
|
$this->offsetUnset($key); |
205
|
|
|
} |
206
|
|
|
|
207
|
9 |
|
return $this; |
208
|
|
|
} |
209
|
|
|
|
210
|
|
|
/** |
211
|
|
|
* Refresh meta collection items. |
212
|
|
|
* |
213
|
|
|
* @return \Arcanedev\SeoHelper\Bases\MetaCollection |
214
|
|
|
*/ |
215
|
|
|
private function refresh() |
216
|
|
|
{ |
217
|
210 |
|
return $this->map(function (MetaContract $meta) { |
218
|
3 |
|
return $meta->setPrefix($this->prefix); |
219
|
210 |
|
}); |
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
/** |
223
|
|
|
* Prepare names. |
224
|
|
|
* |
225
|
|
|
* @param array|string $names |
226
|
|
|
* |
227
|
|
|
* @return array |
228
|
|
|
*/ |
229
|
|
|
protected function prepareName($names) |
230
|
|
|
{ |
231
|
9 |
|
return array_map(function ($name) { |
232
|
9 |
|
return strtolower(trim($name)); |
233
|
9 |
|
}, (array) $names); |
234
|
|
|
} |
235
|
|
|
} |
236
|
|
|
|