1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace StephaneCoinon\SendGridActivity\Requests; |
4
|
|
|
|
5
|
|
|
use StephaneCoinon\SendGridActivity\Responses\Response; |
6
|
|
|
|
7
|
|
|
/** |
8
|
|
|
* Base request builder. |
9
|
|
|
*/ |
10
|
|
|
class Request |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* Endpoint URL. |
14
|
|
|
* |
15
|
|
|
* @var string |
16
|
|
|
*/ |
17
|
|
|
protected $endpoint = ''; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Response model class to map request results to. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
protected $response = Response::class; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* Request method. |
28
|
|
|
* |
29
|
|
|
* @var string |
30
|
|
|
*/ |
31
|
|
|
protected $method = 'GET'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Resource unique identifier. |
35
|
|
|
* |
36
|
|
|
* @var string |
37
|
|
|
*/ |
38
|
|
|
protected $key = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Query string parameters. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
protected $queryStringParameters = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* Builder a request to fetch a resource by unique identifier. |
49
|
|
|
* |
50
|
|
|
* @param string $key |
51
|
|
|
* @return self |
52
|
|
|
*/ |
53
|
|
|
public static function find($key): self |
54
|
|
|
{ |
55
|
|
|
$request = new static; |
56
|
|
|
$request->key = $key; |
57
|
|
|
|
58
|
|
|
return $request; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Return the HTTP method used for this request. |
63
|
|
|
* |
64
|
|
|
* @return string |
65
|
|
|
*/ |
66
|
|
|
public function getMethod(): string |
67
|
|
|
{ |
68
|
|
|
return $this->method; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* Get the response model class to map request result(s) to. |
73
|
|
|
* |
74
|
|
|
* @return string |
75
|
|
|
*/ |
76
|
|
|
public function getResponseModel(): string |
77
|
|
|
{ |
78
|
|
|
return $this->response; |
79
|
|
|
} |
80
|
|
|
|
81
|
|
|
/** |
82
|
|
|
* Add a parameter to the query string. |
83
|
|
|
* |
84
|
|
|
* @param string $key |
85
|
|
|
* @param mixed $value |
86
|
|
|
* @return self |
87
|
|
|
*/ |
88
|
|
|
public function withParameter(string $key, $value): self |
89
|
|
|
{ |
90
|
|
|
$this->queryStringParameters[$key] = $value; |
91
|
|
|
|
92
|
|
|
return $this; |
93
|
|
|
} |
94
|
|
|
|
95
|
|
|
/** |
96
|
|
|
* Encode the query string parameters. |
97
|
|
|
* |
98
|
|
|
* @param array $parameters |
99
|
|
|
* @return string |
100
|
|
|
*/ |
101
|
|
|
protected function encode(array $parameters): string |
102
|
|
|
{ |
103
|
|
|
return http_build_query( |
104
|
|
|
$parameters, |
105
|
|
|
null, |
106
|
|
|
'&', |
107
|
|
|
PHP_QUERY_RFC3986 // use percent-encoding |
108
|
|
|
); |
109
|
|
|
} |
110
|
|
|
|
111
|
|
|
/** |
112
|
|
|
* Build the query string. |
113
|
|
|
* |
114
|
|
|
* @return string |
115
|
|
|
*/ |
116
|
|
|
protected function buildQueryString(): string |
117
|
|
|
{ |
118
|
|
|
return $this->encode($this->queryStringParameters); |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Build the request URL. |
123
|
|
|
* |
124
|
|
|
* @return string |
125
|
|
|
*/ |
126
|
|
|
public function buildUrl(): string |
127
|
|
|
{ |
128
|
|
|
$queryString = $this->buildQueryString(); |
129
|
|
|
|
130
|
|
|
return $this->endpoint |
131
|
|
|
. ($this->key ? '/' . $this->key : '') |
132
|
|
|
. ($queryString ? '?' . $queryString : ''); |
133
|
|
|
} |
134
|
|
|
} |
135
|
|
|
|