From 0d2882459b71c70dc528c94bda9f7de996bd8832 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 17 Jan 2024 00:32:17 +0300 Subject: [PATCH 1/4] Fix a Bug in Setting Min and Max Floating Value --- webfiori/http/RequestParameter.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/webfiori/http/RequestParameter.php b/webfiori/http/RequestParameter.php index a35e317..82e1aed 100644 --- a/webfiori/http/RequestParameter.php +++ b/webfiori/http/RequestParameter.php @@ -686,10 +686,10 @@ public function setType(string $type) : bool { if (in_array($sType, ParamType::getTypes())) { $this->type = $sType; - 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 == ParamType::INT || $sType == ParamType::DOUBLE) { + if ($sType == ParamType::DOUBLE) { + $this->maxVal = defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.7976931348623E+308; + $this->minVal = defined('PHP_FLOAT_MIN') ? PHP_FLOAT_MIN : 2.2250738585072E-308; + } else if ($sType == ParamType::INT) { $this->minVal = defined('PHP_INT_MIN') ? PHP_INT_MIN : ~PHP_INT_MAX; $this->maxVal = PHP_INT_MAX; } else { From bf7ecf5abd33353cd1ed60fb2c3d96a9488b5f3f Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 17 Jan 2024 00:35:02 +0300 Subject: [PATCH 2/4] Run CS Fixer --- webfiori/http/AbstractWebService.php | 22 +++++----- webfiori/http/ParamOption.php | 44 +++++++++---------- webfiori/http/RequestMethod.php | 33 +++++++------- webfiori/http/ResponseMessage.php | 64 ++++++++++++++-------------- webfiori/http/WebServicesManager.php | 44 ++++++++++--------- 5 files changed, 104 insertions(+), 103 deletions(-) diff --git a/webfiori/http/AbstractWebService.php b/webfiori/http/AbstractWebService.php index 97394d5..a53d276 100644 --- a/webfiori/http/AbstractWebService.php +++ b/webfiori/http/AbstractWebService.php @@ -40,7 +40,7 @@ abstract class AbstractWebService implements JsonI { * @since 1.0.2 */ const I = 'info'; - + /** * The name of the service. * @@ -320,16 +320,6 @@ public final function addRequestMethod(string $method) : bool { return false; } - /** - * Adds multiple request methods as one group. - * - * @param array $methods - */ - public function setRequestMethods(array $methods) { - foreach ($methods as $m) { - $this->addRequestMethod($m); - } - } /** * Adds response description. * @@ -815,6 +805,16 @@ public final function setName(string $name) : bool { return false; } + /** + * Adds multiple request methods as one group. + * + * @param array $methods + */ + public function setRequestMethods(array $methods) { + foreach ($methods as $m) { + $this->addRequestMethod($m); + } + } /** * Sets version number or name at which the service was added to a manager. * diff --git a/webfiori/http/ParamOption.php b/webfiori/http/ParamOption.php index c4d60c7..a9609c2 100644 --- a/webfiori/http/ParamOption.php +++ b/webfiori/http/ParamOption.php @@ -16,27 +16,22 @@ */ class ParamOption { /** - * Parameter name option. Applies to all data types. - */ - const NAME = 'name'; - /** - * Parameter type option. Applies to all data types. + * An option which is used to set default value if parameter is optional and + * not provided. */ - const TYPE = 'type'; + const DEFAULT = 'default'; /** - * An option which is used to indicate that a parameter is optional or not (bool). Applies to all data types. + * An option which is used to set a description for the parameter */ - const OPTIONAL = 'optional'; + const DESCRIPTION = 'description'; /** - * An option which is used to set default value if parameter is optional and - * not provided. + * An option which is used to allow empty strings as values. Applicable to string only. */ - const DEFAULT = 'default'; + const EMPTY = 'allow-empty'; /** - * An option which is used to set minimum allowed value. Applicable to numerical - * types only. + * An option which is used to set custom filtering function on the parameter. */ - const MIN = 'min'; + const FILTER = 'custom-filter'; /** * An option which is used to set maximum allowed value. Applicable to numerical * types only. @@ -45,21 +40,26 @@ class ParamOption { /** * An option which is used to set minimum allowed length. Applicable to string types only. */ - const MIN_LENGTH = 'min-length'; + const MAX_LENGTH = 'max-length'; + /** + * An option which is used to set minimum allowed value. Applicable to numerical + * types only. + */ + const MIN = 'min'; /** * An option which is used to set minimum allowed length. Applicable to string types only. */ - const MAX_LENGTH = 'max-length'; + const MIN_LENGTH = 'min-length'; /** - * An option which is used to set custom filtering function on the parameter. + * Parameter name option. Applies to all data types. */ - const FILTER = 'custom-filter'; + const NAME = 'name'; /** - * An option which is used to set a description for the parameter + * An option which is used to indicate that a parameter is optional or not (bool). Applies to all data types. */ - const DESCRIPTION = 'description'; + const OPTIONAL = 'optional'; /** - * An option which is used to allow empty strings as values. Applicable to string only. + * Parameter type option. Applies to all data types. */ - const EMPTY = 'allow-empty'; + const TYPE = 'type'; } diff --git a/webfiori/http/RequestMethod.php b/webfiori/http/RequestMethod.php index 7356607..0a90841 100644 --- a/webfiori/http/RequestMethod.php +++ b/webfiori/http/RequestMethod.php @@ -1,5 +1,4 @@ messages = [ + '401' => 'Not Authorized.', + '404-1' => 'The following parameter(s) has invalid values: ', + '404-2' => 'The following required parameter(s) where missing from the request body: ', + '404-3' => 'Service name is not set.', + '404-4' => 'Service not implemented.', + '404-5' => 'Service not supported.', + '404-6' => 'Request methods of the service are not set in code.', + '405' => 'Method Not Allowed.', + '415' => 'Content type not supported.' + ]; + } /** + * Returns the value of response message given its code. * - * @return ResponseMessage + * @param string $code The code of the message such as 415. + * + * @return string If the code has an error message set, the method will + * return it. Other than that, the string '-' is returned. */ - private static function getInstance() : ResponseMessage { - if (self::$inst === null) { - self::$inst = new ResponseMessage(); + public static function get(string $code) : string { + $tr = trim($code); + + if (isset(self::getInstance()->messages[$tr])) { + return self::getInstance()->messages[$tr]; } - return self::$inst; + + return '-'; } /** * Sets a custom HTTP response message for specific error code. @@ -59,33 +78,14 @@ public static function set(string $code, string $message) { self::getInstance()->messages[trim($code)] = $message; } /** - * Returns the value of response message given its code. - * - * @param string $code The code of the message such as 415. * - * @return string If the code has an error message set, the method will - * return it. Other than that, the string '-' is returned. + * @return ResponseMessage */ - public static function get(string $code) : string { - $tr = trim($code); - - if (isset(self::getInstance()->messages[$tr])) { - return self::getInstance()->messages[$tr]; + private static function getInstance() : ResponseMessage { + if (self::$inst === null) { + self::$inst = new ResponseMessage(); } - - return '-'; - } - private function __construct() { - $this->messages = [ - '401' => 'Not Authorized.', - '404-1' => 'The following parameter(s) has invalid values: ', - '404-2' => 'The following required parameter(s) where missing from the request body: ', - '404-3' => 'Service name is not set.', - '404-4' => 'Service not implemented.', - '404-5' => 'Service not supported.', - '404-6' => 'Request methods of the service are not set in code.', - '405' => 'Method Not Allowed.', - '415' => 'Content type not supported.' - ]; + + return self::$inst; } } diff --git a/webfiori/http/WebServicesManager.php b/webfiori/http/WebServicesManager.php index 8d9d5ef..c2d00c5 100644 --- a/webfiori/http/WebServicesManager.php +++ b/webfiori/http/WebServicesManager.php @@ -816,30 +816,10 @@ public function toJSON() : Json { return $json; } - private function isAuth(AbstractWebService $service) { - if ($service->isAuthRequired()) { - $isAuthCheck = 'isAuthorized'.Request::getMethod(); - if (!method_exists($service, $isAuthCheck)) { - return $service->isAuthorized() === null || $service->isAuthorized(); - } - return $service->$isAuthCheck() === null || $service->$isAuthCheck(); - } - return true; - } - private function processService(AbstractWebService $service) { - $processMethod = 'process'.Request::getMethod(); - - if (!method_exists($service, $processMethod)) { - $service->processRequest(); - } else { - $service->$processMethod(); - } - } private function _AfterParamsCheck($processReq) { if ($processReq) { - $service = $this->getServiceByName($this->getCalledServiceName()); - + if ($this->isAuth($service)) { $this->processService($service); @@ -1016,6 +996,28 @@ private function getAction() { return $retVal; } + private function isAuth(AbstractWebService $service) { + if ($service->isAuthRequired()) { + $isAuthCheck = 'isAuthorized'.Request::getMethod(); + + if (!method_exists($service, $isAuthCheck)) { + return $service->isAuthorized() === null || $service->isAuthorized(); + } + + return $service->$isAuthCheck() === null || $service->$isAuthCheck(); + } + + return true; + } + private function processService(AbstractWebService $service) { + $processMethod = 'process'.Request::getMethod(); + + if (!method_exists($service, $processMethod)) { + $service->processRequest(); + } else { + $service->$processMethod(); + } + } private function setOutputStreamHelper($trimmed, $mode) : bool { $tempStream = fopen($trimmed, $mode); From 9a2209d3213541154ab4d1eb1a8faae8461ac3c5 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 17 Jan 2024 00:38:17 +0300 Subject: [PATCH 3/4] Update RequestParameterTest.php --- tests/webfiori/tests/http/RequestParameterTest.php | 9 ++------- 1 file changed, 2 insertions(+), 7 deletions(-) diff --git a/tests/webfiori/tests/http/RequestParameterTest.php b/tests/webfiori/tests/http/RequestParameterTest.php index 42c0089..1636fe6 100644 --- a/tests/webfiori/tests/http/RequestParameterTest.php +++ b/tests/webfiori/tests/http/RequestParameterTest.php @@ -192,13 +192,8 @@ public function testConstructor07() { $this->assertFalse($requestParam->isEmptyStringAllowed()); $this->assertTrue($requestParam->isOptional()); - if (PHP_MAJOR_VERSION >= 7 && PHP_MINOR_VERSION >= 2) { - $this->assertEquals(PHP_FLOAT_MAX, $requestParam->getMaxValue()); - $this->assertEquals(PHP_FLOAT_MIN,$requestParam->getMinValue()); - } else { - $this->assertEquals(PHP_INT_MAX, $requestParam->getMaxValue()); - $this->assertEquals(~PHP_INT_MAX,$requestParam->getMinValue()); - } + $this->assertEquals(defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.7976931348623157E+308, $requestParam->getMaxValue()); + $this->assertEquals(defined('PHP_FLOAT_MIN') ? PHP_FLOAT_MIN : 2.2250738585072E-308,$requestParam->getMinValue()); $this->assertNull($requestParam->getDefault()); $this->assertNull($requestParam->getDescription()); $this->assertNull($requestParam->getCustomFilterFunction()); From 981b5655257cdddfd6ec37579a1929cde1264698 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 17 Jan 2024 00:41:24 +0300 Subject: [PATCH 4/4] Update RequestParameterTest.php --- tests/webfiori/tests/http/RequestParameterTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/webfiori/tests/http/RequestParameterTest.php b/tests/webfiori/tests/http/RequestParameterTest.php index 1636fe6..1c2220f 100644 --- a/tests/webfiori/tests/http/RequestParameterTest.php +++ b/tests/webfiori/tests/http/RequestParameterTest.php @@ -192,7 +192,7 @@ public function testConstructor07() { $this->assertFalse($requestParam->isEmptyStringAllowed()); $this->assertTrue($requestParam->isOptional()); - $this->assertEquals(defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.7976931348623157E+308, $requestParam->getMaxValue()); + $this->assertEquals(defined('PHP_FLOAT_MAX') ? PHP_FLOAT_MAX : 1.7976931348623E+308, $requestParam->getMaxValue()); $this->assertEquals(defined('PHP_FLOAT_MIN') ? PHP_FLOAT_MIN : 2.2250738585072E-308,$requestParam->getMinValue()); $this->assertNull($requestParam->getDefault()); $this->assertNull($requestParam->getDescription());