Minor changes

This commit is contained in:
2026-08-19 11:30:57 -03:00
parent b4e8758d5d
commit accda2f356
5 changed files with 486 additions and 181 deletions
+180 -39
View File
@@ -2,33 +2,92 @@
namespace RR;
class Request{
use RR\ParamType;
const PARAM_ALPHA = 'alpha';
const PARAM_ALPHANUM = 'alphanum';
/**
* Class to abstract the input (GET PARAMS, POST, etc...) value grabbing
* POST always have precedence
*/
class Request
{
const PARAM_INT = 'integer';
const PARAM_DOUBLE = 'double';
public static function requiredParam($parname, $type, $default = '', $options = Array()){
// POST has precedence.
/**
* Search a param on the GET/POST array
* If the key is not found we throw a `required_param_not_found` Exception
*
* @param strig $paramname
* @param ParamType $type
* @param Array $options
*
* @return mixed
* @throws \Exception
*/
public static function requiredParam(
string $parname,
ParamType $type = ParamType::INT,
$options = Array()
): mixed {
if (isset($_POST[$parname])) {
$param = $_POST[$parname];
} else if (isset($_GET[$parname])) {
$param = $_GET[$parname];
} else {
return $default;
throw new \Exception('required_param_not_found');
}
/*$param = self::filter($type, $param);
foreach ($options as $key => $option){
self::$key($param, $option);
}*/
return $param;
}
public static function requiredParamArray($parname, $type, $default = '', $options = Array() ){
// POST has precedence.
/**
* Search a array param on the GET/POST array
* If the key is not found, we throw a `required_param_not_found` Exception
*
* @param strig $paramname
* @param ParamType $type
* @param Array $options
*
* @return mixed
* @throws \Exception
*/
public static function requiredParamArray(
string $parname,
$options = Array()
): mixed {
if (isset($_POST[$parname])) {
$param = $_POST[$parname];
} else if (isset($_GET[$parname])) {
$param = $_GET[$parname];
} else {
throw new \Exception('required_param_not_found');
}
return $param;
}
/**
* Search a array param on the GET/POST array
* If the key is not found we return the default value
*
* If the default $value type is not the same of the $type
* Wwe throw a `default_value_type_missmatch` Exception
*
* @param strig $paramname
* @param ParamType $type
* @param mixed $default
* @param Array $options
*
* @return mixed
* @throws \Exception
*/
public static function optionalParam(
string $parname,
ParamType $type = ParamType::INT,
mixed $default = "",
$options = Array(),
): mixed {
if (isset($_POST[$parname])) {
$param = $_POST[$parname];
} else if (isset($_GET[$parname])) {
@@ -39,52 +98,134 @@ class Request{
return $param;
}
public static function optionalParam($parname, $type, $default = '', $options = Array()){
// POST has precedence.
if (isset($_POST[$parname])) {
$param = $_POST[$parname];
} else if (isset($_GET[$parname])) {
$param = $_GET[$parname];
} else {
return $default;
/**
* NOT IMPLEMENTED YET
* Search a array param on the GET/POST array
*
* @param strig $paramname
* @param Array $default
* @param Array $options
*
* @todo Implement
*
* @return Array
*/
public static function optionalParamArray(
string $parname,
Array $default = Array(),
$options = Array()
): Array{
// Not implemented yet
return $default;
}
/**
* Try to detect if the request came from AJAX/FETCH/POSTMAN
* in this cases, we might want to return something else
*
* @return bool
*/
public static function detectAjaxRequest(): bool
{
if (
isset($_SERVER['HTTP_X_REQUESTED_WITH']) &&
strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest'
) {
return true;
}
return $param;
if (
isset($_SERVER['CONTENT_TYPE']) &&
str_contains(
strtolower($_SERVER['CONTENT_TYPE']),
'application/json'
)
) {
return true;
}
// Detecta ferramentas conhecidas
$userAgent = strtolower($_SERVER['HTTP_USER_AGENT'] ?? '');
if (
str_contains($userAgent, 'postman') ||
str_contains($userAgent, 'curl') ||
str_contains($userAgent, 'insomnia') ||
str_contains($userAgent, 'python') ||
str_contains($userAgent, 'java')
) {
return true;
}
// Headers comuns de navegadores modernos
if (
isset($_SERVER['HTTP_SEC_FETCH_SITE']) ||
isset($_SERVER['HTTP_SEC_FETCH_MODE']) ||
isset($_SERVER['HTTP_REFERER'])
) {
return false;
}
return false;
}
public static function optionalParamArray(){
}
private static function filter($type, $paramname){
/**
*
* @param string $paramname
* @param ParamType $type
* @param bool $cast
*
* @todo Implement casting
* @todo Review this logic, I don'r remember what it does
*
* @return mixed
private static function filter(
$paramname = "",
ParamType $type = ParamType::INT,
$cast = true
): mixed {
switch ($type){
case 'integer':
case ParamType::INT:
if(is_integer($paramname)){
return $paramname;
}
break;
case 'double':
case ParamType::DOUBLE:
if(is_double($paramname)){
return $paramname;
}
break;
case 'alpha':
case ParamType::ALPHA:
return $paramname;
break;
case 'alphanum':
case ParamType::ALPHANUM:
return $paramname;
break;
}
}
*/
/*
* Validations
/**
* NOT IMPLEMENTED YET
* Check if the numeric value is in a number range
*
* Examples:
* From 10 to 35 = inRange($value, "10-35")
* From 45 to infinity = inRange($value, "45-")
* All numbers below 0 = inRange($value, "-0")
*
* @todo Not Implemented yet
*
* @param int|float $value
* @param string $range
*
* @return bool
*/
private static function minVal(){
private static function inRange(
int|float $value,
string $range
): bool {
return true;
}
private static function maxVal(){
}
}
}