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
1 parent
6309d29
commit 9631039
Showing
2 changed files
with
169 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,82 @@ | ||
<?php | ||
|
||
/** | ||
* @author Félix Girault <felix.girault@gmail.com> | ||
* @license FreeBSD License (http://opensource.org/licenses/BSD-2-Clause) | ||
*/ | ||
|
||
namespace Essence\Provider\OEmbed; | ||
|
||
use Essence\Media; | ||
use Essence\Provider\OEmbed; | ||
|
||
|
||
|
||
/** | ||
* | ||
* @package Essence.Provider.OEmbed | ||
*/ | ||
|
||
class Youtube extends OEmbed { | ||
|
||
/** | ||
* Refactors URLs like these: | ||
* - http://www.youtube.com/watch?v=oHg5SJYRHA0&noise=noise | ||
* - http://www.youtube.com/v/oHg5SJYRHA0 | ||
* - http://www.youtube.com/embed/oHg5SJYRHA0 | ||
* - http://youtu.be/oHg5SJYRHA0 | ||
* | ||
* in such form: | ||
* - http://www.youtube.com/watch?v=oHg5SJYRHA0 | ||
* | ||
* @param string $url Url to prepare. | ||
* @return string Prepared url. | ||
*/ | ||
|
||
public static function prepareUrl( $url, array $options = [ ]) { | ||
|
||
$url = trim( $url ); | ||
|
||
if ( preg_match( '#(?:v=|v/|embed/|youtu\.be/)(?<id>[a-z0-9_-]+)#i', $url, $matches )) { | ||
$url = 'http://www.youtube.com/watch?v=' . $matches['id']; | ||
} | ||
|
||
return $url; | ||
} | ||
|
||
|
||
|
||
/** | ||
* | ||
* | ||
* @param Essence\Media $Media A reference to the Media. | ||
* @param array $options Embed options. | ||
* - 'thumbnailFormat' string | ||
*/ | ||
|
||
public static function completeMedia( Media $Media, array $options = [ ]) { | ||
|
||
if ( isset( $options['thumbnailFormat'])) { | ||
$url = $Media->get( 'thumbnailUrl' ); | ||
|
||
switch ( $options['thumbnailFormat']) { | ||
case 'small': | ||
$url = str_replace( 'hqdefault', 'default', $url ); | ||
break; | ||
|
||
case 'medium': | ||
$url = str_replace( 'hqdefault', 'mqdefault', $url ); | ||
break; | ||
|
||
case 'large': | ||
default: | ||
// unchanged | ||
break; | ||
} | ||
|
||
$Media->set( 'thumbnailUrl', $url ); | ||
} | ||
|
||
return parent::completeMedia( $Media, $options ); | ||
} | ||
} |
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,87 @@ | ||
<?php | ||
|
||
/** | ||
* @author Félix Girault <felix.girault@gmail.com> | ||
* @license FreeBSD License (http://opensource.org/licenses/BSD-2-Clause) | ||
*/ | ||
|
||
namespace Essence\Provider\OEmbed; | ||
|
||
use PHPUnit_Framework_TestCase; | ||
use Essence\Media; | ||
|
||
|
||
|
||
/** | ||
* Test case for Youtube. | ||
*/ | ||
|
||
class YoutubeTest extends PHPUnit_Framework_TestCase { | ||
|
||
/** | ||
* | ||
*/ | ||
|
||
public function testPrepareUrl( ) { | ||
|
||
$this->assertEquals( | ||
'http://www.youtube.com/watch?v=oHg5SJYRHA0', | ||
Youtube::prepareUrl( 'http://www.youtube.com/watch?v=oHg5SJYRHA0&noise=noise' ) | ||
); | ||
|
||
$this->assertEquals( | ||
'http://www.youtube.com/watch?v=oHg5SJYRHA0', | ||
Youtube::prepareUrl( 'http://www.youtube.com/v/oHg5SJYRHA0' ) | ||
); | ||
|
||
$this->assertEquals( | ||
'http://www.youtube.com/watch?v=oHg5SJYRHA0', | ||
Youtube::prepareUrl( 'http://www.youtube.com/embed/oHg5SJYRHA0' ) | ||
); | ||
|
||
$this->assertEquals( | ||
'http://www.youtube.com/watch?v=oHg5SJYRHA0', | ||
Youtube::prepareUrl( 'http://youtu.be/oHg5SJYRHA0' ) | ||
); | ||
} | ||
|
||
|
||
|
||
/** | ||
* | ||
*/ | ||
|
||
public function testCompleteMediaWithSmallThumbnailUrl( ) { | ||
|
||
$Media = new Media([ | ||
'thumbnailUrl' => 'http://i1.ytimg.com/vi/r0dBPI4etvI/hqdefault.jpg' | ||
]); | ||
|
||
Youtube::completeMedia( $Media, [ 'thumbnailFormat' => 'small' ]); | ||
|
||
$this->assertEquals( | ||
'http://i1.ytimg.com/vi/r0dBPI4etvI/default.jpg', | ||
$Media->get( 'thumbnailUrl' ) | ||
); | ||
} | ||
|
||
|
||
|
||
/** | ||
* | ||
*/ | ||
|
||
public function testCompleteMediaWithMediumThumbnailUrl( ) { | ||
|
||
$Media = new Media([ | ||
'thumbnailUrl' => 'http://i1.ytimg.com/vi/r0dBPI4etvI/hqdefault.jpg' | ||
]); | ||
|
||
Youtube::completeMedia( $Media, [ 'thumbnailFormat' => 'medium' ]); | ||
|
||
$this->assertEquals( | ||
'http://i1.ytimg.com/vi/r0dBPI4etvI/mqdefault.jpg', | ||
$Media->get( 'thumbnailUrl' ) | ||
); | ||
} | ||
} |