diff --git a/src/GW2Api.php b/src/GW2Api.php index 7d062f6..82d61b4 100644 --- a/src/GW2Api.php +++ b/src/GW2Api.php @@ -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 = [] ) { diff --git a/src/V2/EndpointTrait.php b/src/V2/EndpointTrait.php index e92836b..3ee3844 100644 --- a/src/V2/EndpointTrait.php +++ b/src/V2/EndpointTrait.php @@ -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 ); } diff --git a/src/V2/Localization/ILocalizedEndpoint.php b/src/V2/Localization/ILocalizedEndpoint.php index 314b13c..818c593 100644 --- a/src/V2/Localization/ILocalizedEndpoint.php +++ b/src/V2/Localization/ILocalizedEndpoint.php @@ -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(); } diff --git a/src/V2/Localization/LocalizationHandler.php b/src/V2/Localization/LocalizationHandler.php new file mode 100644 index 0000000..caf1088 --- /dev/null +++ b/src/V2/Localization/LocalizationHandler.php @@ -0,0 +1,26 @@ +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() ); + } +} diff --git a/src/V2/Localization/LocalizedEndpoint.php b/src/V2/Localization/LocalizedEndpoint.php index 040a71e..fa2b25e 100644 --- a/src/V2/Localization/LocalizedEndpoint.php +++ b/src/V2/Localization/LocalizedEndpoint.php @@ -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; } } diff --git a/src/V2/Localization/LocalizedEndpointProxy.php b/src/V2/Localization/LocalizedEndpointProxy.php deleted file mode 100644 index 47a1418..0000000 --- a/src/V2/Localization/LocalizedEndpointProxy.php +++ /dev/null @@ -1,83 +0,0 @@ -origin = $origin; - $this->lang = $lang; - } - - /** - * Dark magic, rebinding the origin method to $this context - * - * @todo: find a way that doesn't involve reflection, rebinding, ... to do this. - * - * @param string $name - * @param array $arguments - * @return mixed - */ - public function __call( $name, $arguments ) { - $method = new \ReflectionMethod( $this->origin, $name ); - if( !is_null( $method )) { - $closure = $method->getClosure( $this->origin )->bindTo( $this, $this->origin ); - return call_user_func_array( $closure, $arguments ); - } else { - trigger_error( 'Call to undefined method '.__CLASS__.'::'.$name.'()', E_USER_ERROR ); - return false; - } - } - - /** - * {@inheritdoc} - */ - protected function createRequest( array $query = [], $url = null, $method = 'GET', $options = [] ) { - $query = [ 'lang' => $this->lang ] + $query; - return $this->origin->createRequest( $query, $url, $method, $options ); - } - - /** - * {@inheritdoc} - */ - protected function url() { - return $this->origin->url(); - } - - /** - * {@inheritdoc} - */ - protected function getClient() { - return $this->origin->getClient(); - } - - /** - * @param string $language - * @return $this - */ - public function lang( $language ) { - return new LocalizedEndpointProxy( $this->origin, $language ); - } - - /** - * String representation of this localized endpoint. - * - * @return string - */ - function __toString() { - return '[' . get_class( $this ) . - '<' . get_class( $this->origin ) . '(' . $this->url() . ')>' . - '(' . $this->lang . ')]'; - } -} diff --git a/tests/LocalizedEndpointTest.php b/tests/LocalizedEndpointTest.php index 4e64db4..e0d4e40 100644 --- a/tests/LocalizedEndpointTest.php +++ b/tests/LocalizedEndpointTest.php @@ -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' ); - } }