diff --git a/lib/Tmdb/HttpClient/Plugin/LanguageFilterPlugin.php b/lib/Tmdb/HttpClient/Plugin/LanguageFilterPlugin.php new file mode 100644 index 00000000..fd29835c --- /dev/null +++ b/lib/Tmdb/HttpClient/Plugin/LanguageFilterPlugin.php @@ -0,0 +1,40 @@ + + * @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); + } +} diff --git a/test/Tmdb/Tests/HttpClient/Plugin/LanguageFilterTest.php b/test/Tmdb/Tests/HttpClient/Plugin/LanguageFilterTest.php new file mode 100644 index 00000000..55b04f03 --- /dev/null +++ b/test/Tmdb/Tests/HttpClient/Plugin/LanguageFilterTest.php @@ -0,0 +1,38 @@ + + * @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()); + } +} \ No newline at end of file