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