Skip to content

Commit 954aa2a

Browse files
kickstart
1 parent 0834fc3 commit 954aa2a

File tree

8 files changed

+458
-0
lines changed

8 files changed

+458
-0
lines changed

composer.json

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
{
2+
"name": "ricardofiorani/php-video-url-parser",
3+
"description": "PHP Video URL Parser is a simple video url parser.",
4+
"require-dev": {
5+
"phpunit/phpunit": "^4.8"
6+
},
7+
"license": "MIT",
8+
"authors": [
9+
{
10+
"name": "Ricardo Fiorani",
11+
"email": "[email protected]"
12+
}
13+
],
14+
"require": {
15+
"php": ">=5.3"
16+
},
17+
"autoload": {
18+
"psr-4": {
19+
"RicardoFiorani\\": "src/"
20+
}
21+
}
22+
}

config/config.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 29/08/2015
6+
* Time: 14:34
7+
*/
8+
9+
/**
10+
* Array with Available Services
11+
*/
12+
return array(
13+
'#(https?://vimeo.com)/([0-9]+)#i' => 'RicardoFiorani\\Container\\Factory\\VimeoVideoContainerFactory',
14+
);

src/Adapter/VideoAdapterInterface.php

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 29/08/2015
6+
* Time: 14:15
7+
*/
8+
9+
namespace RicardoFiorani\Adapter;
10+
11+
12+
interface VideoAdapterInterface
13+
{
14+
/**
15+
* @param string $rawUrl
16+
*/
17+
public function __construct($rawUrl);
18+
19+
/**
20+
* Returns the service name (ie: "Youtube" or "Vimeo")
21+
* @return string
22+
*/
23+
public function getServiceName();
24+
25+
/**
26+
* Returns the input URL
27+
* @return string
28+
*/
29+
public function getRawUrl();
30+
31+
/**
32+
* Returns if the service has a thumbnail image
33+
* @return bool
34+
*/
35+
public function hasThumbnail();
36+
37+
/**
38+
* @param string $size
39+
* @return string
40+
*/
41+
public function getThumbnail($size);
42+
43+
/**
44+
* @param bool $autoplay
45+
* @return string
46+
*/
47+
public function getEmbedUrl($autoplay = false);
48+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
/**
3+
* Created by PhpStorm.
4+
* User: Ricardo Fiorani
5+
* Date: 29/08/2015
6+
* Time: 14:57
7+
*/
8+
9+
namespace RicardoFiorani\Container\Factory;
10+
11+
12+
use RicardoFiorani\Adapter\VideoAdapterInterface;
13+
14+
interface CallableFactoryInterface
15+
{
16+
/**
17+
* @param string $url
18+
* @param string $patternMatched
19+
* @return VideoAdapterInterface
20+
*/
21+
public function __invoke($url, $patternMatched);
22+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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\Container\Factory;
10+
11+
12+
use RicardoFiorani\Container\VimeoVideoContainer;
13+
14+
class VimeoVideoContainerFactory implements CallableFactoryInterface
15+
{
16+
/**
17+
* @param string $url
18+
* @param string $pattern
19+
* @return VimeoVideoContainer
20+
*/
21+
public function __invoke($url, $pattern)
22+
{
23+
$match = array();
24+
$vimeoContainer = new VimeoVideoContainer($url);
25+
preg_match($pattern, $url, $match);
26+
/*Gets the Video ID*/
27+
$videoId = $match[2];
28+
if (empty($videoId)) {
29+
$videoId = $match[1];
30+
}
31+
$vimeoContainer->setVideoId($videoId);
32+
33+
/*Sends the video ID to the API to get the thumbnails and other infos*/
34+
$hash = unserialize(@file_get_contents("http://vimeo.com/api/v2/video/$videoId.php"));
35+
$data = $hash[0];
36+
37+
38+
$vimeoContainer->setThumbnails(array(
39+
VimeoVideoContainer::THUMBNAIL_SMALL => $data[VimeoVideoContainer::THUMBNAIL_SMALL],
40+
VimeoVideoContainer::THUMBNAIL_MEDIUM => $data[VimeoVideoContainer::THUMBNAIL_MEDIUM],
41+
VimeoVideoContainer::THUMBNAIL_LARGE => $data[VimeoVideoContainer::THUMBNAIL_LARGE],
42+
));
43+
44+
$vimeoContainer->setTitle($data['title']);
45+
$vimeoContainer->setDescription($data['description']);
46+
47+
return $vimeoContainer;
48+
}
49+
}

src/Container/VimeoVideoContainer.php

Lines changed: 173 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,173 @@
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\Container;
10+
11+
12+
use RicardoFiorani\Adapter\VideoAdapterInterface;
13+
14+
class VimeoVideoContainer implements VideoAdapterInterface
15+
{
16+
17+
const THUMBNAIL_SMALL = 'thumbnail_small';
18+
const THUMBNAIL_MEDIUM = 'thumbnail_medium';
19+
const THUMBNAIL_LARGE = 'thumbnail_large';
20+
21+
22+
/**
23+
* @var string
24+
*/
25+
public $rawUrl;
26+
27+
/**
28+
* @var string
29+
*/
30+
public $videoId;
31+
32+
/**
33+
* @var string
34+
*/
35+
public $title;
36+
37+
/**
38+
* @var string
39+
*/
40+
public $description;
41+
42+
/**
43+
* @var array
44+
*/
45+
public $thumbnails;
46+
47+
/**
48+
* @param string $rawUrl
49+
*/
50+
public function __construct($rawUrl)
51+
{
52+
$this->setRawUrl($rawUrl);
53+
}
54+
55+
/**
56+
* Returns the service name (ie: "Youtube" or "Vimeo")
57+
* @return string
58+
*/
59+
public function getServiceName()
60+
{
61+
return 'Vimeo';
62+
}
63+
64+
/**
65+
* Returns the input URL
66+
* @return string
67+
*/
68+
public function getRawUrl()
69+
{
70+
return $this->rawUrl;
71+
}
72+
73+
/**
74+
* Returns if the service has a thumbnail image
75+
* @return bool
76+
*/
77+
public function hasThumbnail()
78+
{
79+
return false == empty($this->thumbnails);
80+
}
81+
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+
}
106+
107+
/**
108+
* @return string
109+
*/
110+
public function getTitle()
111+
{
112+
return $this->title;
113+
}
114+
115+
/**
116+
* @param string $title
117+
*/
118+
public function setTitle($title)
119+
{
120+
$this->title = $title;
121+
}
122+
123+
/**
124+
* @return string
125+
*/
126+
public function getDescription()
127+
{
128+
return $this->description;
129+
}
130+
131+
/**
132+
* @param string $description
133+
*/
134+
public function setDescription($description)
135+
{
136+
$this->description = $description;
137+
}
138+
139+
/**
140+
* @return array
141+
*/
142+
public function getThumbnails()
143+
{
144+
return $this->thumbnails;
145+
}
146+
147+
/**
148+
* @param array $thumbnails
149+
*/
150+
public function setThumbnails($thumbnails)
151+
{
152+
$this->thumbnails = $thumbnails;
153+
}
154+
155+
156+
/**
157+
* @param string $size
158+
* @return string
159+
*/
160+
public function getThumbnail($size)
161+
{
162+
return $this->thumbnails($size);
163+
}
164+
165+
/**
166+
* @param bool $autoplay
167+
* @return string
168+
*/
169+
public function getEmbedUrl($autoplay = false)
170+
{
171+
return "http://player.vimeo.com/video/$videoId?byline=0&amp;portrait=0&amp" . ($autoplay ? '&amp&autoplay=1' : '');
172+
}
173+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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\Container;
10+
11+
12+
use RicardoFiorani\Adapter\VideoAdapterInterface;
13+
14+
class YoutubeVideoContainer implements VideoAdapterInterface
15+
{
16+
17+
18+
}

0 commit comments

Comments
 (0)