\DewdropRequest

A simple wrapper for HTTP request super-globals.

This wrapper serves two primary uses:

  1. It makes it easier to retrieve values from $_POST and $_GET without generating E_NOTICEs due to undefined variables.
  2. It makes it easier to inject other values during unit testing.

Summary

Methods
Properties
Constants
__construct()
setTrustedClientIpHeader()
getClientIp()
getUrl()
isAjax()
isPost()
isGet()
getPost()
getPostBody()
getQuery()
setMethod()
setPost()
setQuery()
No public properties found
No constants found
No protected methods found
No protected properties found
N/A
No private methods found
$post
$query
$method
$trustedClientIpHeader
N/A

Properties

$post

$post : array

The post values for this request. By default, $_POST will be used, but you can inject other values into the constructor.

Type

array

$query

$query : array

The query string values for this request. By default, $_POST will be used, but you can inject other values into the constructor.

Type

array

$method

$method : string

The request method currently being used (e.g. POST or GET). This is taken from $_SERVER['REQUEST_METHOD'] by default, but an alternate value can be injected into the constructor.

Type

string

$trustedClientIpHeader

$trustedClientIpHeader : string

An index present in $_SERVER that is set by a trusted proxy and can be used to get the user's actual IP address.

Type

string

Methods

__construct()

__construct(array  $post = null, array  $query = null, string  $method = null) 

Create request, optionally injecting alterative values for post, query, and method properties, primarily to aid in testing.

Parameters

array $post
array $query
string $method

setTrustedClientIpHeader()

setTrustedClientIpHeader(string  $trustedClientIpHeader) : $this

Parameters

string $trustedClientIpHeader

Returns

$this

getClientIp()

getClientIp() : string

Returns

string

getUrl()

getUrl() : string

Assemble a URL based upon the current Request information.

Returns

string

isAjax()

isAjax() : boolean

Just a simple utility method to check whether the request is an AJAX call by looking for the HTTP_X_REQUESTED_WITH header, which should be added by most (all?) major JS libraries.

Returns

boolean

isPost()

isPost() : boolean

Whether the request is a POST.

Returns

boolean

isGet()

isGet() : boolean

Whether the request is a GET.

Returns

boolean

getPost()

getPost(string  $name = null, mixed  $default = null) : mixed

Get either a single POST variable (by passing a string to the name $name parameter) or the entire POST array (by leaving $name empty).

The second parameter can be used to specify an alternate default value, if the variable specified by $name is not present in the post data.

Parameters

string $name
mixed $default

Returns

mixed

getPostBody()

getPostBody() : string

Get the contents of the body of a POST request. Useful in RESTful API work.

Returns

string

getQuery()

getQuery(string  $name = null, mixed  $default = null) : mixed

Get either a single query variable (by passing a string to the name $name parameter) or the entire query array (by leaving $name empty).

The second parameter can be used to specify an alternate default value, if the variable specified by $name is not present in the post data.

Parameters

string $name
mixed $default

Returns

mixed

setMethod()

setMethod(string  $method) : \Dewdrop\Request

Manually override the request method after instantiation. This is primarily helpful in testing.

Parameters

string $method

Returns

\Dewdrop\Request

setPost()

setPost(mixed  $key, mixed  $value = null) : \Dewdrop\Request

Modify or add the POST value specified by key. This is primarily useful for manipulating requests during testing. If you pass an array as the $key, you'll overwrite the entirety of the POST data.

Parameters

mixed $key
mixed $value

Returns

\Dewdrop\Request

setQuery()

setQuery(mixed  $key, mixed  $value = null) : \Dewdrop\Request

Modify or add the GET value specified by key. This is primarily useful for manipulating requests during testing. If you pass an array as the $key, you'll overwrite the entirety of the GET data.

Parameters

mixed $key
mixed $value

Returns

\Dewdrop\Request