Skip to content

Commit 415647d

Browse files
Merge pull request #7 from ricardofiorani/keep_querystring_from_video_url
Keep querystring from video url
2 parents c49de58 + c1f8f7d commit 415647d

18 files changed

+294
-200
lines changed

.gitignore

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
.idea
3+
index.php
4+
composer.lock
5+
Dockerfile
6+
vendor

.travis.yml

+7-2
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,17 @@ sudo: false
44
# Setup build matrix
55
language: php
66
php:
7-
- 5.3
87
- 5.4
98
- 5.5
109
- 5.6
1110
- 7.0
12-
- hhvm
11+
- 7.1
12+
- 7.2
13+
14+
matrix:
15+
include:
16+
- php: 5.3
17+
dist: precise
1318

1419
# Dependencies
1520
before_install:

README.md

+12-62
Original file line numberDiff line numberDiff line change
@@ -66,78 +66,28 @@ echo $video->getLargestThumbnail();
6666
## Registering your own service video (it's easy !)
6767
If you want to register an implementation of some service your class just needs to implement the "RicardoFiorani\Adapter\VideoAdapterInterface" or extend the RicardoFiorani\Adapter\AbstractServiceAdapter
6868

69-
A Fully functional example can be found [Here](https://github.com/ricardofiorani/php-video-url-parser/tree/master/example/RegisteringANewService.md).
69+
A Fully functional example can be found [Here](https://github.com/ricardofiorani/php-video-url-parser/tree/master/documentation/RegisteringANewService.md).
7070

7171
PS: If you've made your awesome implementation of some well known service, feel free to send a Pull Request. All contributions are welcome :)
7272

7373
## Using your own framework's template engine
74-
In this project I've used a simple renderer (which just does an echo of an iframe) but you can use your own implementation. It must follow the RicardoFiorani\Renderer\EmbedRendererInterface and just like that.
74+
A Fully functional example can be found [Here](https://github.com/ricardofiorani/php-video-url-parser/tree/master/documentation/IntegratingYourOwnRenderer.md).
7575

76-
Here's an example:
77-
### My Example Renderer Class
78-
```php
79-
namespace MyVendor\MyRenderer;
80-
81-
82-
class MyOwnRenderer implements \RicardoFiorani\Renderer\EmbedRendererInterface
83-
{
84-
85-
/**
86-
* @param string $embedUrl
87-
* @param integer $height
88-
* @param integer $width
89-
* @return string
90-
*/
91-
public function render($embedUrl, $height, $width)
92-
{
93-
//Just for example porpoises
94-
return "Hell yeah baby, you've rendered: ".addslashes($embedUrl);
95-
96-
//A functional example would be like
97-
//return '<iframe width="' . $width . '" height="' . $height . '" src="' . addslashes($embedUrl) . '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
98-
}
99-
}
100-
```
101-
### My Example Renderer Factory Class
102-
```php
103-
namespace MyVendor\MyRenderer\Factory;
104-
105-
class MyOwnRendererFactory implements RendererFactoryInterface
106-
{
107-
/**
108-
* @return EmbedRendererInterface
109-
*/
110-
public function __invoke()
111-
{
112-
return new MyOwnRenderer();
113-
}
114-
}
115-
```
116-
### Registering my renderer
117-
118-
```php
119-
<?php
120-
use RicardoFiorani\Matcher\VideoServiceMatcher;
121-
122-
require __DIR__ . '/vendor/autoload.php';
123-
124-
$vsm = new VideoServiceMatcher();
125-
126-
//This is where the magic is done
127-
$vsm->getServiceContainer()->setRenderer('MyOwnRenderer', 'MyVendor\\MyRenderer\\Factory\\MyOwnRendererFactory');
128-
129-
$video = $vsm->parse('https://www.youtube.com/watch?v=PkOcm_XaWrw');
130-
131-
//This will output "Hell yeah baby, you've rendered: http://www.youtube.com/embed/PkOcm_XaWrw"
132-
echo $video->getEmbedCode(500,500);
133-
134-
```
13576

13677
### Currently Suported Services
13778
* Youtube
13879
* Vimeo
13980
* Dailymotion
14081
* Facebook Videos
14182

142-
83+
### Currently Supported PHP Versions
84+
* PHP 5.3
85+
* PHP 5.4
86+
* PHP 5.5
87+
* PHP 5.6
88+
* PHP 7.0
89+
* PHP 7.1
90+
* PHP 7.2
91+
92+
> Please note that even this lib is not passing tests on HHVM, we can't guarantee. Please use it on your own risk.
14393

composer.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "ricardofiorani/php-video-url-parser",
3-
"description": "PHP Video URL Parser is a simple video url parser.",
3+
"description": "A Simple and efficient PHP Video URL Parser that gives you thumbnails and embed codes for various services as Youtube, Vimeo, DailyMotion and Facebook",
44
"require-dev": {
55
"phpunit/phpunit": "^4.8",
66
"fabpot/php-cs-fixer" : ">= 1.0"
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
# Functional Example of Integrating your own renderer
2+
In this project I've used a simple renderer (which just does an echo of an iframe) but you can use your own implementation.
3+
First, It must follow the RicardoFiorani\Renderer\EmbedRendererInterface interface.
4+
5+
Basically you need two classes:
6+
7+
* The Renderer Implementation
8+
* The Renderer's Factory
9+
10+
The examples can be seem below:
11+
12+
### My Renderer Implementation Class
13+
This is the concrete implementation on how your renderer is going to handle the embed URL to give you an embed code.
14+
In here you can inject any dependency you might need by the constructor and add any logic you need.
15+
Please note that it should implement the interface "\RicardoFiorani\Renderer\EmbedRendererInterface".
16+
```php
17+
<?php
18+
namespace MyVendor\MyRenderer;
19+
use \RicardoFiorani\Renderer\EmbedRendererInterface;
20+
21+
class MyOwnRenderer implements EmbedRendererInterface
22+
{
23+
/**
24+
* @param string $embedUrl
25+
* @param integer $height
26+
* @param integer $width
27+
* @return string
28+
*/
29+
public function renderVideoEmbedCode($embedUrl, $height, $width)
30+
{
31+
//Just for example porpoises
32+
return sprintf("Hello, I'm embedding %s", addslashes($embedUrl));
33+
34+
//A functional example would be like
35+
//return '<iframe width="' . $width . '" height="' . $height . '" src="' . addslashes($embedUrl) . '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
36+
}
37+
}
38+
```
39+
### My Renderer Implementation Factory Class
40+
This is the Factory of your renderer, basically all it must do is to implement the interface RicardoFiorani\Renderer\Factory\RendererFactoryInterface
41+
```php
42+
<?php
43+
namespace MyVendor\MyRenderer\Factory;
44+
use RicardoFiorani\Renderer\EmbedRendererInterface;
45+
use RicardoFiorani\Renderer\Factory\RendererFactoryInterface;
46+
47+
class MyOwnRendererFactory implements RendererFactoryInterface
48+
{
49+
/**
50+
* @return EmbedRendererInterface
51+
*/
52+
public function __invoke()
53+
{
54+
return new MyOwnRenderer();
55+
}
56+
}
57+
```
58+
### Registering my renderer
59+
60+
The last part is attaching your own renderer service to the VideoServiceMatcher, which can be done as the example that follows:
61+
62+
```php
63+
<?php
64+
use RicardoFiorani\Matcher\VideoServiceMatcher;
65+
66+
require __DIR__ . '/vendor/autoload.php';
67+
68+
$vsm = new VideoServiceMatcher();
69+
70+
//This is where you attach your own renderer to be used instead of the default one
71+
$vsm->getServiceContainer()->setRenderer('MyOwnRenderer', MyVendor\MyRenderer\Factory\MyOwnRendererFactory::class);
72+
73+
$video = $vsm->parse('https://www.youtube.com/watch?v=PkOcm_XaWrw');
74+
75+
//This will output "Hello, I'm embedding http://www.youtube.com/embed/PkOcm_XaWrw"
76+
echo $video->getEmbedCode(500,500);
77+
```

example/RegisteringANewService.md documentation/RegisteringANewService.md

+41-27
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,14 @@
55
<?php
66
namespace MyVendor\ServiceAdapter;
77

8-
use RicardoFiorani\Adapter\AbstractServiceAdapter;
9-
use RicardoFiorani\Renderer\EmbedRendererInterface;
10-
118
//Your service Adapter must implement VideoAdapterInterface or Extend AbstractServiceAdapter
129
class DailymotionServiceAdapter extends AbstractServiceAdapter
1310
{
1411
const THUMBNAIL_DEFAULT = 'thumbnail';
1512

1613
/**
1714
* AbstractVideoAdapter constructor.
15+
*
1816
* @param string $url
1917
* @param string $pattern
2018
* @param EmbedRendererInterface $renderer
@@ -27,9 +25,9 @@ class DailymotionServiceAdapter extends AbstractServiceAdapter
2725
return parent::__construct($url, $pattern, $renderer);
2826
}
2927

30-
3128
/**
32-
* Returns the service name (ie: "Youtube" or "Vimeo")
29+
* Returns the service name (ie: "Youtube" or "Vimeo").
30+
*
3331
* @return string
3432
*/
3533
public function getServiceName()
@@ -38,7 +36,8 @@ class DailymotionServiceAdapter extends AbstractServiceAdapter
3836
}
3937

4038
/**
41-
* Returns if the service has a thumbnail image
39+
* Returns if the service has a thumbnail image.
40+
*
4241
* @return bool
4342
*/
4443
public function hasThumbnail()
@@ -47,7 +46,8 @@ class DailymotionServiceAdapter extends AbstractServiceAdapter
4746
}
4847

4948
/**
50-
* Returns all thumbnails available sizes
49+
* Returns all thumbnails available sizes.
50+
*
5151
* @return array
5252
*/
5353
public function getThumbNailSizes()
@@ -59,64 +59,79 @@ class DailymotionServiceAdapter extends AbstractServiceAdapter
5959

6060
/**
6161
* @param string $size
62+
* @param bool $forceSecure
63+
*
6264
* @return string
65+
* @throws InvalidThumbnailSizeException
6366
*/
64-
public function getThumbnail($size)
67+
public function getThumbnail($size, $forceSecure = false)
6568
{
6669
if (false == in_array($size, $this->getThumbNailSizes())) {
67-
throw new \RicardoFiorani\Exception\InvalidThumbnailSizeException;
70+
throw new InvalidThumbnailSizeException();
6871
}
6972

70-
return 'http://www.dailymotion.com/' . $size . '/video/' . $this->videoId;
73+
return $this->getScheme($forceSecure) . '://www.dailymotion.com/' . $size . '/video/' . $this->videoId;
7174
}
7275

7376
/**
74-
* Returns the small thumbnail's url
77+
* Returns the small thumbnail's url.
78+
*
79+
* @param bool $forceSecure
7580
* @return string
81+
* @throws InvalidThumbnailSizeException
7682
*/
77-
public function getSmallThumbnail()
83+
public function getSmallThumbnail($forceSecure = false)
7884
{
7985
//Since this service does not provide other thumbnails sizes we just return the default size
80-
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
86+
return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure);
8187
}
8288

8389
/**
84-
* Returns the medium thumbnail's url
90+
* Returns the medium thumbnail's url.
91+
*
92+
* @param bool $forceSecure
8593
* @return string
94+
* @throws InvalidThumbnailSizeException
8695
*/
87-
public function getMediumThumbnail()
96+
public function getMediumThumbnail($forceSecure = false)
8897
{
8998
//Since this service does not provide other thumbnails sizes we just return the default size
90-
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
99+
return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure);
91100
}
92101

93102
/**
94-
* Returns the large thumbnail's url
103+
* Returns the large thumbnail's url.
104+
*
105+
* @param bool $forceSecure
95106
* @return string
107+
* @throws InvalidThumbnailSizeException
96108
*/
97-
public function getLargeThumbnail()
109+
public function getLargeThumbnail($forceSecure = false)
98110
{
99111
//Since this service does not provide other thumbnails sizes we just return the default size
100-
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
112+
return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure);
101113
}
102114

103115
/**
104-
* Returns the largest thumnbnaail's url
116+
* Returns the largest thumnbnaail's url.
117+
* @param bool $forceSecure
105118
* @return string
119+
* @throws InvalidThumbnailSizeException
106120
*/
107-
public function getLargestThumbnail()
121+
public function getLargestThumbnail($forceSecure = false)
108122
{
109123
//Since this service does not provide other thumbnails sizes we just return the default size
110-
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
124+
return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure);
111125
}
112126

113127
/**
114-
* @param bool $autoplay
128+
* @param bool $forceAutoplay
129+
* @param bool $forceSecure
115130
* @return string
116131
*/
117-
public function getEmbedUrl($autoplay = false)
132+
public function getEmbedUrl($forceAutoplay = false, $forceSecure = false)
118133
{
119-
return '//www.dailymotion.com/embed/video/' . $this->videoId . ($autoplay ? '?amp&autoplay=1' : '');
134+
return $this->getScheme($forceSecure) . '://www.dailymotion.com/embed/video/' . $this->videoId . ($forceAutoplay ? '?amp&autoplay=1' : '');
120135
}
121136

122137
/**
@@ -133,7 +148,6 @@ class DailymotionServiceAdapter extends AbstractServiceAdapter
133148
<?php
134149
namespace MyVendor\ServiceAdapter\Factory;
135150

136-
137151
use MyVendor\ServiceAdapter\DailymotionServiceAdapter;
138152
use RicardoFiorani\Adapter\VideoAdapterInterface;
139153
use RicardoFiorani\Renderer\EmbedRendererInterface;
@@ -174,7 +188,7 @@ $patterns = array(
174188
);
175189

176190
//Register the new service
177-
$vsd->getServiceContainer()->registerService($serviceName, $patterns, "\\MyVendor\\ServiceAdapter\\Factory\\DailymotionServiceAdapterFactory");
191+
$vsd->getServiceContainer()->registerService($serviceName, $patterns, MyVendor\ServiceAdapter\Factory\DailymotionServiceAdapterFactory::class);
178192

179193
//This will get you an DailymotionServiceAdapter
180194
$video = $vsd->parse('http://www.dailymotion.com/video/x33ncwc_kittens-fight-in-tiny-boxing-ring_animals');

0 commit comments

Comments
 (0)