Skip to content

Commit 7387825

Browse files
Merge pull request #2 from ricardofiorani/dev
0.3 Dev
2 parents b675436 + a98c091 commit 7387825

22 files changed

+775
-89
lines changed

.idea/vcs.xml

-6
This file was deleted.

README.md

+5-5
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,17 @@ $ composer require ricardofiorani/php-video-url-parser
2020
## Requirements
2121

2222
* PHP 5.3
23-
* cURL
23+
* cURL (Or at least file_get_contents() enabled if you want to use it with Vimeo, otherwise it's not required)
2424

2525
## Basic Usage
2626

2727
```php
2828
<?php
29-
use RicardoFiorani\Detector\VideoServiceDetector;
29+
use RicardoFiorani\Matcher\VideoServiceMatcher;
3030

3131
require __DIR__ . '/vendor/autoload.php';
3232

33-
$vsd = new VideoServiceDetector();
33+
$vsd = new VideoServiceMatcher();
3434

3535
//Detects which service the url belongs to and returns the service's implementation
3636
//of RicardoFiorani\Adapter\VideoAdapterInterface
@@ -114,11 +114,11 @@ class MyOwnRendererFactory implements RendererFactoryInterface
114114

115115
```php
116116
<?php
117-
use RicardoFiorani\Detector\VideoServiceDetector;
117+
use RicardoFiorani\Matcher\VideoServiceMatcher;
118118

119119
require __DIR__ . '/vendor/autoload.php';
120120

121-
$vsd = new VideoServiceDetector();
121+
$vsd = new VideoServiceMatcher();
122122

123123
//This is where the magic is done
124124
$vsd->getServiceContainer()->setRenderer('MyOwnRenderer', 'MyVendor\\MyRenderer\\Factory\\MyOwnRendererFactory');

config/config.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
'services' => array(
1111
'Vimeo' => array(
1212
'patterns' => array(
13-
'#(https?://vimeo.com)/([0-9]+)#i'
13+
'#(https?://vimeo.com)/([0-9]+)#i',
14+
'#(https?://vimeo.com)/channels/staffpicks/([0-9]+)#i',
1415
),
1516
'factory' => '\\RicardoFiorani\\Adapter\\Vimeo\\Factory\\VimeoServiceAdapterFactory',
1617
),

example/RegisteringANewService.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -159,11 +159,11 @@ class DailymotionServiceAdapterFactory implements \RicardoFiorani\Adapter\Factor
159159

160160
```php
161161
<?php
162-
use RicardoFiorani\Detector\VideoServiceDetector;
162+
use RicardoFiorani\Matcher\VideoServiceMatcher;
163163

164164
require __DIR__ . '/vendor/autoload.php';
165165

166-
$vsd = new VideoServiceDetector();
166+
$vsd = new VideoServiceMatcher();
167167
//The Service Name
168168
$serviceName = 'Dailymotion';
169169

src/Adapter/Dailymotion/DailymotionServiceAdapter.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
namespace RicardoFiorani\Adapter\Dailymotion;
1010

1111
use RicardoFiorani\Adapter\AbstractServiceAdapter;
12+
use RicardoFiorani\Exception\InvalidThumbnailSizeException;
1213
use RicardoFiorani\Renderer\EmbedRendererInterface;
1314

1415
class DailymotionServiceAdapter extends AbstractServiceAdapter
@@ -62,11 +63,12 @@ public function getThumbNailSizes()
6263
/**
6364
* @param string $size
6465
* @return string
66+
* @throws InvalidThumbnailSizeException
6567
*/
6668
public function getThumbnail($size)
6769
{
6870
if (false == in_array($size, $this->getThumbNailSizes())) {
69-
throw new \RicardoFiorani\Exception\InvalidThumbnailSizeException;
71+
throw new InvalidThumbnailSizeException;
7072
}
7173

7274
return 'http://www.dailymotion.com/' . $size . '/video/' . $this->videoId;

src/Adapter/Facebook/FacebookServiceAdapter.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ public function __construct($url, $pattern, EmbedRendererInterface $renderer)
3030
$match = array();
3131
preg_match($pattern, $url, $match);
3232
$this->setVideoId($match[1]);
33-
3433
return parent::__construct($url, $pattern, $renderer);
3534
}
3635

@@ -130,4 +129,4 @@ public function isEmbeddable()
130129
{
131130
return true;
132131
}
133-
}
132+
}

src/Adapter/VideoAdapterInterface.php

+1-4
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,6 @@
88

99
namespace RicardoFiorani\Adapter;
1010

11-
12-
use RicardoFiorani\Renderer\EmbedRendererInterface;
13-
1411
interface VideoAdapterInterface
1512
{
1613

@@ -63,7 +60,7 @@ public function getMediumThumbnail();
6360
public function getLargeThumbnail();
6461

6562
/**
66-
* Returns the largest thumnbnaail's url
63+
* Returns the largest thumnbnail's url
6764
* @return string
6865
*/
6966
public function getLargestThumbnail();

src/Adapter/Vimeo/Factory/VimeoServiceAdapterFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@ class VimeoServiceAdapterFactory implements CallableServiceAdapterFactoryInterfa
1818
/**
1919
* @param string $url
2020
* @param string $pattern
21-
* @param EmbedRendererInterface $rendererInterface
21+
* @param EmbedRendererInterface $renderer
2222
* @return VimeoServiceAdapter
23+
* @internal param EmbedRendererInterface $rendererInterface
2324
*/
2425
public function __invoke($url, $pattern, EmbedRendererInterface $renderer)
2526
{
2627
$adapter = new VimeoServiceAdapter($url, $pattern, $renderer);
27-
2828
return $adapter;
2929
}
3030
}

src/Adapter/Vimeo/VimeoServiceAdapter.php

+53-39
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,29 @@
1111

1212
use RicardoFiorani\Adapter\AbstractServiceAdapter;
1313
use RicardoFiorani\Exception\InvalidThumbnailSizeException;
14+
use RicardoFiorani\Exception\ServiceApiNotAvailable;
1415
use RicardoFiorani\Renderer\EmbedRendererInterface;
1516

1617
class VimeoServiceAdapter extends AbstractServiceAdapter
1718
{
18-
1919
const THUMBNAIL_SMALL = 'thumbnail_small';
2020
const THUMBNAIL_MEDIUM = 'thumbnail_medium';
2121
const THUMBNAIL_LARGE = 'thumbnail_large';
2222

23+
/**
24+
* @var string
25+
*/
26+
public $title;
27+
28+
/**
29+
* @var string
30+
*/
31+
public $description;
32+
33+
/**
34+
* @var array
35+
*/
36+
public $thumbnails;
2337

2438
/**
2539
* @param string $url
@@ -28,49 +42,22 @@ class VimeoServiceAdapter extends AbstractServiceAdapter
2842
*/
2943
public function __construct($url, $pattern, EmbedRendererInterface $renderer)
3044
{
31-
$match = array();
32-
preg_match($pattern, $url, $match);
33-
/*Gets the Video ID*/
34-
$videoId = $match[2];
35-
if (empty($videoId)) {
36-
$videoId = $match[1];
37-
}
38-
45+
$videoId = $this->getVideoIdByPattern($url, $pattern);
3946
$this->setVideoId($videoId);
40-
41-
/*Sends the video ID to the API to get the thumbnails and other infos*/
42-
$hash = unserialize(@file_get_contents("http://vimeo.com/api/v2/video/" . $this->getVideoId() . ".php"));
43-
$data = $hash[0];
44-
47+
$videoData = $this->getVideoDataFromServiceApi();
4548

4649
$this->setThumbnails(array(
47-
self::THUMBNAIL_SMALL => $data[self::THUMBNAIL_SMALL],
48-
self::THUMBNAIL_MEDIUM => $data[self::THUMBNAIL_MEDIUM],
49-
self::THUMBNAIL_LARGE => $data[self::THUMBNAIL_LARGE],
50+
self::THUMBNAIL_SMALL => $videoData[self::THUMBNAIL_SMALL],
51+
self::THUMBNAIL_MEDIUM => $videoData[self::THUMBNAIL_MEDIUM],
52+
self::THUMBNAIL_LARGE => $videoData[self::THUMBNAIL_LARGE],
5053
));
5154

52-
$this->setTitle($data['title']);
53-
$this->setDescription($data['description']);
55+
$this->setTitle($videoData['title']);
56+
$this->setDescription($videoData['description']);
5457

5558
return parent::__construct($url, $pattern, $renderer);
5659
}
5760

58-
/**
59-
* @var string
60-
*/
61-
public $title;
62-
63-
/**
64-
* @var string
65-
*/
66-
public $description;
67-
68-
/**
69-
* @var array
70-
*/
71-
public $thumbnails;
72-
73-
7461
/**
7562
* Returns the service name (ie: "Youtube" or "Vimeo")
7663
* @return string
@@ -80,7 +67,6 @@ public function getServiceName()
8067
return 'Vimeo';
8168
}
8269

83-
8470
/**
8571
* Returns if the service has a thumbnail image
8672
* @return bool
@@ -90,7 +76,6 @@ public function hasThumbnail()
9076
return false == empty($this->thumbnails);
9177
}
9278

93-
9479
/**
9580
* @return string
9681
*/
@@ -139,7 +124,6 @@ public function setThumbnails($thumbnails)
139124
$this->thumbnails = $thumbnails;
140125
}
141126

142-
143127
/**
144128
* @param string $size
145129
* @return string
@@ -176,7 +160,6 @@ public function getThumbNailSizes()
176160
);
177161
}
178162

179-
180163
/**
181164
* Returns the small thumbnail's url
182165
* @return string
@@ -220,4 +203,35 @@ public function isEmbeddable()
220203
{
221204
return true;
222205
}
206+
207+
/**
208+
* @param string $url
209+
* @param string $pattern
210+
* @return int
211+
*/
212+
private function getVideoIdByPattern($url, $pattern)
213+
{
214+
$match = array();
215+
preg_match($pattern, $url, $match);
216+
$videoId = $match[2];
217+
218+
return $videoId;
219+
}
220+
221+
/**
222+
* Uses the Vimeo video API to get video info
223+
* @todo make this better by using guzzle
224+
* @return array
225+
* @throws ServiceApiNotAvailable
226+
*/
227+
private function getVideoDataFromServiceApi()
228+
{
229+
$contents = file_get_contents("http://vimeo.com/api/v2/video/" . $this->getVideoId() . ".php");
230+
if (false === $contents) {
231+
throw new ServiceApiNotAvailable('Vimeo Service Adapter could not reach Vimeo API Service. Check if your server has file_get_contents() function available.');
232+
}
233+
$hash = unserialize($contents);
234+
235+
return reset($hash);
236+
}
223237
}

src/Adapter/Youtube/YoutubeServiceAdapter.php

-2
Original file line numberDiff line numberDiff line change
@@ -15,14 +15,12 @@
1515

1616
class YoutubeServiceAdapter extends AbstractServiceAdapter
1717
{
18-
1918
const THUMBNAIL_DEFAULT = 'default';
2019
const THUMBNAIL_STANDARD_DEFINITION = 'sddefault';
2120
const THUMBNAIL_MEDIUM_QUALITY = 'mqdefault';
2221
const THUMBNAIL_HIGH_QUALITY = 'hqdefault';
2322
const THUMBNAIL_MAX_QUALITY = 'maxresdefault';
2423

25-
2624
/**
2725
* @param string $url
2826
* @param string $pattern

src/Container/Factory/ServicesContainerFactory.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,10 @@ public function __invoke()
2222
return $servicesContainer;
2323
}
2424

25-
public static function createNewServiceDetector()
25+
public static function createNewServiceMatcher()
2626
{
2727
$factory = new self();
2828

2929
return $factory();
3030
}
31-
}
31+
}

src/Exception/InvalidThumbnailSizeException.php

-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88

99
namespace RicardoFiorani\Exception;
1010

11-
1211
use Exception;
1312

1413
class InvalidThumbnailSizeException extends Exception
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 10/02/2016
6+
* Time: 18:13
7+
*/
8+
9+
namespace RicardoFiorani\Exception;
10+
11+
use Exception;
12+
13+
class ServiceApiNotAvailable extends Exception
14+
{
15+
16+
}

src/Detector/VideoServiceDetector.php src/Matcher/VideoServiceMatcher.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace RicardoFiorani\Detector;
3+
namespace RicardoFiorani\Matcher;
44

55
use RicardoFiorani\Adapter\VideoAdapterInterface;
66
use RicardoFiorani\Container\Factory\ServicesContainerFactory;
@@ -10,7 +10,7 @@
1010
/**
1111
* @author Ricardo Fiorani
1212
*/
13-
class VideoServiceDetector
13+
class VideoServiceMatcher
1414
{
1515
/**
1616
* @var ServicesContainer
@@ -23,11 +23,11 @@ class VideoServiceDetector
2323
private $parsedUrls = array();
2424

2525
/**
26-
* VideoServiceDetector constructor.
26+
* VideoServiceMatcher constructor.
2727
*/
2828
public function __construct()
2929
{
30-
$this->serviceContainer = ServicesContainerFactory::createNewServiceDetector();
30+
$this->serviceContainer = ServicesContainerFactory::createNewServiceMatcher();
3131
}
3232

3333
/**

0 commit comments

Comments
 (0)