1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Ricardo Fiorani |
5
|
|
|
* Date: 29/08/2015 |
6
|
|
|
* Time: 14:53. |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace RicardoFiorani\Adapter\Youtube; |
10
|
|
|
|
11
|
|
|
use RicardoFiorani\Adapter\AbstractServiceAdapter; |
12
|
|
|
use RicardoFiorani\Exception\InvalidThumbnailSizeException; |
13
|
|
|
use RicardoFiorani\Renderer\EmbedRendererInterface; |
14
|
|
|
|
15
|
|
|
class YoutubeServiceAdapter extends AbstractServiceAdapter |
16
|
|
|
{ |
17
|
|
|
const THUMBNAIL_DEFAULT = 'default'; |
|
|
|
|
18
|
|
|
const THUMBNAIL_STANDARD_DEFINITION = 'sddefault'; |
19
|
|
|
const THUMBNAIL_MEDIUM_QUALITY = 'mqdefault'; |
|
|
|
|
20
|
|
|
const THUMBNAIL_HIGH_QUALITY = 'hqdefault'; |
|
|
|
|
21
|
|
|
const THUMBNAIL_MAX_QUALITY = 'maxresdefault'; |
|
|
|
|
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* @param string $url |
25
|
|
|
* @param string $pattern |
26
|
|
|
* @param EmbedRendererInterface $renderer |
27
|
|
|
*/ |
28
|
14 |
|
public function __construct($url, $pattern, EmbedRendererInterface $renderer) |
29
|
|
|
{ |
30
|
14 |
|
preg_match($pattern, $url, $match); |
31
|
14 |
|
if (isset($match[2])) { |
32
|
10 |
|
$videoId = $match[2]; |
33
|
10 |
|
} |
34
|
14 |
|
if (empty($videoId)) { |
35
|
4 |
|
$videoId = $match[1]; |
36
|
4 |
|
} |
37
|
14 |
|
$this->setVideoId($videoId); |
38
|
|
|
|
39
|
14 |
|
return parent::__construct($url, $pattern, $renderer); |
|
|
|
|
40
|
|
|
} |
41
|
|
|
|
42
|
|
|
/** |
43
|
|
|
* Returns the service name (ie: "Youtube" or "Vimeo"). |
44
|
|
|
* |
45
|
|
|
* @return string |
46
|
|
|
*/ |
47
|
1 |
|
public function getServiceName() |
48
|
|
|
{ |
49
|
1 |
|
return 'Youtube'; |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Returns if the service has a thumbnail image. |
54
|
|
|
* |
55
|
|
|
* @return bool |
56
|
|
|
*/ |
57
|
1 |
|
public function hasThumbnail() |
58
|
|
|
{ |
59
|
1 |
|
return true; |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param string $size |
64
|
|
|
* @param bool $forceSecure |
65
|
|
|
* @return string |
66
|
|
|
* @throws InvalidThumbnailSizeException |
67
|
|
|
*/ |
68
|
2 |
View Code Duplication |
public function getThumbnail($size, $forceSecure = false) |
|
|
|
|
69
|
|
|
{ |
70
|
2 |
|
if (false == in_array($size, $this->getThumbNailSizes())) { |
|
|
|
|
71
|
1 |
|
throw new InvalidThumbnailSizeException(); |
72
|
|
|
} |
73
|
|
|
|
74
|
1 |
|
return $this->getScheme($forceSecure) . '://img.youtube.com/vi/' . $this->getVideoId() . '/' . $size . '.jpg'; |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
/** |
78
|
|
|
* @return array |
|
|
|
|
79
|
|
|
*/ |
80
|
3 |
|
public function getThumbNailSizes() |
81
|
|
|
{ |
82
|
|
|
return array( |
83
|
3 |
|
self::THUMBNAIL_DEFAULT, |
84
|
3 |
|
self::THUMBNAIL_STANDARD_DEFINITION, |
85
|
3 |
|
self::THUMBNAIL_MEDIUM_QUALITY, |
86
|
3 |
|
self::THUMBNAIL_HIGH_QUALITY, |
87
|
3 |
|
self::THUMBNAIL_MAX_QUALITY, |
88
|
3 |
|
); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
/** |
92
|
|
|
* @param bool $forceAutoplay |
93
|
|
|
* @param bool $forceSecure |
94
|
|
|
* @return string |
95
|
|
|
*/ |
96
|
2 |
|
public function getEmbedUrl($forceAutoplay = false, $forceSecure = false) |
97
|
|
|
{ |
98
|
2 |
|
$queryString = $this->generateQuerystring($forceAutoplay); |
99
|
|
|
|
100
|
2 |
|
return sprintf( |
101
|
2 |
|
'%s://www.youtube.com/embed/%s?%s', |
102
|
2 |
|
$this->getScheme($forceSecure), |
103
|
2 |
|
$this->getVideoId(), |
104
|
2 |
|
http_build_query($queryString) |
105
|
2 |
|
); |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
/** |
109
|
|
|
* Returns the small thumbnail's url. |
110
|
|
|
* |
111
|
|
|
* @return string |
112
|
|
|
*/ |
113
|
1 |
|
public function getSmallThumbnail($forceSecure = false) |
114
|
|
|
{ |
115
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_STANDARD_DEFINITION, $forceSecure); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
/** |
119
|
|
|
* Returns the medium thumbnail's url. |
120
|
|
|
* |
121
|
|
|
* @return string |
122
|
|
|
*/ |
123
|
1 |
|
public function getMediumThumbnail($forceSecure = false) |
124
|
|
|
{ |
125
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_MEDIUM_QUALITY, $forceSecure); |
126
|
|
|
} |
127
|
|
|
|
128
|
|
|
/** |
129
|
|
|
* Returns the large thumbnail's url. |
130
|
|
|
* |
131
|
|
|
* @return string |
132
|
|
|
*/ |
133
|
1 |
|
public function getLargeThumbnail($forceSecure = false) |
134
|
|
|
{ |
135
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_HIGH_QUALITY, $forceSecure); |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
/** |
139
|
|
|
* Returns the largest thumnbnaail's url. |
140
|
|
|
* |
141
|
|
|
* @return string |
142
|
|
|
*/ |
143
|
1 |
|
public function getLargestThumbnail($forceSecure = false) |
144
|
|
|
{ |
145
|
1 |
|
return $this->getThumbnail(self::THUMBNAIL_MAX_QUALITY, $forceSecure); |
146
|
|
|
} |
147
|
|
|
|
148
|
|
|
/** |
149
|
|
|
* @return bool |
150
|
|
|
*/ |
151
|
1 |
|
public function isEmbeddable() |
152
|
|
|
{ |
153
|
1 |
|
return true; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param bool $forceAutoplay |
158
|
|
|
* @return array |
|
|
|
|
159
|
|
|
*/ |
160
|
2 |
|
private function generateQuerystring($forceAutoplay = false) |
161
|
|
|
{ |
162
|
2 |
|
parse_str(parse_url($this->rawUrl, PHP_URL_QUERY), $queryString); |
163
|
2 |
|
unset($queryString['v']); |
164
|
|
|
|
165
|
2 |
|
if ($forceAutoplay) { |
166
|
|
|
$queryString['autoplay'] = (int) $forceAutoplay; |
167
|
|
|
} |
168
|
|
|
|
169
|
2 |
|
return $queryString; |
170
|
|
|
} |
171
|
|
|
} |
|
|
|
|
172
|
|
|
|
This check looks for multiple assignments in successive lines of code. It will report an issue if the operators are not in a straight line.
To visualize
will produce issues in the first and second line, while this second example
will produce no issues.