Skip to content

Commit

Permalink
add LocalizationHandler
Browse files Browse the repository at this point in the history
and remove the evil LocalizedEndpointProxy
  • Loading branch information
darthmaim committed May 23, 2015
1 parent a5ad2c7 commit 125d46c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 112 deletions.
1 change: 1 addition & 0 deletions src/GW2Api.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ function __construct( array $options = [] ) {
$this->options = $options;

$this->registerHandler( '\GW2Treasures\GW2Api\V2\Authentication\AuthenticationHandler' );
$this->registerHandler( '\GW2Treasures\GW2Api\V2\Localization\LocalizationHandler' );
}

protected function getOptions( array $options = [] ) {
Expand Down
2 changes: 2 additions & 0 deletions src/V2/EndpointTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,6 @@ protected abstract function request( array $query = [], $url = null, $method = '
* @return ApiResponse[]
*/
public abstract function requestMany( array $queries = [], $url = null, $method = 'GET', $options = [] );

public abstract function attach( ApiHandler $handler );
}
15 changes: 13 additions & 2 deletions src/V2/Localization/ILocalizedEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,21 @@

namespace GW2Treasures\GW2Api\V2\Localization;

interface ILocalizedEndpoint {
use GW2Treasures\GW2Api\V2\IEndpoint;

interface ILocalizedEndpoint extends IEndpoint {
/**
* Change the language of this endpoint.
*
* @param string $language
* @return $this
*/
function lang( $language );
public function lang( $language );

/**
* Get the current language
*
* @return string
*/
public function getLang();
}
26 changes: 26 additions & 0 deletions src/V2/Localization/LocalizationHandler.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php

namespace GW2Treasures\GW2Api\V2\Localization;

use GuzzleHttp\Message\RequestInterface;
use GW2Treasures\GW2Api\V2\ApiHandler;

class LocalizationHandler extends ApiHandler {
function __construct( ILocalizedEndpoint $endpoint ) {
$this->endpoint = $endpoint;
}

/**
* @return ILocalizedEndpoint
*/
protected function getEndpoint() {
return parent::getEndpoint();
}

/**
* @param RequestInterface $request
*/
public function onRequest( RequestInterface $request ) {
$request->getQuery()->add( 'lang', $this->getEndpoint()->getLang() );
}
}
15 changes: 12 additions & 3 deletions src/V2/Localization/LocalizedEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,22 @@ trait LocalizedEndpoint {
protected $language = 'en';

/**
* Get a localized version of this endpoint.
* Change the language of this endpoint.
*
* @param string $lang
* @return $this
*/
public function lang( $lang ) {
/** @noinspection PhpParamsInspection */
return new LocalizedEndpointProxy( $this, $lang );
$this->language = $lang;
return $this;
}

/**
* Get the current language
*
* @return string
*/
public function getLang() {
return $this->language;
}
}
83 changes: 0 additions & 83 deletions src/V2/Localization/LocalizedEndpointProxy.php

This file was deleted.

24 changes: 0 additions & 24 deletions tests/LocalizedEndpointTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,28 +46,4 @@ public function testNested() {
$this->assertEquals( 'fr', $request->getQuery()->get('lang'),
'LocalizedEndpoint sets correct query parameter value on nested request' );
}

public function testPersistence() {
$this->mockResponse('[]');
$this->mockResponse('[]');

$en = $this->getLocalizedEndpoint()->lang('en');
$de = $this->getLocalizedEndpoint()->lang('de');

// send first request
$en->get();
$request = $this->getLastRequest();
$this->assertTrue( $request->getQuery()->hasKey('lang'),
'LocalizedEndpoint sets ?lang query parameter on first request after creating multiple localized endpoints' );
$this->assertEquals( 'en', $request->getQuery()->get('lang'),
'LocalizedEndpoint sets correct query parameter value on first request after creating multiple localized endpoints' );

// send second request
$de->get();
$request = $this->getLastRequest();
$this->assertTrue( $request->getQuery()->hasKey('lang'),
'LocalizedEndpoint sets ?lang query parameter on last request after creating multiple localized endpoints and sending the first' );
$this->assertEquals( 'de', $request->getQuery()->get('lang'),
'LocalizedEndpoint sets correct query parameter value on last request after creating multiple localized endpoints and sending the first' );
}
}

0 comments on commit 125d46c

Please sign in to comment.