Skip to content

Commit e65f127

Browse files
committed
move logic for checking if POST data is allowed
1 parent d31e305 commit e65f127

File tree

3 files changed

+38
-22
lines changed

3 files changed

+38
-22
lines changed

src/Request.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,21 @@ class Request
2121
const ENCODING_JSON = 1;
2222
const ENCODING_RAW = 2;
2323

24+
/**
25+
* Allowed methods => allows postdata
26+
*
27+
* @var array
28+
*/
29+
public static $methods = array(
30+
'get' => false,
31+
'head' => false,
32+
'post' => true,
33+
'put' => true,
34+
'patch' => true,
35+
'delete' => false,
36+
'options' => false,
37+
);
38+
2439
/**
2540
* The HTTP method to use. Defaults to GET.
2641
*
@@ -101,10 +116,14 @@ public function setMethod($method)
101116
{
102117
$method = strtolower($method);
103118

104-
if (!array_key_exists($method, $this->curl->getAllowedMethods())) {
119+
if (!array_key_exists($method, static::$methods)) {
105120
throw new \InvalidArgumentException("Method [$method] not a valid HTTP method.");
106121
}
107122

123+
if ($this->data && !static::$methods[$method]) {
124+
throw new \LogicException('Request has POST data, but tried changing HTTP method to one that does not allow POST data');
125+
}
126+
108127
$this->method = $method;
109128

110129
return $this;
@@ -302,11 +321,25 @@ public function formatHeaders()
302321
*/
303322
public function setData($data)
304323
{
324+
if ($data && !static::$methods[$this->method]) {
325+
throw new \InvalidArgumentException("HTTP method [$this->method] does not allow POST data.");
326+
}
327+
305328
$this->data = $data;
306329

307330
return $this;
308331
}
309332

333+
/**
334+
* Check whether the request has any data.
335+
*
336+
* @return boolean
337+
*/
338+
public function hasData()
339+
{
340+
return (bool) $this->data;
341+
}
342+
310343
/**
311344
* Get the POST data to be sent with the request.
312345
*

src/cURL.php

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,6 @@ class cURL
3838
*/
3939
protected $ch;
4040

41-
/**
42-
* Allowed methods => allows postdata
43-
*
44-
* @var array
45-
*/
46-
protected $methods = array(
47-
'get' => false,
48-
'head' => false,
49-
'post' => true,
50-
'put' => true,
51-
'patch' => true,
52-
'delete' => false,
53-
'options' => false,
54-
);
55-
5641
/**
5742
* The request class to use.
5843
*
@@ -88,7 +73,7 @@ class cURL
8873
*/
8974
public function getAllowedMethods()
9075
{
91-
return $this->methods;
76+
return Request::$methods;
9277
}
9378

9479
/**
@@ -278,7 +263,7 @@ public function prepareRequest(Request $request)
278263

279264
curl_setopt($this->ch, CURLOPT_HTTPHEADER, $request->formatHeaders());
280265

281-
if ($this->methods[$method] === true) {
266+
if ($request->hasData()) {
282267
curl_setopt($this->ch, CURLOPT_POSTFIELDS, $request->encodeData());
283268
}
284269

@@ -358,7 +343,7 @@ public function __call($func, $args)
358343
$method = substr($method, 3);
359344
}
360345

361-
if (!array_key_exists($method, $this->methods)) {
346+
if (!array_key_exists($method, Request::$methods)) {
362347
throw new \BadMethodCallException("Method [$method] not a valid HTTP method.");
363348
}
364349

@@ -368,9 +353,6 @@ public function __call($func, $args)
368353
$url = $args[0];
369354

370355
if (isset($args[1])) {
371-
if (!$this->methods[$method]) {
372-
throw new \InvalidArgumentException("HTTP method [$method] does not allow POST data.");
373-
}
374356
$data = $args[1];
375357
} else {
376358
$data = null;

tests/unit/RequestTest.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ public function settersAndGetters()
3737
public function encodeData()
3838
{
3939
$r = $this->makeRequest();
40+
$r->setMethod('post');
4041

4142
$r->setData(array('foo' => 'bar', 'bar' => 'baz'));
4243
$this->assertEquals('foo=bar&bar=baz', $r->encodeData());

0 commit comments

Comments
 (0)