Skip to content

Commit c136bd7

Browse files
some better naming conventions
1 parent 954aa2a commit c136bd7

14 files changed

+325
-241
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
1-
# videoParser
1+
# PHP Video URL Parser
22
Simple Video Parser in PHP

config/config.php

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,17 @@
1010
* Array with Available Services
1111
*/
1212
return array(
13-
'#(https?://vimeo.com)/([0-9]+)#i' => 'RicardoFiorani\\Container\\Factory\\VimeoVideoContainerFactory',
14-
);
13+
'Vimeo' => array(
14+
'patterns' => array(
15+
'#(https?://vimeo.com)/([0-9]+)#i'
16+
),
17+
'factory' => 'RicardoFiorani\\Container\\Factory\\VimeoVideoContainerFactory',
18+
),
19+
'Youtube' => array(
20+
'patterns' => array(
21+
'#(?:<\>]+href=\")?(?:http://)?((?:[a-zA-Z]{1,4}\.)?youtube.com/(?:watch)?\?v=(.{11}?))[^"]*(?:\"[^\<\>]*>)?([^\<\>]*)(?:)?#',
22+
'%(?:youtube\.com/(?:[^/]+/.+/|(?:v|e(?:mbed)?)/|.*[?&]v=)|youtu\.be/)([^"&?/ ]{11})%i',
23+
),
24+
'factory' => 'RicardoFiorani\\Container\\Factory\\YoutubeVideoContainerFactory',
25+
),
26+
);
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 29/08/2015
6+
* Time: 19:37
7+
*/
8+
9+
namespace RicardoFiorani\Adapter;
10+
11+
12+
abstract class AbstractServiceAdapter implements VideoAdapterInterface
13+
{
14+
/**
15+
* @var string
16+
*/
17+
public $rawUrl;
18+
19+
/**
20+
* @var string
21+
*/
22+
public $videoId;
23+
24+
/**
25+
* AbstractVideoAdapter constructor.
26+
* @param string $url
27+
*/
28+
public function __construct($url, $regex)
29+
{
30+
$this->rawUrl = $url;
31+
}
32+
33+
/**
34+
* Returns the input URL
35+
* @return string
36+
*/
37+
public function getRawUrl()
38+
{
39+
return $this->rawUrl;
40+
}
41+
42+
/**
43+
* @param string $rawUrl
44+
*/
45+
public function setRawUrl($rawUrl)
46+
{
47+
$this->rawUrl = $rawUrl;
48+
}
49+
50+
/**
51+
* @return string
52+
*/
53+
public function getVideoId()
54+
{
55+
return $this->videoId;
56+
}
57+
58+
/**
59+
* @param string $videoId
60+
*/
61+
public function setVideoId($videoId)
62+
{
63+
$this->videoId = $videoId;
64+
}
65+
66+
}

src/Container/Factory/CallableFactoryInterface.php renamed to src/Adapter/Factory/CallableFactoryInterface.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
* Time: 14:57
77
*/
88

9-
namespace RicardoFiorani\Container\Factory;
9+
namespace RicardoFiorani\Adapter\Factory;
1010

1111

1212
use RicardoFiorani\Adapter\VideoAdapterInterface;
@@ -15,8 +15,8 @@ interface CallableFactoryInterface
1515
{
1616
/**
1717
* @param string $url
18-
* @param string $patternMatched
18+
* @param string $regex
1919
* @return VideoAdapterInterface
2020
*/
21-
public function __invoke($url, $patternMatched);
22-
}
21+
public function __invoke($url, $regex);
22+
}

src/Adapter/VideoAdapterInterface.php

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@
1212
interface VideoAdapterInterface
1313
{
1414
/**
15-
* @param string $rawUrl
15+
* @param string $url
1616
*/
17-
public function __construct($rawUrl);
17+
public function __construct($url, $regex);
1818

1919
/**
2020
* Returns the service name (ie: "Youtube" or "Vimeo")
@@ -34,6 +34,12 @@ public function getRawUrl();
3434
*/
3535
public function hasThumbnail();
3636

37+
/**
38+
* Returns all thumbnails available sizes
39+
* @return array
40+
*/
41+
public function getThumbNailSizes();
42+
3743
/**
3844
* @param string $size
3945
* @return string
@@ -45,4 +51,4 @@ public function getThumbnail($size);
4551
* @return string
4652
*/
4753
public function getEmbedUrl($autoplay = false);
48-
}
54+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 29/08/2015
6+
* Time: 14:56
7+
*/
8+
9+
namespace RicardoFiorani\Adapter\Vimeo\Factory;
10+
11+
12+
use RicardoFiorani\Adapter\Factory\CallableFactoryInterface;
13+
use RicardoFiorani\Adapter\Vimeo\VimeoServiceAdapter;
14+
15+
class VimeoServiceAdapterFactory implements CallableFactoryInterface
16+
{
17+
/**
18+
* @param string $url
19+
* @param string $regex
20+
* @return VimeoServiceAdapter
21+
*/
22+
public function __invoke($url, $regex)
23+
{
24+
$vimeoAdapter = new VimeoServiceAdapter($url,$regex);
25+
return $vimeoAdapter;
26+
}
27+
}

src/Container/VimeoVideoContainer.php renamed to src/Adapter/Vimeo/VimeoServiceAdapter.php

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -6,28 +6,48 @@
66
* Time: 14:56
77
*/
88

9-
namespace RicardoFiorani\Container;
9+
namespace RicardoFiorani\Adapter\Vimeo;
1010

1111

12-
use RicardoFiorani\Adapter\VideoAdapterInterface;
12+
use RicardoFiorani\Adapter\AbstractServiceAdapter;
13+
use RicardoFiorani\Exception\InvalidThumbnailSizeException;
1314

14-
class VimeoVideoContainer implements VideoAdapterInterface
15+
class VimeoServiceAdapter extends AbstractServiceAdapter
1516
{
1617

1718
const THUMBNAIL_SMALL = 'thumbnail_small';
1819
const THUMBNAIL_MEDIUM = 'thumbnail_medium';
1920
const THUMBNAIL_LARGE = 'thumbnail_large';
2021

2122

22-
/**
23-
* @var string
24-
*/
25-
public $rawUrl;
23+
public function __construct($url, $regex)
24+
{
25+
$match = array();
26+
preg_match($regex, $url, $match);
27+
/*Gets the Video ID*/
28+
$videoId = $match[2];
29+
if (empty($videoId)) {
30+
$videoId = $match[1];
31+
}
2632

27-
/**
28-
* @var string
29-
*/
30-
public $videoId;
33+
$this->setVideoId($videoId);
34+
35+
/*Sends the video ID to the API to get the thumbnails and other infos*/
36+
$hash = unserialize(@file_get_contents("http://vimeo.com/api/v2/video/$videoId.php"));
37+
$data = $hash[0];
38+
39+
40+
$this->setThumbnails(array(
41+
self::THUMBNAIL_SMALL => $data[self::THUMBNAIL_SMALL],
42+
self::THUMBNAIL_MEDIUM => $data[self::THUMBNAIL_MEDIUM],
43+
self::THUMBNAIL_LARGE => $data[self::THUMBNAIL_LARGE],
44+
));
45+
46+
$this->setTitle($data['title']);
47+
$this->setDescription($data['description']);
48+
49+
return parent::__construct($url, $regex);
50+
}
3151

3252
/**
3353
* @var string
@@ -44,13 +64,6 @@ class VimeoVideoContainer implements VideoAdapterInterface
4464
*/
4565
public $thumbnails;
4666

47-
/**
48-
* @param string $rawUrl
49-
*/
50-
public function __construct($rawUrl)
51-
{
52-
$this->setRawUrl($rawUrl);
53-
}
5467

5568
/**
5669
* Returns the service name (ie: "Youtube" or "Vimeo")
@@ -61,14 +74,6 @@ public function getServiceName()
6174
return 'Vimeo';
6275
}
6376

64-
/**
65-
* Returns the input URL
66-
* @return string
67-
*/
68-
public function getRawUrl()
69-
{
70-
return $this->rawUrl;
71-
}
7277

7378
/**
7479
* Returns if the service has a thumbnail image
@@ -79,30 +84,6 @@ public function hasThumbnail()
7984
return false == empty($this->thumbnails);
8085
}
8186

82-
/**
83-
* @param string $rawUrl
84-
*/
85-
public function setRawUrl($rawUrl)
86-
{
87-
$this->rawUrl = $rawUrl;
88-
}
89-
90-
91-
/**
92-
* @return string
93-
*/
94-
public function getVideoId()
95-
{
96-
return $this->videoId;
97-
}
98-
99-
/**
100-
* @param string $videoId
101-
*/
102-
public function setVideoId($videoId)
103-
{
104-
$this->videoId = $videoId;
105-
}
10687

10788
/**
10889
* @return string
@@ -159,7 +140,11 @@ public function setThumbnails($thumbnails)
159140
*/
160141
public function getThumbnail($size)
161142
{
162-
return $this->thumbnails($size);
143+
if (false == in_array($size, $this->getThumbNailSizes())) {
144+
throw new InvalidThumbnailSizeException();
145+
}
146+
147+
return $this->thumbnails[$size];
163148
}
164149

165150
/**
@@ -170,4 +155,19 @@ public function getEmbedUrl($autoplay = false)
170155
{
171156
return "http://player.vimeo.com/video/$videoId?byline=0&amp;portrait=0&amp" . ($autoplay ? '&amp&autoplay=1' : '');
172157
}
173-
}
158+
159+
/**
160+
* Returns all thumbnails available sizes
161+
* @return array
162+
*/
163+
public function getThumbNailSizes()
164+
{
165+
return array(
166+
self::THUMBNAIL_SMALL,
167+
self::THUMBNAIL_MEDIUM,
168+
self::THUMBNAIL_LARGE,
169+
);
170+
}
171+
172+
173+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 29/08/2015
6+
* Time: 19:24
7+
*/
8+
9+
namespace RicardoFiorani\Adapter\Youtube\Factory;
10+
11+
12+
use RicardoFiorani\Adapter\Factory\CallableFactoryInterface;
13+
use RicardoFiorani\Adapter\Youtube\YoutubeServiceAdapter;
14+
15+
class YoutubeServiceAdapterFactory implements CallableFactoryInterface
16+
{
17+
18+
/**
19+
* @param string $url
20+
* @param string $regex
21+
* @return YoutubeServiceAdapter
22+
*/
23+
public function __invoke($url, $regex)
24+
{
25+
$youtubeVideoAdapter = new YoutubeServiceAdapter($url, $regex);
26+
27+
return $youtubeVideoAdapter;
28+
}
29+
}

0 commit comments

Comments
 (0)