Skip to content

Commit

Permalink
Merge pull request #814 from Zauberfisch/master
Browse files Browse the repository at this point in the history
HTTPRequest and HTTPResponse now return $this on all setters
  • Loading branch information
chillu committed Sep 22, 2012
2 parents 039b402 + 7f1b6cf commit cc702df
Show file tree
Hide file tree
Showing 2 changed files with 135 additions and 33 deletions.
120 changes: 99 additions & 21 deletions control/HTTPRequest.php
Expand Up @@ -121,38 +121,68 @@ public function __construct($httpMethod, $url, $getVars = array(), $postVars = a
$this->postVars = (array)$postVars;
$this->body = $body;
}


/**
* @return bool
*/
public function isGET() {
return $this->httpMethod == 'GET';
}


/**
* @return bool
*/
public function isPOST() {
return $this->httpMethod == 'POST';
}


/**
* @return bool
*/
public function isPUT() {
return $this->httpMethod == 'PUT';
}

/**
* @return bool
*/
public function isDELETE() {
return $this->httpMethod == 'DELETE';
}
}

/**
* @return bool
*/
public function isHEAD() {
return $this->httpMethod == 'HEAD';
}

}

/**
* @param string $body
* @return SS_HTTPRequest $this
*/
public function setBody($body) {
$this->body = $body;
return $this;
}


/**
* @return null|string
*/
public function getBody() {
return $this->body;
}


/**
* @return array
*/
public function getVars() {
return $this->getVars;
}

/**
* @return array
*/
public function postVars() {
return $this->postVars;
}
Expand All @@ -167,15 +197,27 @@ public function postVars() {
public function requestVars() {
return ArrayLib::array_merge_recursive($this->getVars, $this->postVars);
}


/**
* @param string $name
* @return mixed
*/
public function getVar($name) {
if(isset($this->getVars[$name])) return $this->getVars[$name];
}


/**
* @param string $name
* @return mixed
*/
public function postVar($name) {
if(isset($this->postVars[$name])) return $this->postVars[$name];
}


/**
* @param string $name
* @return mixed
*/
public function requestVar($name) {
if(isset($this->postVars[$name])) return $this->postVars[$name];
if(isset($this->getVars[$name])) return $this->getVars[$name];
Expand Down Expand Up @@ -227,6 +269,7 @@ public function getHeaders() {
* Remove an existing HTTP header
*
* @param string $header
* @return mixed
*/
public function getHeader($header) {
return (isset($this->headers[$header])) ? $this->headers[$header] : null;
Expand All @@ -237,16 +280,17 @@ public function getHeader($header) {
* e.g. "Content-Type".
*
* @param string $header
* @return SS_HTTPRequest $this
*/
public function removeHeader($header) {
if(isset($this->headers[$header])) unset($this->headers[$header]);
return $this;
}

/**
* Returns the URL used to generate the page
*
* @param bool $includeGetVars whether or not to include the get parameters\
*
* @return string
*/
public function getURL($includeGetVars = false) {
Expand Down Expand Up @@ -318,6 +362,12 @@ public function offsetUnset($offset) {}

/**
* Construct an SS_HTTPResponse that will deliver a file to the client
*
* @static
* @param $fileData
* @param $fileName
* @param null $mimeType
* @return SS_HTTPResponse
*/
public static function send_file($fileData, $fileName, $mimeType = null) {
if(!$mimeType) {
Expand Down Expand Up @@ -350,6 +400,10 @@ public static function send_file($fileData, $fileName, $mimeType = null) {
*
* The pattern can optionally start with an HTTP method and a space. For example, "POST $Controller/$Action".
* This is used to define a rule that only matches on a specific HTTP method.
*
* @param $pattern
* @param bool $shiftOnSuccess
* @return array|bool
*/
public function match($pattern, $shiftOnSuccess = false) {
// Check if a specific method is required
Expand Down Expand Up @@ -431,7 +485,10 @@ public function match($pattern, $shiftOnSuccess = false) {
if($arguments === array()) $arguments['_matched'] = true;
return $arguments;
}


/**
* @return array
*/
public function allParams() {
return $this->allParams;
}
Expand All @@ -457,26 +514,43 @@ public function shiftAllParams() {

return $value;
}


/**
* @return array
*/
public function latestParams() {
return $this->latestParams;
}


/**
* @param string $name
* @return string|null
*/
public function latestParam($name) {
if(isset($this->latestParams[$name])) return $this->latestParams[$name];
else return null;
}


/**
* @return array
*/
public function routeParams() {
return $this->routeParams;
}


/**
* @param $params
* @return SS_HTTPRequest $this
*/
public function setRouteParams($params) {
$this->routeParams = $params;
return $this;
}

public function params()
{

/**
* @return array
*/
public function params() {
return array_merge($this->allParams, $this->routeParams);
}

Expand Down Expand Up @@ -507,6 +581,9 @@ public function remaining() {
/**
* Returns true if this is a URL that will match without shifting off any of the URL.
* This is used by the request handler to prevent infinite parsing loops.
*
* @param $pattern
* @return bool
*/
public function isEmptyPattern($pattern) {
if(preg_match('/^([A-Za-z]+) +(.*)$/', $pattern, $matches)) {
Expand All @@ -521,7 +598,6 @@ public function isEmptyPattern($pattern) {
* If you specify shifting more than 1 item off, then the items will be returned as an array
*
* @param int $count Shift Count
*
* @return String|Array
*/
public function shift($count = 1) {
Expand All @@ -543,6 +619,8 @@ public function shift($count = 1) {
/**
* Returns true if the URL has been completely parsed.
* This will respect parsed but unshifted directory parts.
*
* @return bool
*/
public function allParsed() {
return sizeof($this->dirParts) <= $this->unshiftedButParsedParts;
Expand Down
48 changes: 36 additions & 12 deletions control/HTTPResponse.php
Expand Up @@ -108,23 +108,27 @@ public function __construct($body = null, $statusCode = null, $statusDescription
* No newlines are allowed in the description.
* If omitted, will default to the standard HTTP description
* for the given $code value (see {@link $status_codes}).
* @return SS_HTTPRequest $this
*/
public function setStatusCode($code, $description = null) {
if(isset(self::$status_codes[$code])) $this->statusCode = $code;
else user_error("Unrecognised HTTP status code '$code'", E_USER_WARNING);

if($description) $this->statusDescription = $description;
else $this->statusDescription = self::$status_codes[$code];
return $this;
}

/**
* The text to be given alongside the status code ("reason phrase").
* Caution: Will be overwritten by {@link setStatusCode()}.
*
* @param String $description
* @param String $description
* @return SS_HTTPRequest $this
*/
public function setStatusDescription($description) {
$this->statusDescription = $description;
return $this;
}

/**
Expand All @@ -143,18 +147,28 @@ public function getStatusDescription() {

/**
* Returns true if this HTTP response is in error
*
* @return bool
*/
public function isError() {
return $this->statusCode && ($this->statusCode < 200 || $this->statusCode > 399);
}


/**
* @param string $body
* @return SS_HTTPRequest $this
*/
public function setBody($body) {
$this->body = $body;

// Set content-length in bytes. Use mbstring to avoid problems with mb_internal_encoding() and mbstring.func_overload
$this->headers['Content-Length'] = mb_strlen($this->body,'8bit');
return $this;
}


/**
* @return null|string
*/
public function getBody() {
return $this->body;
}
Expand All @@ -163,24 +177,24 @@ public function getBody() {
* Add a HTTP header to the response, replacing any header of the same name.
*
* @param string $header Example: "Content-Type"
* @param string $value Example: "text/xml"
* @param string $value Example: "text/xml"
* @return SS_HTTPRequest $this
*/
public function addHeader($header, $value) {
$this->headers[$header] = $value;
return $this;
}

/**
* Return the HTTP header of the given name.
*
* @param string $header
* @returns string
* @returns null|string
*/
public function getHeader($header) {
if(isset($this->headers[$header])) {
return $this->headers[$header];
} else {
return null;
}
if(isset($this->headers[$header]))
return $this->headers[$header];
return null;
}

/**
Expand All @@ -194,16 +208,24 @@ public function getHeaders() {
* Remove an existing HTTP header by its name,
* e.g. "Content-Type".
*
* @param unknown_type $header
* @param string $header
* @return SS_HTTPRequest $this
*/
public function removeHeader($header) {
if(isset($this->headers[$header])) unset($this->headers[$header]);
return $this;
}


/**
* @param string $dest
* @param int $code
* @return SS_HTTPRequest $this
*/
public function redirect($dest, $code=302) {
if(!in_array($code, self::$redirect_codes)) $code = 302;
$this->setStatusCode($code);
$this->headers['Location'] = $dest;
return $this;
}

/**
Expand Down Expand Up @@ -244,6 +266,8 @@ public function output() {
/**
* Returns true if this response is "finished", that is, no more script execution should be done.
* Specifically, returns true if a redirect has already been requested
*
* @return bool
*/
public function isFinished() {
return in_array($this->statusCode, array(301, 302, 401, 403));
Expand Down

0 comments on commit cc702df

Please sign in to comment.