This repository has been archived by the owner on Jun 11, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit edee2f2
Showing
6 changed files
with
177 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
vendor/ | ||
composer.lock | ||
composer.phar | ||
.idea/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
{ | ||
"name": "jeanjar/video_embed", | ||
"description": "A simple video embed for YouTube and Vimeo", | ||
"license": "MIT", | ||
"authors": [ | ||
{ | ||
"name": "Jean Jar Pereira de Araújo", | ||
"email": "jeanjpa@gmail.com" | ||
} | ||
], | ||
"require": { | ||
"php" : ">=5.4.0" | ||
}, | ||
"require-dev": { | ||
"phpunit/phpunit": "5.5.*" | ||
}, | ||
"autoload": { | ||
"psr-4": { | ||
"VideoEmbed\\": "src/" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
<?php | ||
|
||
namespace VideoEmbed\Providers; | ||
|
||
use VideoEmbed\VideoEmbed; | ||
|
||
class Vimeo | ||
{ | ||
public static function render($url, $params = []) | ||
{ | ||
$query = explode('/', $url); | ||
$id = end($query); | ||
$realParams = ''; | ||
|
||
if(VideoEmbed::f($params, 'return_id')) | ||
{ | ||
return $id; | ||
} | ||
|
||
if (!empty($params)) | ||
{ | ||
foreach ($params as $param => $value) | ||
{ | ||
if (is_int($param)) | ||
{ | ||
$realParams .= $value . ' '; | ||
} | ||
else | ||
{ | ||
$realParams .= $param . '="' . $value . '" '; | ||
} | ||
} | ||
} | ||
return '<iframe src="//player.vimeo.com/video/' . $id . '"' . $realParams . '></iframe>'; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
namespace VideoEmbed\Providers; | ||
|
||
use VideoEmbed\VideoEmbed; | ||
|
||
class Youtube | ||
{ | ||
public static function render($url, $params = array()) | ||
{ | ||
$parsed = parse_url($url); | ||
if (VideoEmbed::f($parsed, 'host') == 'youtu.be') | ||
{ | ||
$embed = str_replace('/', '', VideoEmbed::f($parsed, 'path')); | ||
} | ||
elseif (in_array(VideoEmbed::f($parsed, 'host'), array('youtube.com', 'www.youtube.com'))) | ||
{ | ||
$query = explode('&', $parsed['query']); | ||
$id = false; | ||
foreach ($query as $q) | ||
{ | ||
if (substr($q, 0, 2) == 'v=') | ||
{ | ||
$id = $q; | ||
break; | ||
} | ||
} | ||
if (!$id) | ||
{ | ||
return ''; | ||
} | ||
$embed = str_replace('v=', '', $id); | ||
} | ||
else | ||
{ | ||
return false; | ||
} | ||
|
||
if (VideoEmbed::f($params, 'return_id')) | ||
{ | ||
return $embed; | ||
} | ||
|
||
return '<iframe width="' . VideoEmbed::f($params, 'width') . '" height="' . VideoEmbed::f($params, 'height') . '" src="//www.youtube.com/embed/' . $embed . '" frameborder="0" allowfullscreen></iframe>'; | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
<?php | ||
|
||
namespace VideoEmbed; | ||
|
||
use VideoEmbed\Providers\Vimeo; | ||
use VideoEmbed\Providers\Youtube; | ||
|
||
class VideoEmbed | ||
{ | ||
public static function f($type, $variable_name, $filter = FILTER_DEFAULT, $options = null) | ||
{ | ||
if (is_array($type)) | ||
{ | ||
if (isset($type[$variable_name])) | ||
{ | ||
return filter_var($type[$variable_name], $filter, $options); | ||
} | ||
return null; | ||
} | ||
return filter_input($type, $variable_name, $filter, $options); | ||
} | ||
|
||
public static $list = [ | ||
'youtube' => Youtube::class, | ||
'youtu' => Youtube::class, | ||
'vimeo' => Vimeo::class, | ||
]; | ||
|
||
public static function render($url, $params = []) | ||
{ | ||
foreach (self::$list as $host => $class) | ||
{ | ||
if (preg_match('/' . $host . '/i', $url)) | ||
{ | ||
return call_user_func_array([$class, 'render'], [$url, $params]); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?php | ||
|
||
|
||
namespace VideoEmbed\Tests; | ||
|
||
use VideoEmbed\VideoEmbed; | ||
|
||
$loader = include dirname(__DIR__) . '/vendor/autoload.php'; | ||
|
||
class VimeoGetIdTeste extends \PHPUnit_Framework_TestCase | ||
{ | ||
public function testeGetIdEquals() | ||
{ | ||
$id = VideoEmbed::render('https://www.youtube.com/watch?v=bLLxXZoqq_Y', ['return_id' => true]); | ||
$this->assertEquals('bLLxXZoqq_Y', $id); | ||
|
||
$id = VideoEmbed::render('https://vimeo.com/18758609', ['return_id' => true]); | ||
$this->assertEquals('18758609', $id); | ||
} | ||
|
||
public function testeEmbedEquals() | ||
{ | ||
$video = VideoEmbed::render('https://www.youtube.com/watch?v=bLLxXZoqq_Y', ['width' => 300, 'height' => 250]); | ||
$this->assertEquals('<iframe width="300" height="250" src="//www.youtube.com/embed/bLLxXZoqq_Y" frameborder="0" allowfullscreen></iframe>', $video); | ||
|
||
$video = VideoEmbed::render('https://vimeo.com/18758609'); | ||
$this->assertEquals('<iframe src="//player.vimeo.com/video/18758609"></iframe>', $video); | ||
} | ||
} |