Skip to content

Commit

Permalink
Refactoring: Updated How Webservice Class is Constructed
Browse files Browse the repository at this point in the history
  • Loading branch information
usernane committed Jan 3, 2024
1 parent 62204be commit 97ddfd7
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 14 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
4 changes: 2 additions & 2 deletions webfiori/framework/cli/helpers/CreateWebService.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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));
Expand Down
78 changes: 67 additions & 11 deletions webfiori/framework/writers/WebServiceWriter.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@

use webfiori\framework\EAbstractWebService;
use webfiori\http\AbstractWebService;
use webfiori\http\ParamOption;
use webfiori\http\ParamType;
use webfiori\http\RequestParameter;

/**
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
Expand Down

0 comments on commit 97ddfd7

Please sign in to comment.