HasVideo.trait.php
1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
<?php
/**
* Created by Graymur
* Date: 14.12.2014
* Time: 12:39
*/
namespace Cpeople\Traits;
trait HasVideo
{
public function getVideoUrl()
{
return $this->getPropValue('VIDEO');
}
public function getVideoType()
{
$retval = false;
$video = $this->getPropValue('VIDEO');
if ($video && preg_match('/youtu/i', $video))
{
$retval = 'youtube';
}
else if ($video && preg_match('/vimeo/i', $video))
{
$retval = 'vimeo';
}
return $retval;
}
public function getVideoId()
{
$retval = false;
switch ($this->getVideoType())
{
case 'youtube':
preg_match(
"/^(?:http(?:s)?:\/\/)?(?:www\.)?(?:youtu\.be\/|youtube\.com\/(?:(?:watch)?\?(?:.*&)?v(?:i)?=|(?:embed|v|vi|user)\/))([^\?&\"'>]+)/",
$this->getVideoUrl(),
$m
);
$retval = $m[1];
break;
case 'vimeo':
preg_match(
"#(https?://)?(www.)?(player.)?vimeo.com/([a-z]*/)*([0-9]{6,11})[?]?.*#",
$this->getVideoUrl(),
$m
);
$retval = $m[5];
break;
}
return $retval;
}
}