1
|
|
|
<?php namespace Arcanedev\EmbedVideo\Parsers; |
2
|
|
|
|
3
|
|
|
use Arcanedev\EmbedVideo\Contracts\Parser; |
4
|
|
|
use Arcanedev\EmbedVideo\Entities\Iframe; |
5
|
|
|
use Illuminate\Support\Arr; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Class AbstractParser |
9
|
|
|
* |
10
|
|
|
* @package Arcanedev\EmbedVideo\Parsers |
11
|
|
|
* @author ARCANEDEV <[email protected]> |
12
|
|
|
*/ |
13
|
|
|
abstract class AbstractParser implements Parser |
14
|
|
|
{ |
15
|
|
|
/* ----------------------------------------------------------------- |
16
|
|
|
| Properties |
17
|
|
|
| ----------------------------------------------------------------- |
18
|
|
|
*/ |
19
|
|
|
/** |
20
|
|
|
* The given URL to be parsed. |
21
|
|
|
* |
22
|
|
|
* @var string|null |
23
|
|
|
*/ |
24
|
|
|
protected $url; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* The parser's URL patterns. |
28
|
|
|
* |
29
|
|
|
* @var array |
30
|
|
|
*/ |
31
|
|
|
protected $patterns = []; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* The iframe attribute. |
35
|
|
|
* |
36
|
|
|
* @var array |
37
|
|
|
*/ |
38
|
|
|
protected $attributes = []; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var array |
42
|
|
|
*/ |
43
|
|
|
protected $cachedMatches; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Use a secure HTTP Protocol. |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
protected $useSecure = true; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* The HTTP Protocol. |
54
|
|
|
* |
55
|
|
|
* @var string |
56
|
|
|
*/ |
57
|
|
|
protected $protocol = 'https'; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Timestamp pattern and param. |
61
|
|
|
* |
62
|
|
|
* @var array |
63
|
|
|
*/ |
64
|
|
|
protected $timestamp = [ |
65
|
|
|
'pattern' => null, |
66
|
|
|
'param' => null, |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* The URL Queries. |
71
|
|
|
* |
72
|
|
|
* @var array |
73
|
|
|
*/ |
74
|
|
|
protected $queries = []; |
75
|
|
|
|
76
|
|
|
/* ----------------------------------------------------------------- |
77
|
|
|
| Constructor |
78
|
|
|
| ----------------------------------------------------------------- |
79
|
|
|
*/ |
80
|
|
|
/** |
81
|
|
|
* AbstractParser constructor. |
82
|
|
|
* |
83
|
|
|
* @param array $options |
84
|
|
|
*/ |
85
|
30 |
|
public function __construct(array $options) |
86
|
|
|
{ |
87
|
30 |
|
$this->queries = $this->defaultQueries(); |
88
|
|
|
// $this->setPatterns(Arr::get($options, 'patterns', [])); |
89
|
30 |
|
$this->setAttributes(Arr::get($options, 'attributes', [])); |
90
|
|
|
|
91
|
30 |
|
} |
92
|
|
|
|
93
|
|
|
/* ----------------------------------------------------------------- |
94
|
|
|
| Getters & Setters |
95
|
|
|
| ----------------------------------------------------------------- |
96
|
|
|
*/ |
97
|
|
|
/** |
98
|
|
|
* Get the URL. |
99
|
|
|
* |
100
|
|
|
* @return string|null |
101
|
|
|
*/ |
102
|
6 |
|
public function url() |
103
|
|
|
{ |
104
|
6 |
|
return $this->url; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* Set the URL. |
109
|
|
|
* |
110
|
|
|
* @param string $url |
111
|
|
|
* |
112
|
|
|
* @return self |
113
|
|
|
*/ |
114
|
24 |
|
protected function setUrl($url) |
115
|
|
|
{ |
116
|
24 |
|
$this->url = $url; |
117
|
24 |
|
$this->reset(); |
118
|
|
|
|
119
|
24 |
|
return $this; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Set the URL patterns. |
124
|
|
|
* |
125
|
|
|
* @param array $patterns |
126
|
|
|
* |
127
|
|
|
* @return self |
128
|
|
|
*/ |
129
|
|
|
public function setPatterns(array $patterns) |
130
|
|
|
{ |
131
|
|
|
$this->patterns = $patterns; |
132
|
|
|
|
133
|
|
|
return $this; |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
/** |
137
|
|
|
* Get the attributes. |
138
|
|
|
* |
139
|
|
|
* @return array |
140
|
|
|
*/ |
141
|
27 |
|
public function attributes() |
142
|
|
|
{ |
143
|
27 |
|
return $this->attributes; |
144
|
|
|
} |
145
|
|
|
|
146
|
|
|
/** |
147
|
|
|
* Set the attributes. |
148
|
|
|
* |
149
|
|
|
* @param array $attributes |
150
|
|
|
* |
151
|
|
|
* @return self |
152
|
|
|
*/ |
153
|
30 |
|
public function setAttributes(array $attributes) |
154
|
|
|
{ |
155
|
30 |
|
$this->attributes = $attributes; |
156
|
|
|
|
157
|
30 |
|
return $this; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
/** |
161
|
|
|
* Get the URL queries. |
162
|
|
|
* |
163
|
|
|
* @return array |
164
|
|
|
*/ |
165
|
27 |
|
public function queries() |
166
|
|
|
{ |
167
|
27 |
|
return $this->queries; |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
/** |
171
|
|
|
* Create an iframe entity. |
172
|
|
|
* |
173
|
|
|
* @return \Arcanedev\EmbedVideo\Entities\Iframe |
174
|
|
|
*/ |
175
|
21 |
|
public function iframe() |
176
|
|
|
{ |
177
|
21 |
|
return new Iframe( |
178
|
21 |
|
$this->getIframePattern(), |
179
|
21 |
|
$this->getIframeReplacer(), |
180
|
21 |
|
$this->queries(), |
181
|
21 |
|
$this->attributes() |
182
|
7 |
|
); |
183
|
|
|
} |
184
|
|
|
|
185
|
|
|
/** |
186
|
|
|
* Get value from cached matches. |
187
|
|
|
* |
188
|
|
|
* @param int $key |
189
|
|
|
* @param mixed|null $default |
190
|
|
|
* |
191
|
|
|
* @return mixed |
192
|
|
|
*/ |
193
|
21 |
|
public function getCached($key, $default = null) |
194
|
|
|
{ |
195
|
21 |
|
return Arr::get($this->cachedMatches, $key, $default); |
196
|
|
|
} |
197
|
|
|
|
198
|
|
|
/** |
199
|
|
|
* Get the video id from cached matches. |
200
|
|
|
* |
201
|
|
|
* @return mixed |
202
|
|
|
*/ |
203
|
|
|
abstract protected function videoId(); |
204
|
|
|
|
205
|
|
|
/** |
206
|
|
|
* Get the default URL queries. |
207
|
|
|
* |
208
|
|
|
* @return array |
209
|
|
|
*/ |
210
|
|
|
abstract protected function defaultQueries(); |
211
|
|
|
|
212
|
|
|
/** |
213
|
|
|
* Get the iframe pattern. |
214
|
|
|
* |
215
|
|
|
* @return string |
216
|
|
|
*/ |
217
|
|
|
abstract protected function getIframePattern(); |
218
|
|
|
|
219
|
|
|
/** |
220
|
|
|
* Get the iframe replace. |
221
|
|
|
* |
222
|
|
|
* @return array |
223
|
|
|
*/ |
224
|
|
|
abstract protected function getIframeReplacer(); |
225
|
|
|
|
226
|
|
|
/* ----------------------------------------------------------------- |
227
|
|
|
| Main method |
228
|
|
|
| ----------------------------------------------------------------- |
229
|
|
|
*/ |
230
|
|
|
/** |
231
|
|
|
* Parse the given url. |
232
|
|
|
* |
233
|
|
|
* @param string $url |
234
|
|
|
* |
235
|
|
|
* @return self |
236
|
|
|
*/ |
237
|
24 |
|
public function parse($url) |
238
|
|
|
{ |
239
|
24 |
|
$this->setUrl($url); |
240
|
|
|
|
241
|
24 |
|
foreach ($this->patterns as $pattern) { |
242
|
24 |
|
if (preg_match('~'.$pattern.'~imu', $this->url, $matches)) { |
243
|
24 |
|
$this->cachedMatches = $matches; |
244
|
24 |
|
$this->parseProtocol(); |
245
|
24 |
|
$this->parseTimestamp(); |
246
|
24 |
|
$this->parseCustomParsing(); |
247
|
24 |
|
break; |
248
|
|
|
} |
249
|
8 |
|
} |
250
|
|
|
|
251
|
24 |
|
return $this; |
252
|
|
|
} |
253
|
|
|
|
254
|
|
|
/* ----------------------------------------------------------------- |
255
|
|
|
| Other Methods |
256
|
|
|
| ----------------------------------------------------------------- |
257
|
|
|
*/ |
258
|
|
|
/** |
259
|
|
|
* Parse the HTTP protocol. |
260
|
|
|
* |
261
|
|
|
* @throws \Exception |
262
|
|
|
*/ |
263
|
24 |
|
private function parseProtocol() |
264
|
|
|
{ |
265
|
24 |
|
$protocol = $this->cachedMatches[1]; |
266
|
|
|
|
267
|
24 |
|
if ( ! in_array($protocol, ['http://', 'https://'])) |
268
|
8 |
|
$protocol = 'http://'; |
269
|
|
|
|
270
|
24 |
|
if (is_null($this->url)) |
271
|
8 |
|
throw new \Exception('Cannot detect protocol if URL or provider were not set.'); |
272
|
|
|
|
273
|
24 |
|
$this->protocol = ($this->useSecure || $protocol === 'https://') ? 'https' : 'http'; |
274
|
24 |
|
} |
275
|
|
|
|
276
|
|
|
/** |
277
|
|
|
* Parse the timestamp. |
278
|
|
|
*/ |
279
|
24 |
|
protected function parseTimestamp() |
280
|
|
|
{ |
281
|
24 |
|
if ( ! $this->hasTimestampOption()) return; |
282
|
|
|
|
283
|
24 |
|
if (preg_match('~'.$this->timestamp['pattern'].'~imu', $this->url, $matches)) { |
284
|
21 |
|
unset($matches[0]); |
285
|
21 |
|
$multipliers = [3600, 60, 1]; |
286
|
21 |
|
$timestamp = 0; |
287
|
21 |
|
foreach (array_values($matches) as $key => $match) { |
288
|
21 |
|
$timestamp += (int) $match * $multipliers[$key]; |
289
|
7 |
|
} |
290
|
21 |
|
$this->queries[$this->timestamp['param']] = $timestamp; |
291
|
7 |
|
} |
292
|
24 |
|
} |
293
|
|
|
|
294
|
|
|
/** |
295
|
|
|
* Check if the parser has a defined timestamp parsing. |
296
|
|
|
* |
297
|
|
|
* @return bool |
298
|
|
|
*/ |
299
|
24 |
|
protected function hasTimestampOption() |
300
|
|
|
{ |
301
|
24 |
|
return is_string(Arr::get($this->timestamp, 'pattern')) |
302
|
24 |
|
&& is_string(Arr::get($this->timestamp, 'param')); |
303
|
|
|
} |
304
|
|
|
|
305
|
|
|
/** |
306
|
|
|
* Enable to perform custom parsing. |
307
|
|
|
*/ |
308
|
24 |
|
protected function parseCustomParsing() |
309
|
|
|
{ |
310
|
|
|
// |
311
|
24 |
|
} |
312
|
|
|
|
313
|
|
|
/** |
314
|
|
|
* Reset the parser. |
315
|
|
|
*/ |
316
|
24 |
|
private function reset() |
317
|
|
|
{ |
318
|
24 |
|
$this->cachedMatches = []; |
319
|
24 |
|
$this->queries = $this->defaultQueries(); |
320
|
24 |
|
} |
321
|
|
|
} |
322
|
|
|
|