1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Jclyons52\PagePreview; |
4
|
|
|
|
5
|
|
|
class Media |
6
|
|
|
{ |
7
|
57 |
|
public static function get($url) |
8
|
|
|
{ |
9
|
57 |
|
if (strpos($url, "youtu") !== false) { |
10
|
9 |
|
$link = static::youtubeFormat($url); |
|
|
|
|
11
|
9 |
|
if ($link !== false) { |
12
|
6 |
|
return $link; |
13
|
|
|
} |
14
|
2 |
|
} |
15
|
51 |
|
if (strpos($url, "vimeo") !== false) { |
16
|
6 |
|
$link = static::vimeoFormat($url); |
|
|
|
|
17
|
6 |
|
if ($link !== false) { |
18
|
3 |
|
return $link; |
19
|
|
|
} |
20
|
2 |
|
} |
21
|
48 |
|
return []; |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @param $url |
26
|
|
|
* @return array|bool |
27
|
|
|
*/ |
28
|
9 |
|
private static function youtubeFormat($url) |
29
|
|
|
{ |
30
|
9 |
|
$id = null; |
31
|
|
|
$re = "/https?:\\/\\/(?:www\\.)?youtu(?:be\\.com\\/(watch\\?)?". |
32
|
9 |
|
"(.*?&?)?v=|\\.be\\/)([\\w\\-]+)(&(amp;)?[\\w\\?=]*)?/i"; |
33
|
9 |
|
if (preg_match($re, $url, $matching)) { |
34
|
6 |
|
$id = $matching[3]; |
35
|
4 |
|
} else { |
36
|
6 |
|
$re = "/https?:\\/\\/(?:www\\.)?youtube\\.com\\/embed\\/([^&|^?|^\\/]*)/i"; |
37
|
6 |
|
if (preg_match($re, $url, $matching)) { |
38
|
3 |
|
$id = $matching[1]; |
39
|
2 |
|
} |
40
|
|
|
} |
41
|
9 |
|
if ($id !== null) { |
42
|
|
|
return [ |
43
|
6 |
|
'url' => 'https://www.youtube.com/embed/' . $id, |
44
|
6 |
|
'thumbnail' => "http://i2.ytimg.com/vi/{$id}/default.jpg", |
45
|
4 |
|
]; |
46
|
|
|
} |
47
|
|
|
|
48
|
3 |
|
return false; |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param $url |
53
|
|
|
* @param $matches |
54
|
|
|
* @return array |
55
|
|
|
*/ |
56
|
6 |
|
private static function vimeoFormat($url) |
57
|
|
|
{ |
58
|
|
|
$re = "/https?:\\/\\/(?:www\\.|player\\.)?vimeo.com\\/" . |
59
|
4 |
|
"(?:channels\\/(?:\\w+\\/)?|groups\\/([^\\/]*)\\/videos\\/". |
60
|
6 |
|
"|album\\/(\\d+)\\/video\\/|video\\/|)(\\d+)(?:$|\\/|\\?)?/"; |
61
|
6 |
|
preg_match_all($re, $url, $matches); |
62
|
|
|
|
63
|
6 |
|
if (!$matches[3]) { |
64
|
3 |
|
return false; |
65
|
|
|
} |
66
|
3 |
|
$imgId = $matches[3][0]; |
67
|
|
|
|
68
|
3 |
|
$hash = static::fetch("http://vimeo.com/api/v2/video/{$imgId}.json"); |
69
|
|
|
|
70
|
|
|
return [ |
71
|
3 |
|
'url' => "http://player.vimeo.com/video/{$imgId}", |
72
|
3 |
|
'thumbnail' => $hash[0]->thumbnail_large, |
73
|
2 |
|
]; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param $url |
78
|
|
|
* @return mixed |
79
|
|
|
*/ |
80
|
|
|
protected static function fetch($url) |
81
|
|
|
{ |
82
|
|
|
return json_decode(file_get_contents($url)); |
83
|
|
|
} |
84
|
|
|
} |
85
|
|
|
|
Let’s assume you have a class which uses late-static binding:
}
The code above will run fine in your PHP runtime. However, if you now create a sub-class and call the
getSomeVariable()
on that sub-class, you will receive a runtime error:In the case above, it makes sense to update
SomeClass
to useself
instead: