Skip to content

Commit

Permalink
Fix duplicate slash problem
Browse files Browse the repository at this point in the history
  • Loading branch information
M1ke committed Aug 28, 2024
1 parent 61ca194 commit fee6ef4
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 65 deletions.
10 changes: 7 additions & 3 deletions src/ClassGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ abstract class ClassGenerator {
* Creates a generator for a specific namespace
*/
public function __construct(string $namespace_name){
$this->namespace_name = $this->stringNotEndWith($namespace_name, '\\');
$this->namespace_name = self::stringNotEndWith($namespace_name, '\\');
}

protected function namespaceModel(): string{
Expand Down Expand Up @@ -105,10 +105,14 @@ protected function dumpParentInternal(string $dir, string $file, string $namespa
file_put_contents("$dir/$file_name", $content);
}

protected function stringNotEndWith(string $string, string $char): string{
protected static function stringNotEndWith(string $string, string $char): string{
return $string[strlen($string)-1]===$char ? substr($string, 0, -1) : $string;
}

protected static function stringNotBeginWith(string $string, string $char): string{
return $string[0]===$char ? substr($string, 1) : $string;
}

protected function unPlural(string $string): string{
if (substr($string, -3)==='ies'){
return substr($string, 0, -3).'y';
Expand All @@ -132,7 +136,7 @@ protected function typeFromRef(array $property): string{
}

protected function dirNamespace(string $dir, string $namespace): string{
$dir = $this->stringNotEndWith($dir, '/');
$dir = self::stringNotEndWith($dir, '/');

return "$dir/$namespace";
}
Expand Down
93 changes: 31 additions & 62 deletions src/GenerateRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,12 @@ class GenerateRequests extends ClassGenerator {

/**
* @param string $file_path
* @param bool $more_specificity
* @param bool $more_specificity
* @throws InvalidArgumentException
*/
public function generate(string $file_path, bool $more_specificity = false): void{
public function generate(string $file_path, bool $more_specificity = false): void{
$api = Yaml::parseFile($file_path);
$base_uri = $this->stringNotEndWith($api['basePath'], '/');
$base_uri = self::stringNotEndWith($api['basePath'], '/');
foreach ($api['paths'] as $path => $path_details){
if ($more_specificity){
$adapted_path = $this->pathWithParamsNoPlaceholders($path);
Expand All @@ -40,10 +40,17 @@ public function generate(string $file_path, bool $more_specificity = false): vo
$class = new ClassType($class_name);
$class->setExtends(self::REQUEST_CLASS_NAME);
$class->addComment($method_details['summary']);
$uri = empty($base_uri) ? $path : "{$base_uri}/{$path}";
$class->addConstant('URI', $uri);
$class->addConstant('METHOD', strtoupper($method));

if ($base_uri){
$path = self::stringNotBeginWith($path, '/');
$uri = "{$base_uri}/{$path}";
}
else {
$uri = $path;
}
$class->addConstant('URI', $uri);

$this->handleParams($method_details, $class);

$this->handleResponse($method_details, $class);
Expand All @@ -53,11 +60,6 @@ public function generate(string $file_path, bool $more_specificity = false): vo
}
}

/**
* @param string $path
*
* @return string
*/
private function pathToCamelCase(string $path): string{
$path = str_replace('/', '-', $path);

Expand All @@ -66,15 +68,10 @@ private function pathToCamelCase(string $path): string{
$item = ucfirst($item);
}
unset($item);
$path = implode('', $path_arr);

return $path;
return implode('', $path_arr);
}

/**
* @param string $path
* @return string
*/
private function pathNoParams(string $path): string{
$path = substr($path, 1);

Expand All @@ -86,35 +83,23 @@ private function pathNoParams(string $path): string{
}
$path_no_params[] = $item;
}
$path = implode('/', $path_no_params);

return $path;
return implode('/', $path_no_params);
}

/**
* @param string $path
*
* @return string
*/
private function pathWithParamsNoPlaceholders(string $path){
private function pathWithParamsNoPlaceholders(string $path): string{
$path = substr($path, 1);

$path = explode('/', $path);
$parts = explode('/', $path);
$path_with_params = [];
foreach ($path as $item){
$item = str_replace('{', '', $item);
$item = str_replace('}', '', $item);
foreach ($parts as $item){
$item = str_replace(['{', '}'], '', $item);
$path_with_params[] = $item;
}
$path = implode('/', $path_with_params);

return $path;
return implode('/', $path_with_params);
}

/**
* @param array $path_params
* @param ClassType $class
*/
private function handlePathParams(array $path_params, ClassType $class): void{
if (empty($path_params)){
return;
Expand All @@ -135,7 +120,7 @@ private function handlePathParams(array $path_params, ClassType $class): void{
$type = $path_param['type'] ?? 'null';
$is_nullable = ($path_param['nullable'] ?? null)===true;
if ($is_nullable && $type!=='null'){
$type = "?{$type}";
$type = "?{$type}";
}

$class->addProperty($param_name)
Expand All @@ -147,16 +132,12 @@ private function handlePathParams(array $path_params, ClassType $class): void{

if (!empty($param_names)){
$class->addProperty('path_params')
->setStatic(true)
->setStatic()
->setValue($param_names)
->setVisibility('protected');
}
}

/**
* @param array $query_params
* @param ClassType $class
*/
private function handleQueryParams(array $query_params, ClassType $class): void{
if (empty($query_params)){
return;
Expand All @@ -174,11 +155,11 @@ private function handleQueryParams(array $query_params, ClassType $class): void{
$method = $class->addMethod('set'.$this->pathToCamelCase($query_param['name']));
}

$type = $path_param['type'] ?? 'null';
$is_nullable = ($path_param['nullable'] ?? null)===true;
if ($is_nullable && $type!=='null'){
$type = "?{$type}";
}
$type = $path_param['type'] ?? 'null';
$is_nullable = ($path_param['nullable'] ?? null)===true;
if ($is_nullable && $type!=='null'){
$type = "?{$type}";
}

$method->addParameter($param_name);
$class->addProperty($param_name)
Expand All @@ -193,23 +174,19 @@ private function handleQueryParams(array $query_params, ClassType $class): void{
$query_params_property = $class->getProperty('query_params');
$query_params_array = $query_params_property->getValue();
}
catch (InvalidArgumentException $e){
catch (InvalidArgumentException $e) {
$query_params_property = $class->addProperty('query_params')
->setStatic(true)
->setStatic()
->setVisibility('protected');
$query_params_array = [];
}
$value = array_merge($query_params_array, $param_names);
$query_params_property->setStatic(true)
$query_params_property->setStatic()
->setValue($value)
->setVisibility('protected');
}
}

/**
* @param array $method_details
* @param ClassType $class
*/
protected function handleParams(array $method_details, ClassType $class): void{
$parameters = $method_details['parameters'] ?? [];
$path_params = $query_params = [];
Expand All @@ -232,31 +209,25 @@ protected function handleParams(array $method_details, ClassType $class): void{
}

/**
* @param string $dir
* @throws RuntimeException
* @throws FileNotFoundException
*/
public function saveClasses(string $dir) :void{
public function saveClasses(string $dir): void{
$dir = $this->dirNamespace($dir, self::NAMESPACE_REQUEST);
$use_ns = $this->namespaceModel();
$use = "use $use_ns\\SwaggerModel;\n";
$this->saveClassesInternal($dir, $this->namespaceRequest(), $use);
}

/**
* @param string $dir
* @throws FileNotFoundException
*/
public function dumpParentClass(string $dir) :void{
public function dumpParentClass(string $dir): void{
$dir = $this->dirNamespace($dir, self::NAMESPACE_REQUEST);
$this->dumpParentInternal($dir, __DIR__.'/SwaggerRequest.php', $this->namespaceRequest());
$this->dumpParentInternal($dir, __DIR__.'/SwaggerClient.php', $this->namespaceRequest(), $this->namespaceModel());
}

/**
* @param array $parameter
* @param ClassType $class
*/
private function handleBodyParam(array $parameter, ClassType $class): void{
$schema = $parameter['schema'];
if (empty($schema)){
Expand All @@ -275,8 +246,6 @@ private function handleBodyParam(array $parameter, ClassType $class): void{
}

/**
* @param array $method_details
* @param ClassType $class
* @throws InvalidArgumentException
*/
protected function handleResponse(array $method_details, ClassType $class): void{
Expand Down

0 comments on commit fee6ef4

Please sign in to comment.