Skip to content

Commit

Permalink
Merge pull request #7 from ricardofiorani/keep_querystring_from_video…
Browse files Browse the repository at this point in the history
…_url

Keep querystring from video url
  • Loading branch information
ricardofiorani authored Apr 16, 2018
2 parents c49de58 + c1f8f7d commit 415647d
Show file tree
Hide file tree
Showing 18 changed files with 294 additions and 200 deletions.
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Created by .ignore support plugin (hsz.mobi)
.idea
index.php
composer.lock
Dockerfile
vendor
9 changes: 7 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ sudo: false
# Setup build matrix
language: php
php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm
- 7.1
- 7.2

matrix:
include:
- php: 5.3
dist: precise

# Dependencies
before_install:
Expand Down
74 changes: 12 additions & 62 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,78 +66,28 @@ echo $video->getLargestThumbnail();
## Registering your own service video (it's easy !)
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

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

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

## Using your own framework's template engine
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.
A Fully functional example can be found [Here](https://github.com/ricardofiorani/php-video-url-parser/tree/master/documentation/IntegratingYourOwnRenderer.md).

Here's an example:
### My Example Renderer Class
```php
namespace MyVendor\MyRenderer;


class MyOwnRenderer implements \RicardoFiorani\Renderer\EmbedRendererInterface
{

/**
* @param string $embedUrl
* @param integer $height
* @param integer $width
* @return string
*/
public function render($embedUrl, $height, $width)
{
//Just for example porpoises
return "Hell yeah baby, you've rendered: ".addslashes($embedUrl);

//A functional example would be like
//return '<iframe width="' . $width . '" height="' . $height . '" src="' . addslashes($embedUrl) . '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
}
}
```
### My Example Renderer Factory Class
```php
namespace MyVendor\MyRenderer\Factory;

class MyOwnRendererFactory implements RendererFactoryInterface
{
/**
* @return EmbedRendererInterface
*/
public function __invoke()
{
return new MyOwnRenderer();
}
}
```
### Registering my renderer

```php
<?php
use RicardoFiorani\Matcher\VideoServiceMatcher;

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

$vsm = new VideoServiceMatcher();

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

$video = $vsm->parse('https://www.youtube.com/watch?v=PkOcm_XaWrw');

//This will output "Hell yeah baby, you've rendered: http://www.youtube.com/embed/PkOcm_XaWrw"
echo $video->getEmbedCode(500,500);

```

### Currently Suported Services
* Youtube
* Vimeo
* Dailymotion
* Facebook Videos


### Currently Supported PHP Versions
* PHP 5.3
* PHP 5.4
* PHP 5.5
* PHP 5.6
* PHP 7.0
* PHP 7.1
* PHP 7.2

> Please note that even this lib is not passing tests on HHVM, we can't guarantee. Please use it on your own risk.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "ricardofiorani/php-video-url-parser",
"description": "PHP Video URL Parser is a simple video url parser.",
"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",
"require-dev": {
"phpunit/phpunit": "^4.8",
"fabpot/php-cs-fixer" : ">= 1.0"
Expand Down
77 changes: 77 additions & 0 deletions documentation/IntegratingYourOwnRenderer.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# Functional Example of Integrating your own renderer
In this project I've used a simple renderer (which just does an echo of an iframe) but you can use your own implementation.
First, It must follow the RicardoFiorani\Renderer\EmbedRendererInterface interface.

Basically you need two classes:

* The Renderer Implementation
* The Renderer's Factory

The examples can be seem below:

### My Renderer Implementation Class
This is the concrete implementation on how your renderer is going to handle the embed URL to give you an embed code.
In here you can inject any dependency you might need by the constructor and add any logic you need.
Please note that it should implement the interface "\RicardoFiorani\Renderer\EmbedRendererInterface".
```php
<?php
namespace MyVendor\MyRenderer;
use \RicardoFiorani\Renderer\EmbedRendererInterface;

class MyOwnRenderer implements EmbedRendererInterface
{
/**
* @param string $embedUrl
* @param integer $height
* @param integer $width
* @return string
*/
public function renderVideoEmbedCode($embedUrl, $height, $width)
{
//Just for example porpoises
return sprintf("Hello, I'm embedding %s", addslashes($embedUrl));

//A functional example would be like
//return '<iframe width="' . $width . '" height="' . $height . '" src="' . addslashes($embedUrl) . '" frameborder="0" webkitAllowFullScreen mozallowfullscreen allowFullScreen></iframe>';
}
}
```
### My Renderer Implementation Factory Class
This is the Factory of your renderer, basically all it must do is to implement the interface RicardoFiorani\Renderer\Factory\RendererFactoryInterface
```php
<?php
namespace MyVendor\MyRenderer\Factory;
use RicardoFiorani\Renderer\EmbedRendererInterface;
use RicardoFiorani\Renderer\Factory\RendererFactoryInterface;

class MyOwnRendererFactory implements RendererFactoryInterface
{
/**
* @return EmbedRendererInterface
*/
public function __invoke()
{
return new MyOwnRenderer();
}
}
```
### Registering my renderer

The last part is attaching your own renderer service to the VideoServiceMatcher, which can be done as the example that follows:

```php
<?php
use RicardoFiorani\Matcher\VideoServiceMatcher;

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

$vsm = new VideoServiceMatcher();

//This is where you attach your own renderer to be used instead of the default one
$vsm->getServiceContainer()->setRenderer('MyOwnRenderer', MyVendor\MyRenderer\Factory\MyOwnRendererFactory::class);

$video = $vsm->parse('https://www.youtube.com/watch?v=PkOcm_XaWrw');

//This will output "Hello, I'm embedding http://www.youtube.com/embed/PkOcm_XaWrw"
echo $video->getEmbedCode(500,500);
```
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,14 @@
<?php
namespace MyVendor\ServiceAdapter;

use RicardoFiorani\Adapter\AbstractServiceAdapter;
use RicardoFiorani\Renderer\EmbedRendererInterface;

//Your service Adapter must implement VideoAdapterInterface or Extend AbstractServiceAdapter
class DailymotionServiceAdapter extends AbstractServiceAdapter
{
const THUMBNAIL_DEFAULT = 'thumbnail';

/**
* AbstractVideoAdapter constructor.
*
* @param string $url
* @param string $pattern
* @param EmbedRendererInterface $renderer
Expand All @@ -27,9 +25,9 @@ class DailymotionServiceAdapter extends AbstractServiceAdapter
return parent::__construct($url, $pattern, $renderer);
}


/**
* Returns the service name (ie: "Youtube" or "Vimeo")
* Returns the service name (ie: "Youtube" or "Vimeo").
*
* @return string
*/
public function getServiceName()
Expand All @@ -38,7 +36,8 @@ class DailymotionServiceAdapter extends AbstractServiceAdapter
}

/**
* Returns if the service has a thumbnail image
* Returns if the service has a thumbnail image.
*
* @return bool
*/
public function hasThumbnail()
Expand All @@ -47,7 +46,8 @@ class DailymotionServiceAdapter extends AbstractServiceAdapter
}

/**
* Returns all thumbnails available sizes
* Returns all thumbnails available sizes.
*
* @return array
*/
public function getThumbNailSizes()
Expand All @@ -59,64 +59,79 @@ class DailymotionServiceAdapter extends AbstractServiceAdapter

/**
* @param string $size
* @param bool $forceSecure
*
* @return string
* @throws InvalidThumbnailSizeException
*/
public function getThumbnail($size)
public function getThumbnail($size, $forceSecure = false)
{
if (false == in_array($size, $this->getThumbNailSizes())) {
throw new \RicardoFiorani\Exception\InvalidThumbnailSizeException;
throw new InvalidThumbnailSizeException();
}

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

/**
* Returns the small thumbnail's url
* Returns the small thumbnail's url.
*
* @param bool $forceSecure
* @return string
* @throws InvalidThumbnailSizeException
*/
public function getSmallThumbnail()
public function getSmallThumbnail($forceSecure = false)
{
//Since this service does not provide other thumbnails sizes we just return the default size
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure);
}

/**
* Returns the medium thumbnail's url
* Returns the medium thumbnail's url.
*
* @param bool $forceSecure
* @return string
* @throws InvalidThumbnailSizeException
*/
public function getMediumThumbnail()
public function getMediumThumbnail($forceSecure = false)
{
//Since this service does not provide other thumbnails sizes we just return the default size
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure);
}

/**
* Returns the large thumbnail's url
* Returns the large thumbnail's url.
*
* @param bool $forceSecure
* @return string
* @throws InvalidThumbnailSizeException
*/
public function getLargeThumbnail()
public function getLargeThumbnail($forceSecure = false)
{
//Since this service does not provide other thumbnails sizes we just return the default size
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure);
}

/**
* Returns the largest thumnbnaail's url
* Returns the largest thumnbnaail's url.
* @param bool $forceSecure
* @return string
* @throws InvalidThumbnailSizeException
*/
public function getLargestThumbnail()
public function getLargestThumbnail($forceSecure = false)
{
//Since this service does not provide other thumbnails sizes we just return the default size
return $this->getThumbnail(self::THUMBNAIL_DEFAULT);
return $this->getThumbnail(self::THUMBNAIL_DEFAULT, $forceSecure);
}

/**
* @param bool $autoplay
* @param bool $forceAutoplay
* @param bool $forceSecure
* @return string
*/
public function getEmbedUrl($autoplay = false)
public function getEmbedUrl($forceAutoplay = false, $forceSecure = false)
{
return '//www.dailymotion.com/embed/video/' . $this->videoId . ($autoplay ? '?amp&autoplay=1' : '');
return $this->getScheme($forceSecure) . '://www.dailymotion.com/embed/video/' . $this->videoId . ($forceAutoplay ? '?amp&autoplay=1' : '');
}

/**
Expand All @@ -133,7 +148,6 @@ class DailymotionServiceAdapter extends AbstractServiceAdapter
<?php
namespace MyVendor\ServiceAdapter\Factory;


use MyVendor\ServiceAdapter\DailymotionServiceAdapter;
use RicardoFiorani\Adapter\VideoAdapterInterface;
use RicardoFiorani\Renderer\EmbedRendererInterface;
Expand Down Expand Up @@ -174,7 +188,7 @@ $patterns = array(
);

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

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

0 comments on commit 415647d

Please sign in to comment.