Skip to content

Commit

Permalink
Add a couple more helper methods
Browse files Browse the repository at this point in the history
  • Loading branch information
borkweb committed May 22, 2024
1 parent 2f61ea6 commit 8d31282
Show file tree
Hide file tree
Showing 2 changed files with 102 additions and 0 deletions.
52 changes: 52 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ A library that handles access to superglobals.
* [Usage](#usage)
* [`Request::get_get_var( $var, $default = null )`](#get_get_var-var-default-null)
* [`Request::get_post_var( $var, $default = null )`](#get_post_var-var-default-null)
* [`Request::get_raw_superglobal( $var, $default = null )`](#get_raw_superglobal-var-default-null)
* [`Request::get_sanitized_superglobal( $var, $default = null )`](#get_sanitized_superglobal-var-default-null)
* [`Request::get_server_var( $var, $default = null )`](#get_server_var-var-default-null)
* [`Request::get_var( $var, $default = null )`](#get_var-var-default-null)
* [`Request::sanitize_deep( &$value )`](#sanitize_deep-value)
Expand Down Expand Up @@ -63,6 +65,56 @@ $var = Request::get_post_var( 'post_id' );
$var = Request::get_post_var( 'post_id', 12 );
```

### `Request::get_raw_superglobal( $var, $default = null )`

Gets the requested superglobal variable. Options are `ENV`, `GET`, `POST`, `REQUEST`, or `SERVER`.

#### Example

```php
use StellarWP\SuperGlobals\Request;

// Get $_ENV
$env = Request::get_raw_superglobal( 'ENV' );

// Get $_GET
$get = Request::get_raw_superglobal( 'GET' );

// Get $_POST
$post = Request::get_raw_superglobal( 'POST' );

// Get $_REQUEST
$request = Request::get_raw_superglobal( 'REQUEST' );

// Get $_SERVER
$server = Request::get_raw_superglobal( 'SERVER' );
```

### `Request::get_sanitized_superglobal( $var, $default = null )`

Gets the requested superglobal variable, sanitized. Options are `ENV`, `GET`, `POST`, `REQUEST`, or `SERVER`.

#### Example

```php
use StellarWP\SuperGlobals\Request;

// Get $_ENV
$env = Request::get_sanitized_superglobal( 'ENV' );

// Get $_GET
$get = Request::get_sanitized_superglobal( 'GET' );

// Get $_POST
$post = Request::get_sanitized_superglobal( 'POST' );

// Get $_REQUEST
$request = Request::get_sanitized_superglobal( 'REQUEST' );

// Get $_SERVER
$server = Request::get_sanitized_superglobal( 'SERVER' );
```

### `Request::get_server_var( $var, $default = null )`

Get a `$_SERVER` value and recursively sanitize it using `Request::sanitize_deep()`.
Expand Down
50 changes: 50 additions & 0 deletions src/SuperGlobals/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,56 @@ public static function get_post_var( string $var, $default = null ) {
return static::sanitize_deep( $unsafe );
}

/**
* Gets the requested superglobal variable.
*
* @param string $superglobal A superglobal, such as 'ENV', 'GET', 'POST', 'REQUEST', or 'SERVER'.
*
* @return mixed
*/
public static function get_raw_superglobal( string $superglobal ) {
$superglobal = strtoupper( $superglobal );

switch ( $superglobal ) {
case '_ENV':
case 'ENV':
$var = $_ENV;
break;
case '_GET':
case 'GET':
$var = $_GET;
break;
case '_POST':
case 'POST':
$var = $_POST;
break;
case '_REQUEST':
case 'REQUEST':
$var = $_REQUEST;
break;
case '_SERVER':
case 'SERVER':
$var = $_SERVER;
break;
default:
return [];
}

return $var;
}

/**
* Gets the requested superglobal variable, sanitized.
*
* @param string $superglobal A superglobal, such as 'ENV', 'GET', 'POST', 'REQUEST', or 'SERVER'.
*
* @return mixed
*/
public static function get_sanitized_superglobal( string $superglobal ) {
$var = static::get_raw_superglobal( $superglobal );
return static::sanitize_deep( $var );
}

/**
* Tests to see if the requested variable is set either as a post field or as a URL
* param and returns the value if so.
Expand Down

0 comments on commit 8d31282

Please sign in to comment.