Skip to content

Commit

Permalink
Merge pull request #3 from felixgirault/feature-template
Browse files Browse the repository at this point in the history
Added templating feature
  • Loading branch information
felixgirault committed Nov 11, 2015
2 parents aebf4d7 + e25c0f3 commit 28f8d07
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 24 deletions.
27 changes: 23 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ Example
-------

```php
$Multiplayer = new Multiplayer\Multiplayer( );
$Multiplayer = new Multiplayer\Multiplayer();
$options = array(
'autoPlay' => true,
'foregroundColor' => 'BADA55'
);

echo $Multiplayer->html( 'http://www.dailymotion.com/video/xzn5qk', $options );
echo $Multiplayer->html( 'http://vimeo.com/47457051', $options );
echo $Multiplayer->html( 'http://www.youtube.com/watch?v=3qSMS4c5WAk', $options );
echo $Multiplayer->html('http://www.dailymotion.com/video/xzn5qk', $options);
echo $Multiplayer->html('http://vimeo.com/47457051', $options);
echo $Multiplayer->html('http://www.youtube.com/watch?v=3qSMS4c5WAk', $options);
```

This code would produce:
Expand All @@ -25,3 +25,22 @@ This code would produce:
<iframe src="http://player.vimeo.com/video/47457051?autoplay=1&color=BADA55" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
<iframe src="http://www.youtube-nocookie.com/embed/3qSMS4c5WAk?autoplay=1" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen></iframe>
```

Templating
----------

You can customize the generated HTML code by passing a templating function:

```php
echo $Multiplayer->html($url, $options, function($playerUrl) {
return '<iframe src="' . $playerUrl . '" class="video-player">'
});
```

A default one can also be set on instanciation:

```php
new Multiplayer\Multiplayer($services, function($playerUrl) {
return '<iframe src="' . $playerUrl . '" class="video-player">'
});
```
51 changes: 36 additions & 15 deletions lib/Multiplayer/Multiplayer.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,11 @@
class Multiplayer {

/**
* A HTML code to wrap a player URL.
* Template.
*
* @var string
* @var callable
*/
const wrapper = <<<HTML
<iframe
src="%s"
frameborder="0"
webkitAllowFullScreen
mozallowfullscreen
allowFullScreen
></iframe>
HTML;
protected $_template = 'Multiplayer::template';



Expand Down Expand Up @@ -113,9 +105,34 @@ class Multiplayer {
*
* @param array $services A set of services to be merged with the
* default ones.
* @param callable $template A function to generate the HTML code of a
* player from an URL.
*/
public function __construct(array $services = []) {
public function __construct(array $services = [], callable $template = null) {
$this->_services = array_merge($this->_services, $services);

if ($template) {
$this->_template = $template;
}
}



/**
* A HTML code to wrap a player URL.
*
* @var string
*/
public static function template($url) {
return <<<HTML
<iframe
src="$url"
frameborder="0"
webkitAllowFullScreen
mozallowfullscreen
allowFullScreen
></iframe>
HTML;
}


Expand All @@ -125,10 +142,11 @@ public function __construct(array $services = []) {
*
* @param string $source URL or HTML code.
* @param array $params Player configuration.
* @param string $wrapper HTML code surrounding the player URL.
* @param callable $template A function to generate the HTML code of a
* player from an URL.
* @return string Prepared HTML code.
*/
public function html($source, array $params = [], $wrapper = self::wrapper) {
public function html($source, array $params = [], callable $template = null) {
$params += $this->_params;
$id = null;

Expand All @@ -147,7 +165,10 @@ public function html($source, array $params = [], $wrapper = self::wrapper) {
$url .= '?' . http_build_query($params);
}

$source = sprintf($wrapper, $url);
$source = call_user_func(
$template ? $template : $this->_template,
$url
);
}

return $source;
Expand Down
51 changes: 46 additions & 5 deletions tests/Multiplayer/MultiplayerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,31 @@ class MultiplayerTest extends PHPUnit_Framework_TestCase {
*
*/
public function setUp() {
$this->Multiplayer = new Multiplayer($this->services);
$this->Multiplayer = new Multiplayer($this->services, function($url) {
return $url;
});
}



/**
*
*/
public function testTemplate() {
$expected = <<<HTML
<iframe
src="http://foo.bar"
frameborder="0"
webkitAllowFullScreen
mozallowfullscreen
allowFullScreen
></iframe>
HTML;

$this->assertEquals(
$expected,
$this->Multiplayer->template('http://foo.bar')
);
}


Expand All @@ -54,7 +78,7 @@ public function setUp() {
public function testHtml() {
$this->assertEquals(
'http://service.com/player/42',
$this->Multiplayer->html('service.com/video/42', [], '%s')
$this->Multiplayer->html('service.com/video/42', [])
);
}

Expand All @@ -66,7 +90,7 @@ public function testHtml() {
public function testHtmlWithParam() {
$this->assertEquals(
'http://service.com/player/42?foo=bar',
$this->Multiplayer->html('service.com/video/42', ['foo' => 'bar'], '%s')
$this->Multiplayer->html('service.com/video/42', ['foo' => 'bar'])
);
}

Expand All @@ -78,12 +102,29 @@ public function testHtmlWithParam() {
public function testHtmlWithMappedParam() {
$this->assertEquals(
'http://service.com/player/42?play=1',
$this->Multiplayer->html('service.com/video/42', ['autoPlay' => true], '%s')
$this->Multiplayer->html('service.com/video/42', ['autoPlay' => true])
);

$this->assertEquals(
'http://service.com/player/42?title=1&author=1',
$this->Multiplayer->html('service.com/video/42', ['showInfos' => true], '%s')
$this->Multiplayer->html('service.com/video/42', ['showInfos' => true])
);
}



/**
*
*/
public function testWithTemplate() {
$expected = '<iframe />';
$template = function($url) use ($expected) {
return $expected;
};

$this->assertEquals(
$expected,
$this->Multiplayer->html('service.com/video/42', [], $template)
);
}
}

0 comments on commit 28f8d07

Please sign in to comment.