Skip to content

Commit

Permalink
Rename ParamTypes to ParamType
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jan 3, 2024
1 parent b25b52f commit 62b6f61
Show file tree
Hide file tree
Showing 8 changed files with 50 additions and 50 deletions.
2 changes: 1 addition & 1 deletion tests/loader.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'HttpHeader.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'HttpCookie.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'HeadersPool.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'ParamTypes.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'ParamType.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'ParamOption.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'AbstractWebService.php';
require_once $rootDir.'webfiori'.$DS.'http'.$DS.'APIFilter.php';
Expand Down
4 changes: 2 additions & 2 deletions tests/webfiori/tests/http/RequestParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use PHPUnit\Framework\TestCase;
use webfiori\http\APIFilter;
use webfiori\http\ParamOption;
use webfiori\http\ParamTypes;
use webfiori\http\ParamType;
use webfiori\http\RequestParameter;
/**
* Description of RequestParameterTest
Expand Down Expand Up @@ -58,7 +58,7 @@ public function testCreateParameter02() {
public function testCreateParameter03() {
$param = RequestParameter::create([
'name'=>'ok',
ParamOption::TYPE => ParamTypes::STRING,
ParamOption::TYPE => ParamType::STRING,
ParamOption::DEFAULT => 'Ibrahim',
ParamOption::EMPTY => true,
ParamOption::DESCRIPTION => 'Super param.'
Expand Down
4 changes: 2 additions & 2 deletions tests/webfiori/tests/http/WebServiceTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

use PHPUnit\Framework\TestCase;
use webfiori\http\ParamOption;
use webfiori\http\ParamTypes;
use webfiori\http\ParamType;
use webfiori\http\RequestMethod;
use webfiori\http\RequestParameter;
use webfiori\tests\http\testServices\NoAuthService;
Expand Down Expand Up @@ -75,7 +75,7 @@ public function testAddParameter01() {
$action = new TestServiceObj('add-user');
$this->assertTrue($action->addParameter([
ParamOption::NAME => 'new-param',
ParamOption::TYPE => ParamTypes::BOOL
ParamOption::TYPE => ParamType::BOOL
]));

$this->assertEquals(1,count($action->getParameters()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

use Exception;
use webfiori\http\ParamOption;
use webfiori\http\ParamTypes;
use webfiori\http\ParamType;
use webfiori\http\RequestMethod;
use webfiori\http\RequestParameter;
use webfiori\json\Json;
Expand All @@ -21,13 +21,13 @@ public function __construct() {
$this->getParameterByName('id')->setIsOptional(true);
$this->addParameters([
'name' => [
ParamOption::TYPE => ParamTypes::STRING
ParamOption::TYPE => ParamType::STRING
],
'username' => [
ParamOption::TYPE => ParamTypes::STRING
ParamOption::TYPE => ParamType::STRING
],
'x' => [
ParamOption::TYPE => ParamTypes::INT,
ParamOption::TYPE => ParamType::INT,
ParamOption::OPTIONAL => true,
ParamOption::DEFAULT => 3
]
Expand Down
52 changes: 26 additions & 26 deletions webfiori/http/APIFilter.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,21 +82,21 @@ public function addRequestParameter(RequestParameter $reqParam) {
}
$paramType = $reqParam->getType();

if ($paramType == ParamTypes::INT) {
if ($paramType == ParamType::INT) {
$attribute[$filterIdx][] = FILTER_VALIDATE_INT;
$this->checkNumericRange($reqParam, $attribute);
} else if ($paramType == ParamTypes::STRING) {
} else if ($paramType == ParamType::STRING) {
$attribute[$optIdx][$optIdx]['allow-empty'] = $reqParam->isEmptyStringAllowed();
$attribute[$filterIdx][] = FILTER_DEFAULT;
$this->checkStringLength($reqParam, $attribute);
} else if ($paramType == ParamTypes::DOUBLE) {
} else if ($paramType == ParamType::DOUBLE) {
$attribute[$filterIdx][] = FILTER_VALIDATE_FLOAT;
$this->checkNumericRange($reqParam, $attribute);
} else if ($paramType == ParamTypes::EMAIL) {
} else if ($paramType == ParamType::EMAIL) {
$attribute[$filterIdx][] = FILTER_SANITIZE_EMAIL;
$attribute[$filterIdx][] = FILTER_VALIDATE_EMAIL;
$this->checkStringLength($reqParam, $attribute);
} else if ($paramType == ParamTypes::URL) {
} else if ($paramType == ParamType::URL) {
$attribute[$filterIdx][] = FILTER_SANITIZE_URL;
$attribute[$filterIdx][] = FILTER_VALIDATE_URL;
$this->checkStringLength($reqParam, $attribute);
Expand Down Expand Up @@ -176,7 +176,7 @@ public static function filter(APIFilter $apiFilter, array $arr): array {
} else {
$retVal[$filteredIdx][$name] = self::applyBasicFilterOnly($def, $toBeFiltered);
}
$booleanCheck = $paramType == ParamTypes::BOOL && $retVal[$filteredIdx][$name] === true || $retVal[$filteredIdx][$name] === false;
$booleanCheck = $paramType == ParamType::BOOL && $retVal[$filteredIdx][$name] === true || $retVal[$filteredIdx][$name] === false;

if (!$booleanCheck && $retVal[$filteredIdx][$name] == self::INVALID && $defaultVal !== null) {
$retVal[$filteredIdx][$name] = $defaultVal;
Expand Down Expand Up @@ -347,32 +347,32 @@ private static function applyBasicFilterOnly($def,$toBeFiltered) {
$paramType = $paramObj->getType();
$optIdx = 'options';

if ($paramType == ParamTypes::BOOL) {
if ($paramType == ParamType::BOOL) {
$returnVal = self::filterBoolean($toBeFiltered);
} else if ($paramType == ParamTypes::ARR) {
} else if ($paramType == ParamType::ARR) {
$returnVal = self::filterArray(filter_var($toBeFiltered));
} else {
$returnVal = filter_var($toBeFiltered);

foreach ($def['filters'] as $val) {
$returnVal = filter_var($returnVal, $val, $def[$optIdx]);

if ($paramType == ParamTypes::DOUBLE) {
if ($paramType == ParamType::DOUBLE) {
$returnVal = self::minMaxValueCheck($returnVal, $def['options']['options']);
}

if (in_array($paramType, ParamTypes::getStringTypes())) {
if (in_array($paramType, ParamType::getStringTypes())) {
$returnVal = self::minMaxLengthCheck($returnVal, $def['options']['options']);
}
}

if ($returnVal === false ||
(($paramType == ParamTypes::URL || $paramType == ParamTypes::EMAIL) && strlen($returnVal) == 0) ||
(($paramType == ParamTypes::INT || $paramType == ParamTypes::DOUBLE) && strlen($returnVal) == 0)) {
(($paramType == ParamType::URL || $paramType == ParamType::EMAIL) && strlen($returnVal) == 0) ||
(($paramType == ParamType::INT || $paramType == ParamType::DOUBLE) && strlen($returnVal) == 0)) {
$returnVal = self::INVALID;
}

if ($paramType == ParamTypes::STRING &&
if ($paramType == ParamType::STRING &&
$returnVal != self::INVALID &&
strlen($returnVal) == 0 &&
$def['options']['options']['allow-empty'] === false) {
Expand Down Expand Up @@ -401,7 +401,7 @@ private static function applyCustomFilterFunc($def, $toBeFiltered) {
$returnVal = $filterFuncResult;
}

if ($returnVal === false && $paramObj->getType() != ParamTypes::BOOL) {
if ($returnVal === false && $paramObj->getType() != ParamType::BOOL) {
$returnVal = self::INVALID;
}

Expand All @@ -417,16 +417,16 @@ private function applyJsonBasicFilter(Json $extraClean, $toBeFiltered, $def) {
$toBeFiltered = strip_tags($toBeFiltered);
}

if ($paramType == $toBeFilteredType || $toBeFilteredType == 'object' && $paramType == ParamTypes::JSON_OBJ) {
if ($paramType == ParamTypes::BOOL) {
if ($paramType == $toBeFilteredType || $toBeFilteredType == 'object' && $paramType == ParamType::JSON_OBJ) {
if ($paramType == ParamType::BOOL) {
$extraClean->addBoolean($name, $toBeFiltered);
} else if ($paramType == ParamTypes::DOUBLE || $paramType == ParamTypes::INT) {
} else if ($paramType == ParamType::DOUBLE || $paramType == ParamType::INT) {
$extraClean->addNumber($name, $toBeFiltered);
} else if ($paramType == 'string') {
$this->cleanJsonStr($extraClean, $def, $toBeFiltered);
} else if ($paramType == ParamTypes::ARR) {
} else if ($paramType == ParamType::ARR) {
$extraClean->addArray($name, $this->cleanJsonArray($toBeFiltered, true));
} else if ($paramType == ParamTypes::JSON_OBJ) {
} else if ($paramType == ParamType::JSON_OBJ) {
if ($toBeFiltered instanceof Json) {
$extraClean->add($name, $toBeFiltered);
} else {
Expand Down Expand Up @@ -607,7 +607,7 @@ private static function filterArray($arr) {

if ($result['parsed'] === true) {
$x = $result['end'];
$arrayValues[] = filter_var(strip_tags($result[ParamTypes::STRING]));
$arrayValues[] = filter_var(strip_tags($result[ParamType::STRING]));
$tmpArrValue = '';
continue;
} else {
Expand Down Expand Up @@ -709,7 +709,7 @@ private function filterJson(Json $toBeCleaned) {
if (isset($def[$optIdx]['filter-func'])) {
$filteredValue = self::applyCustomFilterFunc($def, $toBeFiltered);

if ($paramType == ParamTypes::STRING &&
if ($paramType == ParamType::STRING &&
$filteredValue != self::INVALID &&
strlen($filteredValue) == 0 &&
$def[$optIdx][$optIdx]['allow-empty'] === false) {
Expand All @@ -736,21 +736,21 @@ private static function getBasicFilterResultForCustomFilter($def, $toBeFiltered)
}
$paramType = $def['parameter']->getType();

if ($paramType == ParamTypes::BOOL) {
if ($paramType == ParamType::BOOL) {
$filteredValue = self::filterBoolean(filter_var($toBeFiltered));
} else if ($paramType == ParamTypes::ARR) {
} else if ($paramType == ParamType::ARR) {
$filteredValue = self::filterArray($toBeFiltered);
} else {
$filteredValue = filter_var($toBeFiltered);

foreach ($def['filters'] as $val) {
$filteredValue = filter_var($filteredValue, $val, $def['options']);

if ($paramType == ParamTypes::DOUBLE) {
if ($paramType == ParamType::DOUBLE) {
$filteredValue = self::minMaxValueCheck($filteredValue, $def['options']['options']);
}

if (in_array($paramType, ParamTypes::getStringTypes())) {
if (in_array($paramType, ParamType::getStringTypes())) {
$filteredValue = self::minMaxLengthCheck($filteredValue, $def['options']['options']);
}
}
Expand All @@ -759,7 +759,7 @@ private static function getBasicFilterResultForCustomFilter($def, $toBeFiltered)
$filteredValue = self::INVALID;
}

if ($paramType == ParamTypes::STRING &&
if ($paramType == ParamType::STRING &&
$filteredValue != self::INVALID &&
strlen($filteredValue) == 0 &&
$def['options']['options']['allow-empty'] === false) {
Expand Down
2 changes: 1 addition & 1 deletion webfiori/http/ManagerInfoService.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public function __construct() {
.'under given manager.');
$this->addParameter([
ParamOption::NAME => 'version',
ParamOption::TYPE => ParamTypes::STRING,
ParamOption::TYPE => ParamType::STRING,
ParamOption::OPTIONAL => true,
ParamOption::DESCRIPTION => 'Optional parameter. '
.'If set, the information that will be returned will be specific '
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
*
* @since 1.5.2
*/
class ParamTypes {
class ParamType {
/**
* A constant to indicate that a parameter is of type array.
*
Expand Down
26 changes: 13 additions & 13 deletions webfiori/http/RequestParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -451,9 +451,9 @@ public function setDefault($val) : bool {
$RPType = $this->getType();

if ($valType == $RPType ||
($valType == 'integer' && $RPType == ParamTypes::DOUBLE) ||
($valType == 'double' && $RPType == ParamTypes::DOUBLE) ||
(($RPType == ParamTypes::EMAIL || $RPType == ParamTypes::URL) && $valType == ParamTypes::STRING) ||
($valType == 'integer' && $RPType == ParamType::DOUBLE) ||
($valType == 'double' && $RPType == ParamType::DOUBLE) ||
(($RPType == ParamType::EMAIL || $RPType == ParamType::URL) && $valType == ParamType::STRING) ||
($val instanceof Json && $RPType == 'json-obj')) {
$this->default = $val;

Expand Down Expand Up @@ -490,7 +490,7 @@ public function setDescription(string $desc) {
* @since 1.2.1
*/
public function setIsEmptyStringAllowed(bool $bool) : bool {
if ($this->getType() == ParamTypes::STRING) {
if ($this->getType() == ParamType::STRING) {
//in php 5.6, primitive type hinting is not allowed.
//this will resolve the issue.
$this->isEmptyStrAllowed = $bool;
Expand Down Expand Up @@ -529,7 +529,7 @@ public function setIsOptional(bool $bool) {
public function setMaxLength(int $val) : bool {
$type = $this->getType();

if (in_array($type, ParamTypes::getStringTypes())) {
if (in_array($type, ParamType::getStringTypes())) {
$min = $this->getMinLength() === null ? 0 : $this->getMinLength();

if ($val >= $min && $val > 0) {
Expand Down Expand Up @@ -561,11 +561,11 @@ public function setMaxLength(int $val) : bool {
public function setMaxValue(float $val) : bool {
$type = $this->getType();

if (in_array($type, ParamTypes::getNumericTypes())) {
if (in_array($type, ParamType::getNumericTypes())) {
$min = $this->getMinValue();

if ($min !== null && $val > $min) {
$this->maxVal = $type === ParamTypes::INT ? intval($val) : $val;
$this->maxVal = $type === ParamType::INT ? intval($val) : $val;

return true;
}
Expand All @@ -592,7 +592,7 @@ public function setMaxValue(float $val) : bool {
public function setMinLength(int $val) : bool {
$type = $this->getType();

if (in_array($type, ParamTypes::getStringTypes())) {
if (in_array($type, ParamType::getStringTypes())) {
$max = $this->getMaxLength() === null ? PHP_INT_MAX : $this->getMaxLength();

if ($val <= $max && $val > 0) {
Expand Down Expand Up @@ -623,11 +623,11 @@ public function setMinLength(int $val) : bool {
public function setMinValue(float $val) : bool {
$type = $this->getType();

if (in_array($type, ParamTypes::getNumericTypes())) {
if (in_array($type, ParamType::getNumericTypes())) {
$max = $this->getMaxValue();

if ($max !== null && $val < $max) {
$this->minVal = $type == ParamTypes::INT ? intval($val) : $val;
$this->minVal = $type == ParamType::INT ? intval($val) : $val;

return true;
}
Expand Down Expand Up @@ -683,13 +683,13 @@ public function setType(string $type) : bool {
$sType = 'integer';
}

if (in_array($sType, ParamTypes::getTypes())) {
if (in_array($sType, ParamType::getTypes())) {
$this->type = $sType;

if ($sType == ParamTypes::DOUBLE && PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 2) {
if ($sType == ParamType::DOUBLE && PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 2) {
$this->maxVal = PHP_FLOAT_MAX;
$this->minVal = PHP_FLOAT_MIN;
} else if ($sType == ParamTypes::INT || $sType == ParamTypes::DOUBLE) {
} else if ($sType == ParamType::INT || $sType == ParamType::DOUBLE) {
$this->minVal = defined('PHP_INT_MIN') ? PHP_INT_MIN : ~PHP_INT_MAX;
$this->maxVal = PHP_INT_MAX;
} else {
Expand Down

0 comments on commit 62b6f61

Please sign in to comment.