1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of :project_name |
4
|
|
|
* |
5
|
|
|
* PHP version 7 |
6
|
|
|
* |
7
|
|
|
* @category PHP |
8
|
|
|
* @package WilliamEspindola\AbstractHTTPClient |
9
|
|
|
* @author William Espindola <[email protected]> |
10
|
|
|
* @copyright Free |
11
|
|
|
* @license MIT |
12
|
|
|
* @link https://github.com/williamespindola/abstract-http-client |
13
|
|
|
*/ |
14
|
|
|
declare(strict_types=1); |
15
|
|
|
|
16
|
|
|
namespace WilliamEspindola\AbstractHTTPClient\QueryString; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Manange extra query string options like Limit, Offset and Sort |
20
|
|
|
* |
21
|
|
|
* @category PHP |
22
|
|
|
* @package WilliamEspindola\AbstractHTTPClient |
23
|
|
|
* @author William Espindola <[email protected]> |
24
|
|
|
* @copyright Free |
25
|
|
|
* @license MIT |
26
|
|
|
* @version Release: 0.0.0 |
27
|
|
|
* @link https://github.com/williamespindola/abstract-http-client |
28
|
|
|
*/ |
29
|
|
|
trait ExtraQueryString |
30
|
|
|
{ |
31
|
|
|
/** |
32
|
|
|
* @var array $queryString query string elements |
33
|
|
|
*/ |
34
|
|
|
private $queryString = []; |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Limit result |
38
|
|
|
* |
39
|
|
|
* @param Integer $number The number of results |
40
|
|
|
* |
41
|
|
|
* @return void |
42
|
|
|
*/ |
43
|
|
|
public function setLimit(int $number) |
44
|
|
|
{ |
45
|
|
|
$this->queryString[] = 'limit=' . $number; |
46
|
|
|
} |
47
|
|
|
|
48
|
|
|
/** |
49
|
|
|
* Offset result |
50
|
|
|
* |
51
|
|
|
* @param Integer $offset Offset result |
52
|
|
|
* |
53
|
|
|
* @return void |
54
|
|
|
*/ |
55
|
|
|
public function setOffset(int $offset) |
56
|
|
|
{ |
57
|
|
|
$this->queryString[] = 'offset=' . $offset; |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
/** |
61
|
|
|
* Sort result |
62
|
|
|
* |
63
|
|
|
* @param array $sort Sort the result options |
64
|
|
|
* |
65
|
|
|
* @return void |
66
|
|
|
*/ |
67
|
|
|
public function setSort(array $sort) |
68
|
|
|
{ |
69
|
|
|
if (count($sort) == 1) { |
70
|
|
|
$sort = $sort[0]; |
71
|
|
|
} |
72
|
|
|
|
73
|
|
|
if (count($sort) > 1) { |
74
|
|
|
$sort = implode(',', $sort); |
75
|
|
|
} |
76
|
|
|
|
77
|
|
|
$this->queryString[] = 'sort=' . $sort; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* Get uri with extra query strings |
82
|
|
|
* |
83
|
|
|
* @param String $uri String uri |
84
|
|
|
* |
85
|
|
|
* @return String Uri concatenated with extra query string |
86
|
|
|
*/ |
87
|
|
|
public function getUriWithExtraString(string $uri): String |
88
|
|
|
{ |
89
|
|
|
if (!$this->queryString) { |
90
|
|
|
return $uri; |
91
|
|
|
} |
92
|
|
|
|
93
|
|
|
return $uri . '?' . implode('&', $this->queryString); |
94
|
|
|
} |
95
|
|
|
} |
96
|
|
|
|