From 8d312824c1ed2f87498c926be4efbc6bfdbdbc12 Mon Sep 17 00:00:00 2001 From: Matthew Batchelder Date: Wed, 22 May 2024 18:07:32 -0400 Subject: [PATCH] Add a couple more helper methods --- README.md | 52 ++++++++++++++++++++++++++++++++++++ src/SuperGlobals/Request.php | 50 ++++++++++++++++++++++++++++++++++ 2 files changed, 102 insertions(+) diff --git a/README.md b/README.md index 8c023fa..9c9b4c5 100644 --- a/README.md +++ b/README.md @@ -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) @@ -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()`. diff --git a/src/SuperGlobals/Request.php b/src/SuperGlobals/Request.php index afc8c3d..ad2d337 100644 --- a/src/SuperGlobals/Request.php +++ b/src/SuperGlobals/Request.php @@ -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.