Skip to content
This repository has been archived by the owner on Jun 11, 2020. It is now read-only.

Commit

Permalink
first commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jeanjar committed Aug 17, 2016
0 parents commit edee2f2
Show file tree
Hide file tree
Showing 6 changed files with 177 additions and 0 deletions.
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
vendor/
composer.lock
composer.phar
.idea/
22 changes: 22 additions & 0 deletions composer.json
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/"
}
}
}
36 changes: 36 additions & 0 deletions src/Providers/Vimeo.php
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>';
}
}
47 changes: 47 additions & 0 deletions src/Providers/Youtube.php
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>';
}

}
39 changes: 39 additions & 0 deletions src/VideoEmbed.php
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]);
}
}
}
}
29 changes: 29 additions & 0 deletions tests/Equals.php
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);
}
}

0 comments on commit edee2f2

Please sign in to comment.