Skip to content

Commit

Permalink
Merge pull request #51 from WebFiori/dev
Browse files Browse the repository at this point in the history
Fix a Bug in Setting Min and Max Floating Value
  • Loading branch information
usernane authored Jan 16, 2024
2 parents 7f137a3 + 981b565 commit e1efcc5
Show file tree
Hide file tree
Showing 7 changed files with 110 additions and 114 deletions.
9 changes: 2 additions & 7 deletions tests/webfiori/tests/http/RequestParameterTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.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());
$this->assertNull($requestParam->getCustomFilterFunction());
Expand Down
22 changes: 11 additions & 11 deletions webfiori/http/AbstractWebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ abstract class AbstractWebService implements JsonI {
* @since 1.0.2
*/
const I = 'info';

/**
* The name of the service.
*
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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.
*
Expand Down
44 changes: 22 additions & 22 deletions webfiori/http/ParamOption.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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';
}
33 changes: 16 additions & 17 deletions webfiori/http/RequestMethod.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace webfiori\http;

/**
Expand All @@ -9,59 +8,59 @@
*/
class RequestMethod {
/**
* A constant which is used to represent 'GET' request method.
* A constant which is used to represent 'connect' request method.
*
* @var string
*/
const GET = 'GET';
const CONNECT = 'CONNECT';
/**
* A constant which is used to represent 'GET' request method.
* A constant which is used to represent 'delete' request method.
*
* @var string
*/
const POST = 'POST';
const DELETE = 'DELETE';
/**
* A constant which is used to represent 'PUT' request method.
* A constant which is used to represent 'GET' request method.
*
* @var string
*/
const PUT = 'PUT';
const GET = 'GET';
/**
* A constant which is used to represent 'head' request method.
*
* @var string
*/
const HEAD = 'HEAD';
/**
* A constant which is used to represent 'patch' request method.
* A constant which is used to represent 'options' request method.
*
* @var string
*/
const PATCH = 'PATCH';
const OPTIONS = 'OPTIONS';
/**
* A constant which is used to represent 'delete' request method.
* A constant which is used to represent 'patch' request method.
*
* @var string
*/
const DELETE = 'DELETE';
const PATCH = 'PATCH';
/**
* A constant which is used to represent 'trace' request method.
* A constant which is used to represent 'GET' request method.
*
* @var string
*/
const TRACE = 'TRACE';
const POST = 'POST';
/**
* A constant which is used to represent 'options' request method.
* A constant which is used to represent 'PUT' request method.
*
* @var string
*/
const OPTIONS = 'OPTIONS';
const PUT = 'PUT';
/**
* A constant which is used to represent 'connect' request method.
* A constant which is used to represent 'trace' request method.
*
* @var string
*/
const CONNECT = 'CONNECT';
const TRACE = 'TRACE';
/**
* Returns an array that holds request methods in upper case.
*
Expand Down
8 changes: 4 additions & 4 deletions webfiori/http/RequestParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
64 changes: 32 additions & 32 deletions webfiori/http/ResponseMessage.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

namespace webfiori\http;

/**
Expand All @@ -20,17 +19,37 @@
* @author Ibrahim
*/
class ResponseMessage {
private $messages;
private static $inst;
private $messages;
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.'
];
}
/**
* 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.
Expand Down Expand Up @@ -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;
}
}
44 changes: 23 additions & 21 deletions webfiori/http/WebServicesManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit e1efcc5

Please sign in to comment.