Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring: Updated How Webservice Class is Constructed #202

Merged
merged 2 commits into from
Jan 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number Diff line number Diff line change
Expand Up @@ -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());
}
/**
Expand All @@ -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',
Expand All @@ -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',
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 @@

$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 writeClassDeclaration() {
$this->append('class '.$this->getName().' extends EAbstractWebService {');
}
private function getType(string $type) {
switch ($type) {

Check warning on line 150 in webfiori/framework/writers/WebServiceWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/WebServiceWriter.php#L150

Added line #L150 was not covered by tests
case 'int': {
return 'ParamType::INT';

Check warning on line 152 in webfiori/framework/writers/WebServiceWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/WebServiceWriter.php#L152

Added line #L152 was not covered by tests
}
case 'integer': {
return 'ParamType::INT';
}
case 'string': {
return 'ParamType::STRING';
}
case 'array': {
return 'ParamType::ARR';

Check warning on line 161 in webfiori/framework/writers/WebServiceWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/WebServiceWriter.php#L161

Added line #L161 was not covered by tests
}
case 'bool': {
return 'ParamType::BOOL';

Check warning on line 164 in webfiori/framework/writers/WebServiceWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/WebServiceWriter.php#L164

Added line #L164 was not covered by tests
}
case 'boolean': {
return 'ParamType::BOOL';
}
case 'double': {
return 'ParamType::DOUBLE';

Check warning on line 170 in webfiori/framework/writers/WebServiceWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/WebServiceWriter.php#L169-L170

Added lines #L169 - L170 were not covered by tests
}
case 'email': {
return 'ParamType::EMAIL';

Check warning on line 173 in webfiori/framework/writers/WebServiceWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/WebServiceWriter.php#L172-L173

Added lines #L172 - L173 were not covered by tests
}
case 'json-obj': {
return 'ParamType::JSON_OBJ';

Check warning on line 176 in webfiori/framework/writers/WebServiceWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/WebServiceWriter.php#L175-L176

Added lines #L175 - L176 were not covered by tests
}
case 'url': {
return 'ParamType::URL';

Check warning on line 179 in webfiori/framework/writers/WebServiceWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/WebServiceWriter.php#L178-L179

Added lines #L178 - L179 were not covered by tests
}
}
}
/**
*
* @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);

Check warning on line 212 in webfiori/framework/writers/WebServiceWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/WebServiceWriter.php#L212

Added line #L212 was not covered by tests
}
if ($param->getMaxLength() !== null) {
$this->append("ParamOption::MAX_LENGTH => ".$param->getMaxLength().",", 4);

Check warning on line 215 in webfiori/framework/writers/WebServiceWriter.php

View check run for this annotation

Codecov / codecov/patch

webfiori/framework/writers/WebServiceWriter.php#L215

Added line #L215 was not covered by tests
}
}

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
Loading