Skip to content

Commit

Permalink
Adding the LanguageFilterPlugin
Browse files Browse the repository at this point in the history
  • Loading branch information
wtfzdotnet committed Feb 23, 2014
1 parent cf0e604 commit ae88a75
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
40 changes: 40 additions & 0 deletions lib/Tmdb/HttpClient/Plugin/LanguageFilterPlugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\HttpClient\Plugin;

use Guzzle\Common\Event;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;

class LanguageFilterPlugin implements EventSubscriberInterface
{
private $language;

public function __construct($language = 'en')
{
$this->language = $language;
}

public static function getSubscribedEvents()
{
return array('request.before_send' => 'onBeforeSend');
}

public function onBeforeSend(Event $event)
{
$url = $event['request']->getUrl(true);

$url->getQuery()->set('language', $this->language);

$event['request']->setUrl($url);
}
}
38 changes: 38 additions & 0 deletions test/Tmdb/Tests/HttpClient/Plugin/LanguageFilterTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
<?php
/**
* This file is part of the Tmdb PHP API created by Michael Roterman.
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*
* @package Tmdb
* @author Michael Roterman <[email protected]>
* @copyright (c) 2013, Michael Roterman
* @version 0.0.1
*/
namespace Tmdb\Tests\HttpClient\Plugin;

use Guzzle\Common\Event;
use Guzzle\Http\Message\Request;
use Tmdb\HttpClient\Plugin\LanguageFilterPlugin;
use Tmdb\Tests\TestCase;

class LanguageFilterTest extends TestCase
{
/**
* @test
*/
public function shouldAddToken()
{
$request = new Request('GET', '/');

$event = new Event();
$event['request'] = $request;

$plugin = new LanguageFilterPlugin();

$plugin->onBeforeSend($event);

$this->assertEquals('/?language=en', $event['request']->getUrl());
}
}

0 comments on commit ae88a75

Please sign in to comment.