From 3f11aac90bdec73b0d8d6fb093d6d3872b006e0e Mon Sep 17 00:00:00 2001 From: darthmaim Date: Sun, 17 May 2015 17:38:23 +0200 Subject: [PATCH] Refactored traits and added simple pagination --- src/V2/BulkEndpoint.php | 21 ----------------- src/V2/EndpointTrait.php | 30 ++++++++++++++++++++++++ src/V2/Interfaces/IPaginatedEndpoint.php | 12 +++++++++- src/V2/LocalizedEndpoint.php | 1 + src/V2/PaginatedEndpoint.php | 29 +++++++++++++++++++++++ tests/BasicTest.php | 7 ++++++ 6 files changed, 78 insertions(+), 22 deletions(-) create mode 100644 src/V2/EndpointTrait.php diff --git a/src/V2/BulkEndpoint.php b/src/V2/BulkEndpoint.php index 2af1f47..05c373d 100644 --- a/src/V2/BulkEndpoint.php +++ b/src/V2/BulkEndpoint.php @@ -3,8 +3,6 @@ namespace GW2Treasures\GW2Api\V2; use GuzzleHttp\Client; -use GuzzleHttp\Message\RequestInterface; -use GuzzleHttp\Message\ResponseInterface; use GuzzleHttp\Pool; trait BulkEndpoint { @@ -92,23 +90,4 @@ public function all() { return $this->many( $ids ); } } - - /** - * @return Client - */ - protected abstract function getClient(); - - /** - * Creates a new Request to this Endpoint. - * - * @param string[] $query - * @param null $url - * @param string $method - * @param array $options - * @return \GuzzleHttp\Message\Request|\GuzzleHttp\Message\RequestInterface - */ - protected abstract function createRequest( array $query = [], $url = null, $method = 'GET', $options = [] ); - - protected abstract function request( RequestInterface $request ); - protected abstract function getResponseAsJson( ResponseInterface $response ); } diff --git a/src/V2/EndpointTrait.php b/src/V2/EndpointTrait.php new file mode 100644 index 0000000..9aa0134 --- /dev/null +++ b/src/V2/EndpointTrait.php @@ -0,0 +1,30 @@ +maxPageSize(); + } + + if( $size > $this->maxPageSize() || $size <= 0 ) { + throw new OutOfRangeException('$size has to be between 0 and ' . $this->maxPageSize() . ', was ' . $size ); + } + + if( $index < 0 ) { + throw new OutOfRangeException('$index has to be 0 or greater'); + } + + $request = $this->createRequest(['page' => $index, 'page_size' => $size ]); + $response = $this->request( $request ); + return $this->getResponseAsJson( $response ); + } } diff --git a/tests/BasicTest.php b/tests/BasicTest.php index 279a57f..9b7814f 100644 --- a/tests/BasicTest.php +++ b/tests/BasicTest.php @@ -12,6 +12,13 @@ public function testQuaggans() { $q2 = $quagganEndpoint->many( $quagganEndpoint->ids() ); } + public function testPagination() { + $quaggans = $this->api()->quaggans()->page(2, 2); + + $this->assertEquals( 'bowl', $quaggans[0]->id ); + $this->assertEquals( 'box', $quaggans[1]->id ); + } + public function testWorlds() { $anvilRock = $this->api()->worlds()->get(1001); $this->assertEquals("Anvil Rock", $anvilRock->name);