Skip to content

Commit

Permalink
Enum support
Browse files Browse the repository at this point in the history
  • Loading branch information
Bedram Tamang committed Oct 19, 2022
1 parent d7a24f2 commit 7c44e6c
Show file tree
Hide file tree
Showing 27 changed files with 145 additions and 123 deletions.
28 changes: 0 additions & 28 deletions .github/workflows/php-pint.yml

This file was deleted.

7 changes: 3 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"scripts": {
"test": "vendor/bin/phpunit",
"psalm": "vendor/bin/psalm",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage",
"format": "vendor/bin/pint"
"pint": "vendor/bin/pint",
"test-coverage": "vendor/bin/phpunit --coverage-html coverage"
},
"autoload-dev": {
"psr-4": {
Expand All @@ -46,8 +46,7 @@
"require-dev": {
"orchestra/testbench": "^6.15",
"phpunit/phpunit": "^9.0",
"vimeo/psalm": "^4.7",
"laravel/pint": "^1.2"
"vimeo/psalm": "^4.7"
},
"extra": {
"laravel": {
Expand Down
7 changes: 5 additions & 2 deletions config/api-generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@
return [
'servers' => [
'url' => 'https://{environment}.localhost.test',

'variables' => [
'environment' => [

'default' => 'api',

'enum' => [
'api',
'api.dev',
Expand All @@ -16,8 +19,8 @@
],

'openapi' => '3.0.0',
'title' => 'Reaching APP API',
'title' => 'Reaching APP API',
'version' => '1.0.0',

'file-path' => storage_path('api-docs/api-docs.json'),
'file-path' => public_path('api-docs/api-docs.json'),
];
11 changes: 0 additions & 11 deletions pint.json

This file was deleted.

20 changes: 15 additions & 5 deletions src/Parser/HasPropertyType.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Illuminate\Support\Arr;
use JoBins\APIGenerator\Rules\ArrayRule;
use JoBins\APIGenerator\Rules\EnumRule;
use JoBins\APIGenerator\Rules\FileRule;
use JoBins\APIGenerator\Rules\IntegerRule;

Expand All @@ -22,15 +23,16 @@ trait HasPropertyType
/**
* @param array $rules
* @param string $name
*
* @return array|string[]
*/
public function getPropertyType(array $rules, string $name): array
{
if (ArrayRule::check($rules)) {
if ( ArrayRule::check($rules) ) {
$rules = Arr::get($this->rulesArray, "{$name}.*");

return [
'type' => 'array',
'type' => 'array',
'items' => $this->getPropertyItemType($rules),
];
}
Expand All @@ -48,21 +50,29 @@ public function setRulesArray(array $rulesArray): void

private function getPropertyItemType(array $rules): array
{
if (FileRule::check($rules)) {
if ( FileRule::check($rules) ) {
$this->contentType = 'multipart/form-data';

return [
'type' => 'string',
'type' => 'string',
'format' => 'binary',
];
}

if (IntegerRule::check($rules)) {
if ( IntegerRule::check($rules) ) {
return [
'type' => 'integer',
];
}

$enums = EnumRule::data($rules);
if ( count($enums) > 0 ) {
return [
'type' => 'string',
'enum' => $enums,
];
}

return [
'type' => 'string',
];
Expand Down
18 changes: 10 additions & 8 deletions src/Parser/RequestBodyComponent.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,14 @@ public function getRules(object $class): array
return [];
}

return collect($class->rules())->map(function ($item) {
if (! is_array($item)) {
return explode('|', $item);
return collect($class->rules())->map(function ($rules) {
if (! is_array($rules)) {
return explode('|', $rules);
}

return $item;
return collect($rules)->filter(function ($item) {
return is_string($item);
});
})->toArray();
}

Expand All @@ -61,10 +63,10 @@ public function getDescriptions(object $class): array

public function getParseSchema(array $request): array
{
$data = [];
$data = [];
$data['schema'] = [
'type' => 'object',
'required' => $this->getRequired($request),
'type' => 'object',
'required' => $this->getRequired($request),
'properties' => $this->getProperties($request),
];

Expand All @@ -82,7 +84,7 @@ public function parseRequestBodies(): array
}

/** @var FormRequest $class */
$class = (new $className);
$class = new $className;

// Set rules of a class for global access.
$rules = $this->getRules($class);
Expand Down
32 changes: 32 additions & 0 deletions src/Rules/EnumRule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

namespace JoBins\APIGenerator\Rules;

use Illuminate\Support\Str;

class EnumRule implements RuleContract
{
public static function check(array $rules): bool
{
foreach ($rules as $rule) {
if (Str::contains($rule, 'in')) {
return true;
}
}

return false;
}

public static function data(array $rules): array
{
foreach ($rules as $rule) {
if (Str::contains($rule, 'in')) {
$enumRule = Str::replace('in:', '', $rule);

return explode(',', $enumRule);
}
}

return [];
}
}
3 changes: 0 additions & 3 deletions src/Rules/FileRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,6 @@ public static function check(array $rules): bool
$rule = (string) $rule;
}

if (! is_string($rule)) {
continue;
}
if (in_array($rule, self::CONTAIN)) {
return true;
}
Expand Down
4 changes: 0 additions & 4 deletions src/Rules/IntegerRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,6 @@ class IntegerRule implements RuleContract
*/
public static function check(array $rules): bool
{
$rules = collect($rules)->filter(function ($item) {
return is_string($item);
})->toArray();

if (count(array_intersect(self::CONTAIN, $rules)) > 0) {
return true;
}
Expand Down
4 changes: 0 additions & 4 deletions src/Rules/RequiredRule.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,6 @@ class RequiredRule
*/
public static function check(array $rules): bool
{
$rules = collect($rules)->filter(function ($item) {
return is_string($item);
})->toArray();

if (count(array_intersect(self::CONTAIN, $rules)) == 0) {
return false;
}
Expand Down
4 changes: 2 additions & 2 deletions src/Security/Bearer.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Bearer
public function getSchema()
{
return [
'name' => 'barerToken',
'type' => 'http',
'name' => 'barerToken',
'type' => 'http',
'scheme' => 'bearer',
];
}
Expand Down
12 changes: 6 additions & 6 deletions src/Security/HasParameter.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ trait HasParameter
*/
public function preparePathWithParam(): array
{
$url = Arr::get($this->request, 'url');
$url = Arr::get($this->request, 'url');
$method = Arr::get($this->request, 'method');

$route = Route::getRoutes()->match(Request::create($url, $method));
Expand All @@ -39,8 +39,8 @@ public function processQuery(array $queries): array

return collect($queries)->map(function ($value, $param) use ($definitions) {
return [
'in' => 'query',
'name' => $param,
'in' => 'query',
'name' => $param,
'schema' => [
'type' => is_numeric($value) ? 'integer' : 'string',
],
Expand All @@ -59,12 +59,12 @@ public function processParameters($parameters): array

return collect($parameters)->map(function ($value, $param) use ($definitions) {
return [
'in' => 'path',
'name' => $param,
'in' => 'path',
'name' => $param,
'schema' => [
'type' => is_numeric($value) ? 'integer' : 'string',
],
'required' => true,
'required' => true,
'description' => Arr::get($definitions, $param) ?? ' ',
];
})->toArray();
Expand Down
12 changes: 6 additions & 6 deletions src/Security/HasResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ public function processResponse()

if (Str::contains($attribute, '*') && $refSchemaName) {
return [
'type' => 'array',
'type' => 'array',
'items' => [
'$ref' => "#/components/schemas/{$refSchemaName}",
],
Expand All @@ -42,7 +42,7 @@ public function processResponse()
}

return [
'type' => 'string',
'type' => 'string',
'description' => $properties,
];
})->toArray();
Expand All @@ -52,7 +52,7 @@ public function parseResponse($originalResponseData)
{
$code = $this->response->getStatusCode();

$schema = [];
$schema = [];
$properties = $this->processResponse();
if (! empty($properties)) {
$schema['properties'] = $this->processResponse();
Expand All @@ -66,7 +66,7 @@ public function parseResponse($originalResponseData)
$responseData = [
$code => [
'description' => "{$code} status response",
'content' => [
'content' => [
'application/json' => [
'schema' => $schema,
],
Expand All @@ -80,7 +80,7 @@ public function parseResponse($originalResponseData)
private function defineSchema(string $name, string $type, array $properties)
{
$schemaData = [
'type' => $type,
'type' => $type,
'items' => [
'type' => 'object',
],
Expand All @@ -94,7 +94,7 @@ private function getSchemaProperties($properties)
{
return collect($properties)->map(function ($definition) {
return [
'type' => 'string',
'type' => 'string',
'description' => $definition,
];
})->toArray();
Expand Down
2 changes: 1 addition & 1 deletion src/Security/HasSecurity.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public function ensureSecuritySchemaExists(array $schema): void
}

data_set($this->data, $key, [
'type' => 'http',
'type' => 'http',
'scheme' => 'bearer',
]);
}
Expand Down
8 changes: 4 additions & 4 deletions src/Services/Generator.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,8 @@ public function __construct()
$this->data = array_merge($this->data, [
'servers' => $this->processServer(config()->get('api-generator')),
'openapi' => config()->get('api-generator.openapi'),
'info' => [
'title' => config()->get('api-generator.title'),
'info' => [
'title' => config()->get('api-generator.title'),
'version' => config()->get('api-generator.version'),
],
]);
Expand Down Expand Up @@ -117,8 +117,8 @@ public function getBasicPathInfoData($responseData)
private function parseParam()
{
[$url, $parameters] = $this->preparePathWithParam();
$method = $this->request['method'];
$pathKey = "paths.{$url}.{$method}";
$method = $this->request['method'];
$pathKey = "paths.{$url}.{$method}";

$pathData = Arr::get($this->data, $pathKey, []);
$pathData = $pathData + $this->getBasicPathInfo($pathData, $parameters);
Expand Down
4 changes: 2 additions & 2 deletions src/Services/ProcessRequestTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,9 @@ public function setRequest(array $request): self

private function parseRequestBody(): ?array
{
$requestClass = app($this->request);
$className = Arr::get($this->request, 'rule');

if (! ($className = Arr::get($this->request, 'rule'))) {
if (empty($className)) {
return null;
}

Expand Down
Loading

0 comments on commit 7c44e6c

Please sign in to comment.