2016-12-07 19:03:50 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (C) 2013-2016 Mailgun
|
|
|
|
*
|
|
|
|
* This software may be modified and distributed under the terms
|
|
|
|
* of the MIT license. See the LICENSE file for details.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Mailgun\Api;
|
|
|
|
|
|
|
|
use Mailgun\Assert;
|
2017-02-20 13:21:55 -06:00
|
|
|
use Mailgun\Resource\Api\PagingProvider;
|
2016-12-07 19:03:50 +01:00
|
|
|
use Psr\Http\Message\ResponseInterface;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Tobias Nyholm <tobias.nyholm@gmail.com>
|
|
|
|
*/
|
|
|
|
trait Pagination
|
|
|
|
{
|
|
|
|
abstract protected function httpGet($path, array $parameters = [], array $requestHeaders = []);
|
|
|
|
|
|
|
|
abstract protected function safeDeserialize(ResponseInterface $response, $className);
|
|
|
|
|
2017-02-20 13:21:55 -06:00
|
|
|
/**
|
|
|
|
* @param PagingProvider $response
|
|
|
|
*
|
|
|
|
* @return PagingProvider|null
|
|
|
|
*/
|
|
|
|
public function nextPage(PagingProvider $response)
|
|
|
|
{
|
|
|
|
return $this->getPaginationUrl($response->getNextUrl(), get_class($response));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PagingProvider $response
|
|
|
|
*
|
|
|
|
* @return PagingProvider|null
|
|
|
|
*/
|
|
|
|
public function previousPage(PagingProvider $response)
|
|
|
|
{
|
|
|
|
return $this->getPaginationUrl($response->getPreviousUrl(), get_class($response));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PagingProvider $response
|
|
|
|
*
|
|
|
|
* @return PagingProvider|null
|
|
|
|
*/
|
|
|
|
public function firstPage(PagingProvider $response)
|
|
|
|
{
|
|
|
|
return $this->getPaginationUrl($response->getFirstUrl(), get_class($response));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param PagingProvider $response
|
|
|
|
*
|
|
|
|
* @return PagingProvider|null
|
|
|
|
*/
|
|
|
|
public function lastPage(PagingProvider $response)
|
|
|
|
{
|
|
|
|
return $this->getPaginationUrl($response->getLastUrl(), get_class($response));
|
|
|
|
}
|
|
|
|
|
2016-12-07 19:03:50 +01:00
|
|
|
/**
|
|
|
|
* @param string $url
|
|
|
|
* @param string $class
|
|
|
|
*
|
2017-02-20 13:21:55 -06:00
|
|
|
* @return PagingProvider|null
|
2016-12-07 19:03:50 +01:00
|
|
|
*/
|
2017-02-20 13:21:55 -06:00
|
|
|
private function getPaginationUrl($url, $class)
|
2016-12-07 19:03:50 +01:00
|
|
|
{
|
|
|
|
Assert::stringNotEmpty($class);
|
|
|
|
|
|
|
|
if (empty($url)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$response = $this->httpGet($url);
|
|
|
|
|
|
|
|
return $this->safeDeserialize($response, $class);
|
|
|
|
}
|
|
|
|
}
|