1 | <?php |
||
8 | class Request |
||
9 | { |
||
10 | protected $parameters; |
||
11 | protected $httpMethod; |
||
12 | protected $httpUrl; |
||
13 | public static $version = '1.0'; |
||
14 | |||
15 | /** |
||
16 | * Constructor |
||
17 | * |
||
18 | * @param string $httpMethod |
||
19 | * @param string $httpUrl |
||
20 | * @param array|null $parameters |
||
21 | */ |
||
22 | public function __construct($httpMethod, $httpUrl, array $parameters = []) |
||
23 | { |
||
24 | $parameters = array_merge(Util::parseParameters(parse_url($httpUrl, PHP_URL_QUERY)), $parameters); |
||
|
|||
25 | $this->parameters = $parameters; |
||
26 | $this->httpMethod = $httpMethod; |
||
27 | $this->httpUrl = $httpUrl; |
||
28 | } |
||
29 | |||
30 | /** |
||
31 | * pretty much a helper function to set up the request |
||
32 | * |
||
33 | * @param Consumer $consumer |
||
34 | * @param Token $token |
||
35 | * @param string $httpMethod |
||
36 | * @param string $httpUrl |
||
37 | * @param array $parameters |
||
38 | * |
||
39 | * @return Request |
||
40 | */ |
||
41 | public static function fromConsumerAndToken( |
||
42 | Consumer $consumer, |
||
43 | Token $token = null, |
||
44 | $httpMethod, |
||
45 | $httpUrl, |
||
46 | array $parameters = [] |
||
47 | ) { |
||
48 | $defaults = [ |
||
49 | "oauth_version" => Request::$version, |
||
50 | "oauth_nonce" => Request::generateNonce(), |
||
51 | "oauth_timestamp" => time(), |
||
52 | "oauth_consumer_key" => $consumer->key |
||
53 | ]; |
||
54 | if (null !== $token) { |
||
55 | $defaults['oauth_token'] = $token->key; |
||
56 | } |
||
57 | |||
58 | $parameters = array_merge($defaults, $parameters); |
||
59 | |||
60 | return new Request($httpMethod, $httpUrl, $parameters); |
||
61 | } |
||
62 | |||
63 | /** |
||
64 | * @param string $name |
||
65 | * @param string $value |
||
66 | */ |
||
67 | public function setParameter($name, $value) |
||
68 | { |
||
69 | $this->parameters[$name] = $value; |
||
70 | } |
||
71 | |||
72 | /** |
||
73 | * @param $name |
||
74 | * |
||
75 | * @return string|null |
||
76 | */ |
||
77 | public function getParameter($name) |
||
78 | { |
||
79 | return isset($this->parameters[$name]) ? $this->parameters[$name] : null; |
||
80 | } |
||
81 | |||
82 | /** |
||
83 | * @return array |
||
84 | */ |
||
85 | public function getParameters() |
||
89 | |||
90 | /** |
||
91 | * @param $name |
||
92 | */ |
||
93 | public function removeParameter($name) |
||
97 | |||
98 | /** |
||
99 | * The request parameters, sorted and concatenated into a normalized string. |
||
100 | * |
||
101 | * @return string |
||
102 | */ |
||
103 | public function getSignableParameters() |
||
116 | |||
117 | /** |
||
118 | * Returns the base string of this request |
||
119 | * |
||
120 | * The base string defined as the method, the url |
||
121 | * and the parameters (normalized), each urlencoded |
||
122 | * and the concated with &. |
||
123 | * |
||
124 | * @return string |
||
125 | */ |
||
126 | public function getSignatureBaseString() |
||
138 | |||
139 | /** |
||
140 | * Returns the HTTP Method in uppercase |
||
141 | * |
||
142 | * @return string |
||
143 | */ |
||
144 | public function getNormalizedHttpMethod() |
||
148 | |||
149 | /** |
||
150 | * parses the url and rebuilds it to be |
||
151 | * scheme://host/path |
||
152 | * |
||
153 | * @return string |
||
154 | */ |
||
155 | public function getNormalizedHttpUrl() |
||
165 | |||
166 | /** |
||
167 | * Builds a url usable for a GET request |
||
168 | * |
||
169 | * @return string |
||
170 | */ |
||
171 | public function toUrl() |
||
180 | |||
181 | /** |
||
182 | * Builds the data one would send in a POST request |
||
183 | * |
||
184 | * @return string |
||
185 | */ |
||
186 | public function toPostdata() |
||
190 | |||
191 | /** |
||
192 | * Builds the Authorization: header |
||
193 | * |
||
194 | * @return string |
||
195 | * @throws TwitterOAuthException |
||
196 | */ |
||
197 | public function toHeader() |
||
214 | |||
215 | /** |
||
216 | * @return string |
||
217 | */ |
||
218 | public function __toString() |
||
222 | |||
223 | /** |
||
224 | * @param SignatureMethod $signatureMethod |
||
225 | * @param Consumer $consumer |
||
226 | * @param Token $token |
||
227 | */ |
||
228 | public function signRequest(SignatureMethod $signatureMethod, Consumer $consumer, Token $token = null) |
||
234 | |||
235 | /** |
||
236 | * @param SignatureMethod $signatureMethod |
||
237 | * @param Consumer $consumer |
||
238 | * @param Token $token |
||
239 | * |
||
240 | * @return string |
||
241 | */ |
||
242 | public function buildSignature(SignatureMethod $signatureMethod, Consumer $consumer, Token $token = null) |
||
246 | |||
247 | /** |
||
248 | * @return string |
||
249 | */ |
||
250 | public static function generateNonce() |
||
254 | } |
||
255 |