Skip to content

Commit

Permalink
Merge branch 'main' into feature/refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
limenet committed Apr 24, 2024
2 parents 6bd9e08 + 07ccb47 commit 380b5b0
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 15 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "valantic/pimcore-api-documentation",
"version": "0.5.0",
"version": "0.5.1",
"description": "Auto generate API documentation for routes",
"homepage": "https://github.com/valantic/pimcore-api-documentation",
"license": "MIT",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
"@release-it/bumper": "^6.0.0",
"release-it": "^17.2.0"
},
"version": "0.5.0"
"version": "0.5.1"
}
11 changes: 11 additions & 0 deletions src/Model/Component/Property/AbstractPropertyDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,15 @@ public function jsonSerialize(): array

return $data;
}

/**
* @return mixed[]
*/
public function getSchema(): array
{
return [
'type' => $this->getType(),
'nullable' => $this->getNullable(),
];
}
}
12 changes: 12 additions & 0 deletions src/Model/Component/Property/ArrayPropertyDoc.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,16 @@ public function jsonSerialize(): array

return $data;
}

/**
* @return mixed[]
*/
public function getSchema(): array
{
return [
'type' => $this->getType(),
'nullable' => $this->getNullable(),
'items' => $this->getItems(),
];
}
}
52 changes: 52 additions & 0 deletions src/Model/Component/Property/EnumPropertyDoc.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace Valantic\PimcoreApiDocumentationBundle\Model\Component\Property;

class EnumPropertyDoc extends AbstractPropertyDoc
{
/**
* @var array<int, string>
*/
protected array $enumOptions = [];

/**
* @return array<int, string>
*/
public function getEnumOptions(): array
{
return $this->enumOptions;
}

/**
* @param array<int, string> $enumOptions
*/
public function setEnumOptions(array $enumOptions): self
{
$this->enumOptions = $enumOptions;

return $this;
}

public function jsonSerialize(): array
{
$data = parent::jsonSerialize();

$data['enum'] = $this->getEnumOptions();

return $data;
}

/**
* @return mixed[]
*/
public function getSchema(): array
{
return [
'type' => $this->getType(),
'nullable' => $this->getNullable(),
'enum' => $this->getEnumOptions(),
];
}
}
12 changes: 1 addition & 11 deletions src/Service/ControllerMethodParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@
use Valantic\PimcoreApiDocumentationBundle\Http\Request\Contracts\HasJsonPayload;
use Valantic\PimcoreApiDocumentationBundle\Http\Response\ApiResponseInterface;
use Valantic\PimcoreApiDocumentationBundle\Model\Component\Property\AbstractPropertyDoc;
use Valantic\PimcoreApiDocumentationBundle\Model\Component\Property\ArrayPropertyDoc;
use Valantic\PimcoreApiDocumentationBundle\Model\Doc\MethodDoc;
use Valantic\PimcoreApiDocumentationBundle\Model\Doc\Request\ParameterDoc;
use Valantic\PimcoreApiDocumentationBundle\Model\Doc\Request\RequestDoc;
Expand Down Expand Up @@ -175,20 +174,11 @@ private function parseRequest(\ReflectionMethod $method): ?RequestDoc

$propertyDoc = $this->getDataTypeParser($property)->parse($property);

$schema = [
'type' => $propertyDoc->getType(),
'nullable' => $propertyDoc->getNullable(),
];

if ($propertyDoc instanceof ArrayPropertyDoc) {
$schema['items'] = $propertyDoc->getItems();
}

$parameterDoc
->setName($property->getName())
->setIn(ParameterDoc::IN_QUERY)
->setRequired(false)
->setSchema($schema);
->setSchema($propertyDoc->getSchema());

$parsedParameters[] = $parameterDoc;
}
Expand Down
56 changes: 56 additions & 0 deletions src/Service/DataTypeParser/EnumParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
<?php

declare(strict_types=1);

namespace Valantic\PimcoreApiDocumentationBundle\Service\DataTypeParser;

use Valantic\PimcoreApiDocumentationBundle\Contract\Service\DataTypeParserInterface;
use Valantic\PimcoreApiDocumentationBundle\Enum\DataTypeEnum;
use Valantic\PimcoreApiDocumentationBundle\Model\Component\Property\AbstractPropertyDoc;
use Valantic\PimcoreApiDocumentationBundle\Model\Component\Property\EnumPropertyDoc;

/**
* @implements DataTypeParserInterface<EnumPropertyDoc>
*/
class EnumParser implements DataTypeParserInterface
{
public function parse(\ReflectionProperty $reflectionProperty): AbstractPropertyDoc
{
$propertyDoc = new EnumPropertyDoc();

if (!$reflectionProperty->getType() instanceof \ReflectionNamedType) {
return $propertyDoc;
}

$propertyTypeName = $reflectionProperty->getType()->getName();

if (!is_subclass_of($propertyTypeName, \UnitEnum::class)) {
return $propertyDoc;
}

$enumOptions = [];

foreach ($propertyTypeName::cases() as $enumCase) {
if ($enumCase instanceof \BackedEnum) {
$enumOptions[] = (string) $enumCase->value;
} else {
$enumOptions[] = (string) $enumCase->name;
}
}

$propertyDoc
->setName($reflectionProperty->getName())
->setType(DataTypeEnum::STRING->value)
->setEnumOptions($enumOptions)
->setNullable($reflectionProperty->getType()->allowsNull() ?? true);

return $propertyDoc;
}

public function supports(\ReflectionProperty $reflectionProperty): bool
{
return
$reflectionProperty->getType() instanceof \ReflectionNamedType
&& is_subclass_of($reflectionProperty->getType()->getName(), \UnitEnum::class);
}
}

0 comments on commit 380b5b0

Please sign in to comment.