Skip to content

Commit

Permalink
Refactored traits and added simple pagination
Browse files Browse the repository at this point in the history
  • Loading branch information
darthmaim committed May 17, 2015
1 parent de57eab commit 3f11aac
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 22 deletions.
21 changes: 0 additions & 21 deletions src/V2/BulkEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
namespace GW2Treasures\GW2Api\V2;

use GuzzleHttp\Client;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Message\ResponseInterface;
use GuzzleHttp\Pool;

trait BulkEndpoint {
Expand Down Expand Up @@ -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 );
}
30 changes: 30 additions & 0 deletions src/V2/EndpointTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php

namespace GW2Treasures\GW2Api\V2;

use GuzzleHttp\Client;
use GuzzleHttp\Message\RequestInterface;
use GuzzleHttp\Message\ResponseInterface;

trait EndpointTrait {

/**
* @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 );
}
12 changes: 11 additions & 1 deletion src/V2/Interfaces/IPaginatedEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,15 @@ function maxPageSize();
*
* @return array
*/
public function all();
function all();


/**
* Get a single page.
*
* @param int $index
* @param int $size
* @return mixed
*/
function page( $index, $size = null );
}
1 change: 1 addition & 0 deletions src/V2/LocalizedEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace GW2Treasures\GW2Api\V2;

trait LocalizedEndpoint {
use EndpointTrait;

/** @var string $language */
protected $language = 'en';
Expand Down
29 changes: 29 additions & 0 deletions src/V2/PaginatedEndpoint.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@

namespace GW2Treasures\GW2Api\V2;

use OutOfRangeException;

trait PaginatedEndpoint {
use EndpointTrait;

/**
* Max page size of this endpoint.
*
Expand All @@ -22,4 +26,29 @@ protected function maxPageSize() {
public function all() {
// TODO: implement
}

/**
* Get a single page.
*
* @param int $index
* @param int $size
* @return mixed
*/
public function page( $index, $size = null ) {
if( is_null( $size )) {
$size = $this->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 );
}
}
7 changes: 7 additions & 0 deletions tests/BasicTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 3f11aac

Please sign in to comment.