1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Created by PhpStorm. |
4
|
|
|
* User: Ricardo Fiorani |
5
|
|
|
* Date: 29/08/2015 |
6
|
|
|
* Time: 14:56. |
7
|
|
|
*/ |
8
|
|
|
namespace RicardoFiorani\Adapter\Vimeo; |
9
|
|
|
|
10
|
|
|
use RicardoFiorani\Adapter\AbstractServiceAdapter; |
11
|
|
|
use RicardoFiorani\Exception\InvalidThumbnailSizeException; |
12
|
|
|
use RicardoFiorani\Exception\ServiceApiNotAvailable; |
13
|
|
|
use RicardoFiorani\Renderer\EmbedRendererInterface; |
14
|
|
|
|
15
|
|
|
class VimeoServiceAdapter extends AbstractServiceAdapter |
16
|
|
|
{ |
17
|
|
|
const THUMBNAIL_SMALL = 'thumbnail_small'; |
18
|
|
|
const THUMBNAIL_MEDIUM = 'thumbnail_medium'; |
19
|
|
|
const THUMBNAIL_LARGE = 'thumbnail_large'; |
20
|
|
|
|
21
|
|
|
/** |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $title; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var string |
28
|
|
|
*/ |
29
|
|
|
public $description; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @var array |
33
|
|
|
*/ |
34
|
|
|
public $thumbnails; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param string $url |
38
|
|
|
* @param string $pattern |
39
|
|
|
* @param EmbedRendererInterface $renderer |
40
|
|
|
*/ |
41
|
|
|
public function __construct($url, $pattern, EmbedRendererInterface $renderer) |
42
|
|
|
{ |
43
|
|
|
$videoId = $this->getVideoIdByPattern($url, $pattern); |
44
|
|
|
$this->setVideoId($videoId); |
45
|
|
|
$videoData = $this->getVideoDataFromServiceApi(); |
46
|
|
|
|
47
|
|
|
$this->setThumbnails(array( |
48
|
|
|
self::THUMBNAIL_SMALL => $videoData[self::THUMBNAIL_SMALL], |
49
|
|
|
self::THUMBNAIL_MEDIUM => $videoData[self::THUMBNAIL_MEDIUM], |
50
|
|
|
self::THUMBNAIL_LARGE => $videoData[self::THUMBNAIL_LARGE], |
51
|
|
|
)); |
52
|
|
|
|
53
|
|
|
$this->setTitle($videoData['title']); |
54
|
|
|
$this->setDescription($videoData['description']); |
55
|
|
|
|
56
|
|
|
return parent::__construct($url, $pattern, $renderer); |
|
|
|
|
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Returns the service name (ie: "Youtube" or "Vimeo"). |
61
|
|
|
* |
62
|
|
|
* @return string |
63
|
|
|
*/ |
64
|
|
|
public function getServiceName() |
65
|
|
|
{ |
66
|
|
|
return 'Vimeo'; |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* Returns if the service has a thumbnail image. |
71
|
|
|
* |
72
|
|
|
* @return bool |
73
|
|
|
*/ |
74
|
|
|
public function hasThumbnail() |
75
|
|
|
{ |
76
|
|
|
return false == empty($this->thumbnails); |
|
|
|
|
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return string |
81
|
|
|
*/ |
82
|
|
|
public function getTitle() |
83
|
|
|
{ |
84
|
|
|
return $this->title; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* @param string $title |
89
|
|
|
*/ |
90
|
|
|
public function setTitle($title) |
91
|
|
|
{ |
92
|
|
|
$this->title = $title; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* @return string |
97
|
|
|
*/ |
98
|
|
|
public function getDescription() |
99
|
|
|
{ |
100
|
|
|
return $this->description; |
101
|
|
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param string $description |
105
|
|
|
*/ |
106
|
|
|
public function setDescription($description) |
107
|
|
|
{ |
108
|
|
|
$this->description = $description; |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* @param array $thumbnails |
113
|
|
|
*/ |
114
|
|
|
private function setThumbnails(array $thumbnails) |
115
|
|
|
{ |
116
|
|
|
foreach ($thumbnails as $key => $thumbnail) { |
117
|
|
|
$this->thumbnails[$key] = parse_url($thumbnail); |
118
|
|
|
} |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* @param string $size |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
* |
126
|
|
|
* @throws InvalidThumbnailSizeException |
127
|
|
|
*/ |
128
|
|
|
public function getThumbnail($size, $forceSecure = false) |
129
|
|
|
{ |
130
|
|
|
if (false == in_array($size, $this->getThumbNailSizes())) { |
|
|
|
|
131
|
|
|
throw new InvalidThumbnailSizeException(); |
132
|
|
|
} |
133
|
|
|
|
134
|
|
|
return sprintf( |
135
|
|
|
'%s://%s%s', |
136
|
|
|
$this->getScheme($forceSecure), |
137
|
|
|
$this->thumbnails[$size]['host'], |
138
|
|
|
$this->thumbnails[$size]['path'] |
139
|
|
|
); |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* @param bool $forceAutoplay |
144
|
|
|
* |
145
|
|
|
* @return string |
146
|
|
|
*/ |
147
|
|
|
public function getEmbedUrl($forceAutoplay = false, $forceSecure = false) |
148
|
|
|
{ |
149
|
|
|
return $this->getScheme($forceSecure) . '://player.vimeo.com/video/' . $this->getVideoId() . ($forceAutoplay ? '?autoplay=1' : ''); |
150
|
|
|
} |
151
|
|
|
|
152
|
|
|
/** |
153
|
|
|
* Returns all thumbnails available sizes. |
154
|
|
|
* |
155
|
|
|
* @return array |
|
|
|
|
156
|
|
|
*/ |
157
|
|
|
public function getThumbNailSizes() |
158
|
|
|
{ |
159
|
|
|
return array( |
160
|
|
|
self::THUMBNAIL_SMALL, |
161
|
|
|
self::THUMBNAIL_MEDIUM, |
162
|
|
|
self::THUMBNAIL_LARGE, |
163
|
|
|
); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Returns the small thumbnail's url. |
168
|
|
|
* |
169
|
|
|
* @param bool $forceSecure |
170
|
|
|
* @return string |
171
|
|
|
* @throws InvalidThumbnailSizeException |
172
|
|
|
*/ |
173
|
|
|
public function getSmallThumbnail($forceSecure = false) |
174
|
|
|
{ |
175
|
|
|
return $this->getThumbnail(self::THUMBNAIL_SMALL,$forceSecure); |
176
|
|
|
} |
177
|
|
|
|
178
|
|
|
/** |
179
|
|
|
* Returns the medium thumbnail's url. |
180
|
|
|
* |
181
|
|
|
* @param bool $forceSecure |
182
|
|
|
* @return string |
183
|
|
|
* @throws InvalidThumbnailSizeException |
184
|
|
|
*/ |
185
|
|
|
public function getMediumThumbnail($forceSecure = false) |
186
|
|
|
{ |
187
|
|
|
return $this->getThumbnail(self::THUMBNAIL_MEDIUM,$forceSecure); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* Returns the large thumbnail's url. |
192
|
|
|
* |
193
|
|
|
* @param bool $forceSecure |
194
|
|
|
* @param $forceSecure |
195
|
|
|
* @return string |
196
|
|
|
* @throws InvalidThumbnailSizeException |
197
|
|
|
*/ |
198
|
|
|
public function getLargeThumbnail($forceSecure = false) |
199
|
|
|
{ |
200
|
|
|
return $this->getThumbnail(self::THUMBNAIL_LARGE,$forceSecure); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* Returns the largest thumnbnaail's url. |
205
|
|
|
* |
206
|
|
|
* @param bool $forceSecure |
207
|
|
|
* @param $forceSecure |
208
|
|
|
* @return string |
209
|
|
|
* @throws InvalidThumbnailSizeException |
210
|
|
|
*/ |
211
|
|
|
public function getLargestThumbnail($forceSecure = false) |
212
|
|
|
{ |
213
|
|
|
return $this->getThumbnail(self::THUMBNAIL_LARGE,$forceSecure); |
214
|
|
|
} |
215
|
|
|
|
216
|
|
|
/** |
217
|
|
|
* @return bool |
218
|
|
|
*/ |
219
|
|
|
public function isEmbeddable() |
220
|
|
|
{ |
221
|
|
|
return true; |
222
|
|
|
} |
223
|
|
|
|
224
|
|
|
/** |
225
|
|
|
* @param string $url |
226
|
|
|
* @param string $pattern |
227
|
|
|
* |
228
|
|
|
* @return int |
|
|
|
|
229
|
|
|
*/ |
230
|
|
|
private function getVideoIdByPattern($url, $pattern) |
231
|
|
|
{ |
232
|
|
|
$match = array(); |
233
|
|
|
preg_match($pattern, $url, $match); |
234
|
|
|
$videoId = $match[2]; |
235
|
|
|
|
236
|
|
|
return $videoId; |
237
|
|
|
} |
238
|
|
|
|
239
|
|
|
/** |
240
|
|
|
* Uses the Vimeo video API to get video info. |
241
|
|
|
* |
242
|
|
|
* @todo make this better by using guzzle |
243
|
|
|
* |
244
|
|
|
* @return array |
245
|
|
|
* |
246
|
|
|
* @throws ServiceApiNotAvailable |
247
|
|
|
*/ |
248
|
|
|
private function getVideoDataFromServiceApi() |
249
|
|
|
{ |
250
|
|
|
$contents = file_get_contents('http://vimeo.com/api/v2/video/' . $this->getVideoId() . '.php'); |
251
|
|
|
if (false === $contents) { |
252
|
|
|
throw new ServiceApiNotAvailable('Vimeo Service Adapter could not reach Vimeo API Service. Check if your server has file_get_contents() function available.'); |
253
|
|
|
} |
254
|
|
|
$hash = unserialize($contents); |
255
|
|
|
|
256
|
|
|
return reset($hash); |
257
|
|
|
} |
258
|
|
|
} |
259
|
|
|
|