1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* Copyright (C) 2013-2016 Mailgun |
5
|
|
|
* |
6
|
|
|
* This software may be modified and distributed under the terms |
7
|
|
|
* of the MIT license. See the LICENSE file for details. |
8
|
|
|
*/ |
9
|
|
|
|
10
|
|
|
namespace Mailgun\Api; |
11
|
|
|
|
12
|
|
|
use Mailgun\Assert; |
13
|
|
|
use Mailgun\Resource\Api\PagingProvider; |
14
|
|
|
use Psr\Http\Message\ResponseInterface; |
15
|
|
|
|
16
|
|
|
/** |
17
|
|
|
* @author Tobias Nyholm <[email protected]> |
18
|
|
|
*/ |
19
|
|
|
trait Pagination |
20
|
|
|
{ |
21
|
|
|
abstract protected function httpGet($path, array $parameters = [], array $requestHeaders = []); |
22
|
|
|
|
23
|
|
|
abstract protected function safeDeserialize(ResponseInterface $response, $className); |
24
|
|
|
|
25
|
|
|
/** |
26
|
|
|
* @param PagingProvider $response |
27
|
|
|
* |
28
|
|
|
* @return PagingProvider|null |
29
|
|
|
*/ |
30
|
|
|
public function nextPage(PagingProvider $response) |
31
|
|
|
{ |
32
|
|
|
return $this->getPaginationUrl($response->getNextUrl(), get_class($response)); |
33
|
|
|
} |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @param PagingProvider $response |
37
|
|
|
* |
38
|
|
|
* @return PagingProvider|null |
39
|
|
|
*/ |
40
|
|
|
public function previousPage(PagingProvider $response) |
41
|
|
|
{ |
42
|
|
|
return $this->getPaginationUrl($response->getPreviousUrl(), get_class($response)); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @param PagingProvider $response |
47
|
|
|
* |
48
|
|
|
* @return PagingProvider|null |
49
|
|
|
*/ |
50
|
|
|
public function firstPage(PagingProvider $response) |
51
|
|
|
{ |
52
|
|
|
return $this->getPaginationUrl($response->getFirstUrl(), get_class($response)); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param PagingProvider $response |
57
|
|
|
* |
58
|
|
|
* @return PagingProvider|null |
59
|
|
|
*/ |
60
|
|
|
public function lastPage(PagingProvider $response) |
61
|
|
|
{ |
62
|
|
|
return $this->getPaginationUrl($response->getLastUrl(), get_class($response)); |
63
|
|
|
} |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* @param string $url |
67
|
|
|
* @param string $class |
68
|
|
|
* |
69
|
|
|
* @return PagingProvider|null |
70
|
|
|
*/ |
71
|
|
|
private function getPaginationUrl($url, $class) |
72
|
|
|
{ |
73
|
|
|
Assert::stringNotEmpty($class); |
74
|
|
|
|
75
|
|
|
if (empty($url)) { |
76
|
|
|
return; |
77
|
|
|
} |
78
|
|
|
|
79
|
|
|
$response = $this->httpGet($url); |
80
|
|
|
|
81
|
|
|
return $this->safeDeserialize($response, $class); |
82
|
|
|
} |
83
|
|
|
} |
84
|
|
|
|