Skip to content
Snippets Groups Projects
Commit fd6d6eea authored by Hahn Axel (hahn)'s avatar Hahn Axel (hahn)
Browse files

queryparam class: php8 only; added variable types; short array syntax

parent b8488569
No related branches found
No related tags found
1 merge request!66php8 only; added variable types; short array syntax; remove glyphicons
......@@ -9,7 +9,7 @@
---------------------------------------------------------------------
Axel <axel.hahn@unibe.ch>
2013-11-08 Axel
2024-08-28 Axel php8 only; added variable types; short array syntax
2024-08-29 Axel php8 only; added variable types; short array syntax
###################################################################### */
require_once 'base.class.php';
......
<?php
/**
* Wrapping class to access $_GET|$_POST|$_SESSION variables with validation
* of a value
*
* @example
* Ensure that https://example.com/?page=<VALUE> will be accepted only if
* <VALUE> matches /^[a-z]*$/
* <code>queryparam::get('page', '/^[a-z]*$/');</code>
*
* Axel <axel.hahn@unibe.ch>
* 2024-08-29 Axel php8 only; added variable types; short array syntax
*/
class queryparam{
class queryparam
{
/**
* return value of a a given scope variable (e.g. $_GET|$_POST|$_SESSION) if it exists.
* Get value of a a given scope variable (e.g. $_GET|$_POST|$_SESSION) if it exists.
* It will return NULLL if the value does not match an optional regex or type.
*
* @param array $aScope array of scope variables
* @param string $sVarname name of post or get variable (POST has priority)
* @param string $sRegexMatch set a regex that must match
* @param string $sType force type: false|int
* @return mixed NULL|value
*/
static public function getvar($aScope, $sVarname, $sRegexMatch=false, $sType=false){
static public function getvar(array $aScope, string $sVarname, string $sRegexMatch = '', string $sType = ''): mixed
{
// check if it exist
if (!isset($aScope[$sVarname])) {
return NULL;
......@@ -44,7 +58,8 @@ class queryparam{
* @param string $sType force type: false|int
* @return mixed NULL|value
*/
static function get($sVarname, $sRegexMatch=false, $sType=false) {
static function get(string $sVarname, string $sRegexMatch = '', string $sType = ''): mixed
{
return self::getvar($_GET, $sVarname, $sRegexMatch, $sType);
}
......@@ -56,7 +71,8 @@ class queryparam{
* @param string $sType force type: false|int
* @return mixed NULL|value
*/
static function getorpost($sVarname, $sRegexMatch=false, $sType=false) {
static function getorpost(string $sVarname, string $sRegexMatch = '', string $sType = ''): mixed
{
// $this->logAdd(__METHOD__."($sVarname, $sRegexMatch, $sType) start");
// check if it exist
......@@ -84,7 +100,8 @@ class queryparam{
* @param string $sType force type: false|int
* @return mixed NULL|value
*/
static function post($sVarname, $sRegexMatch=false, $sType=false) {
static function post(string $sVarname, string $sRegexMatch = '', string $sType = ''): mixed
{
return self::getvar($_POST, $sVarname, $sRegexMatch, $sType);
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment