From 97ddfd7bff62ed940bb4da8a5abfae930efbc02f Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 17:41:21 +0300 Subject: [PATCH 1/2] Refactoring: Updated How Webservice Class is Constructed --- composer.json | 2 +- .../cli/helpers/CreateWebService.php | 4 +- .../framework/writers/WebServiceWriter.php | 78 ++++++++++++++++--- 3 files changed, 70 insertions(+), 14 deletions(-) diff --git a/composer.json b/composer.json index 96a5873ca..23e08e7ea 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,7 @@ "ext-mbstring": "*", "ext-fileinfo": "*", "ext-openssl": "*", - "webfiori/http":"v3.3.7", + "webfiori/http":"v3.3.8", "webfiori/file":"v1.3.4", "webfiori/jsonx":"v3.3.0", "webfiori/ui":"v2.6.1", diff --git a/webfiori/framework/cli/helpers/CreateWebService.php b/webfiori/framework/cli/helpers/CreateWebService.php index 8a50fa38d..17f4c8f3c 100644 --- a/webfiori/framework/cli/helpers/CreateWebService.php +++ b/webfiori/framework/cli/helpers/CreateWebService.php @@ -13,7 +13,7 @@ use webfiori\framework\cli\commands\CreateCommand; use webfiori\framework\writers\ServiceHolder; use webfiori\framework\writers\WebServiceWriter; -use webfiori\http\ParamTypes; +use webfiori\http\ParamType; use webfiori\http\RequestMethod; use webfiori\http\RequestParameter; @@ -52,7 +52,7 @@ public function readClassInfo() { private function addParamsToService() { do { $paramObj = new RequestParameter('h'); - $paramObj->setType($this->select('Choose parameter type:', ParamTypes::getTypes(), 0)); + $paramObj->setType($this->select('Choose parameter type:', ParamType::getTypes(), 0)); $this->setParamName($paramObj); $added = $this->serviceObj->addParameter($paramObj); $paramObj->setIsOptional($this->confirm('Is this parameter optional?', true)); diff --git a/webfiori/framework/writers/WebServiceWriter.php b/webfiori/framework/writers/WebServiceWriter.php index c206ac457..2b9293745 100644 --- a/webfiori/framework/writers/WebServiceWriter.php +++ b/webfiori/framework/writers/WebServiceWriter.php @@ -12,6 +12,8 @@ use webfiori\framework\EAbstractWebService; use webfiori\http\AbstractWebService; +use webfiori\http\ParamOption; +use webfiori\http\ParamType; use webfiori\http\RequestParameter; /** @@ -49,6 +51,8 @@ public function __construct($webServicesObj = null) { $this->setSuffix('Service'); $this->addUseStatement(EAbstractWebService::class); + $this->addUseStatement(ParamType::class); + $this->addUseStatement(ParamOption::class); $this->servicesObj = new ServiceHolder(); if ($webServicesObj instanceof AbstractWebService) { @@ -142,35 +146,87 @@ public function writeClassComment() { public function writeClassDeclaration() { $this->append('class '.$this->getName().' extends EAbstractWebService {'); } + private function getType(string $type) { + switch ($type) { + case 'int': { + return 'ParamType::INT'; + } + case 'integer': { + return 'ParamType::INT'; + } + case 'string': { + return 'ParamType::STRING'; + } + case 'array': { + return 'ParamType::ARR'; + } + case 'bool': { + return 'ParamType::BOOL'; + } + case 'boolean': { + return 'ParamType::BOOL'; + } + case 'double': { + return 'ParamType::DOUBLE'; + } + case 'email': { + return 'ParamType::EMAIL'; + } + case 'json-obj': { + return 'ParamType::JSON_OBJ'; + } + case 'url': { + return 'ParamType::URL'; + } + } + } /** * * @param RequestParameter $param */ private function appendParam($param) { $this->append("'".$param->getName()."' => [", 3); - $this->append("'type' => '".$param->getType()."',", 4); + + $this->append("ParamOption::TYPE => ".$this->getType($param->getType()).",", 4); if ($param->isOptional()) { - $this->append("'optional' => true,", 4); + $this->append("ParamOption::OPTIONAL => true,", 4); } - + if ($param->getDefault() !== null) { - $toAppend = "'default' => ".$param->getDefault().","; + $toAppend = "ParamOption::DEFAULT => ".$param->getDefault().","; - if (($param->getType() == 'string' || $param->getType() == 'url' || $param->getType() == 'email') && strlen($param->getDefault()) > 0) { - $toAppend = "'default' => '".$param->getDefault()."',"; - } else if ($param->getType() == 'boolean') { - $toAppend = $param->getDefault() === true ? "'default' => true," : "'default' => false,"; + if (($param->getType() == ParamType::STRING || $param->getType() == ParamType::URL || $param->getType() == ParamType::EMAIL) && strlen($param->getDefault()) > 0) { + $toAppend = "ParamOption::DEFAULT => '".$param->getDefault()."',"; + } else if ($param->getType() == ParamType::BOOL) { + $toAppend = $param->getDefault() === true ? "ParamOption::DEFAULT => true," : "ParamOption::DEFAULT => false,"; } $this->append($toAppend, 4); } - if (($param->getType() == 'string' || $param->getType() == 'url' || $param->getType() == 'email') && $param->isEmptyStringAllowed()) { - $this->append("'allow-empty' => true,", 4); + if (($param->getType() == ParamType::STRING || $param->getType() == ParamType::URL || $param->getType() == ParamType::EMAIL)) { + if ($param->isEmptyStringAllowed()) { + $this->append("ParamOption::EMPTY => true,", 4); + } + if ($param->getMinLength() !== null) { + $this->append("ParamOption::MIN_LENGTH => ".$param->getMinLength().",", 4); + } + if ($param->getMaxLength() !== null) { + $this->append("ParamOption::MAX_LENGTH => ".$param->getMaxLength().",", 4); + } + } + + if ($param->getType() == ParamType::INT || $param->getType() == ParamType::DOUBLE) { + if ($param->getMinValue() !== null) { + $this->append("ParamOption::MIN => ".$param->getMinValue().",", 4); + } + if ($param->getMaxValue() !== null) { + $this->append("ParamOption::MAX => ".$param->getMinValue().",", 4); + } } if ($param->getDescription() !== null) { - $this->append("'description' => '".str_replace('\'', '\\\'', $param->getDescription())."',", 4); + $this->append("ParamOption::DESCRIPTION => '".str_replace('\'', '\\\'', $param->getDescription())."',", 4); } $this->append('],', 3); } From 53eeba29e2ae9b4c5c1e1adc47e394da46627442 Mon Sep 17 00:00:00 2001 From: Ibrahim BinAlshikh Date: Wed, 3 Jan 2024 17:45:16 +0300 Subject: [PATCH 2/2] Update WebServiceWritterTest.php --- .../framework/test/writers/WebServiceWritterTest.php | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/tests/webfiori/framework/test/writers/WebServiceWritterTest.php b/tests/webfiori/framework/test/writers/WebServiceWritterTest.php index efbd31daf..08aca7016 100644 --- a/tests/webfiori/framework/test/writers/WebServiceWritterTest.php +++ b/tests/webfiori/framework/test/writers/WebServiceWritterTest.php @@ -18,6 +18,8 @@ public function test00() { $this->assertEquals('Service', $writter->getSuffix()); $this->assertEquals([ "webfiori\\framework\\EAbstractWebService", + "webfiori\http\ParamType", + "webfiori\http\ParamOption" ], $writter->getUseStatements()); } /** @@ -31,6 +33,8 @@ public function test01() { $this->assertEquals('Service', $writter->getSuffix()); $this->assertEquals([ "webfiori\\framework\\EAbstractWebService", + "webfiori\http\ParamType", + "webfiori\http\ParamOption" ], $writter->getUseStatements()); $writter->addRequestParam([ 'name' => 'param-1', @@ -56,6 +60,8 @@ public function test02() { $this->assertEquals('Service', $writter->getSuffix()); $this->assertEquals([ "webfiori\\framework\\EAbstractWebService", + "webfiori\http\ParamType", + "webfiori\http\ParamOption" ], $writter->getUseStatements()); $writter->addRequestParam([ 'name' => 'param-1',