Skip to content

Commit

Permalink
Merge pull request #6 from Spacegame66/pathParams
Browse files Browse the repository at this point in the history
feat(brackets): remove brackets and replace them by colons
  • Loading branch information
profclems authored Feb 22, 2021
2 parents aa3dce4 + d1f7ee2 commit 11e3928
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 12 deletions.
5 changes: 3 additions & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@
},
"minimum-stability": "dev",
"require": {
"php": ">=7",
"ext-json": "*"
"php": ">=7.1",
"ext-json": "*",
"ext-mbstring": "*"
},
"extra": {
"laravel": {
Expand Down
60 changes: 50 additions & 10 deletions src/ExportPostmanCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function uuidv4($data = null) : string
* Execute the console command.
*
* @return mixed
* @throws Exception
*/
public function handle() : void
{
Expand Down Expand Up @@ -107,13 +108,14 @@ public function handle() : void
//GETTING @PARAMs @VARs @DESCRIPTIONs from PhpDoc comments
$p = $this->getParams($route);
//dd($p);

if(empty($route->middleware())) {
continue;
}

//API ROUTES
if ($this->option('api') && "api" == $route->middleware()[0]) {
$data = $this->processPathParams($route);
$routes['item'][] = [
'name' => $method . ' | ' . $route->uri(),
'request' => [
Expand All @@ -131,8 +133,9 @@ public function handle() : void
'raw' => '{\n \n}',
],
'url' => [
'raw' => $url . '/' . $route->uri(),
'host' => $url . '/' . $route->uri(),
'raw' => $url . '/' . $data['url'],
'host' => $url . '/' . $data['url'],
'variable' => $data['variables'],
'query' => $p['paramsArray']??null,
],
'description' => $p['description']??null,
Expand All @@ -141,15 +144,10 @@ public function handle() : void
];
} else if ($this->option('web') && "web" == $route->middleware()[0]) {
//WEB ROUTES
$data = $this->processPathParams($route);
$routes['item'][] = [
'name' => $method . ' | ' . $route->uri(),
'request' => [
'url' => $url . '/' . $route->uri(),
'params' => [
'key' => '',
'value' => '',
'description' => '',
],
'method' => strtoupper($method),
'header' => [
[
Expand All @@ -162,6 +160,16 @@ public function handle() : void
'mode' => 'raw',
'raw' => '{\n \n}',
],
'url' => [
'raw' => $url . '/' . $data['url'],
'host' => $url . '/' . $data['url'],
'variable' => $data['variables'],
],
'params' => [
'key' => '',
'value' => '',
'description' => '',
],
'description' => $p['description']??null,
],
'response' => [],
Expand All @@ -183,6 +191,10 @@ public function handle() : void
}
}

/**
* @param $route
* @return array|false
*/
public function getParams($route) {
if (empty($route->action['controller'])) {
return false;
Expand Down Expand Up @@ -286,7 +298,12 @@ public function getParams($route) {
return $p;
}

public function cleanString($string) {
/**
* @param $string
* @return string
*/
public function cleanString($string): string
{
$string = str_replace('*', '', $string); // Replaces
$string = str_replace('#', '', $string); // Replaces
$string = str_replace(' ', '', $string); // Replaces
Expand All @@ -297,4 +314,27 @@ public function cleanString($string) {
return trim($string);
}

/**
* @param $route
* @return array
*/
public function processPathParams($route): array
{
$matches = [];
$variables = [];
$regex = '/{([^{}:?]+):?[^{}:?]*(\??)}/';
preg_match_all($regex, $route->uri(), $matches, PREG_SET_ORDER);
$clean_url = preg_replace($regex, ":$1", $route->uri());
foreach ($matches as $match) {
$variables[] = [
'key' => $match[1],
'description' => $match[2] === '?' ? '(Optional)' : '(Required)',
];
}

return [
'url' => $clean_url,
'variables' => $variables
];
}
}

0 comments on commit 11e3928

Please sign in to comment.