Skip to content

Commit

Permalink
✨ Convert to Enum value if it is an Enum class string.
Browse files Browse the repository at this point in the history
  • Loading branch information
k2tzumi committed Sep 2, 2022
1 parent 0e79cb9 commit 820b159
Showing 1 changed file with 71 additions and 1 deletion.
72 changes: 71 additions & 1 deletion src/Processors/ExpandEnums.php
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
<?php declare(strict_types=1);
<?php
declare(strict_types=1);

/**
* @license Apache 2.0
*/

namespace OpenApi\Processors;

use InvalidArgumentException;
use OpenApi\Analysis;
use OpenApi\Annotations\Items as AnnotationsItems;
use OpenApi\Annotations\JsonContent as AnnotationsJsonContent;
use OpenApi\Annotations\Property as AnnotationsProperty;
use OpenApi\Annotations\Schema as AnnotationSchema;
use OpenApi\Annotations\XmlContent as AnnotationsXmlContent;
use OpenApi\Attributes\AdditionalProperties as AttributesAdditionalProperties;
use OpenApi\Annotations\AdditionalProperties as AnnotationsAdditionalProperties;
use OpenApi\Attributes\Items as AttributesItems;
use OpenApi\Attributes\JsonContent as AttributesJsonContent;
use OpenApi\Attributes\Property as AttributesProperty;
use OpenApi\Attributes\Schema as AttributeSchema;
use OpenApi\Attributes\ServerVariable as AttributesServerVariable;
use OpenApi\Annotations\ServerVariable as AnnotationsServerVariable;
use OpenApi\Attributes\XmlContent as AttributesXmlContent;
use OpenApi\Generator;

/**
Expand All @@ -20,12 +34,27 @@ class ExpandEnums
{
use Concerns\TypesTrait;

/**
* @param Analysis $analysis
* @return void
* @throws \ReflectionException
*/
public function __invoke(Analysis $analysis)
{
if (!class_exists('\\ReflectionEnum')) {
return;
}

$this->expandContextEnum($analysis);
$this->expandSchemaEnum($analysis);
}

/**
* @param Analysis $analysis
* @return void
* @throws \ReflectionException
*/
private function expandContextEnum(Analysis $analysis): void {
/** @var AnnotationSchema[] $schemas */
$schemas = $analysis->getAnnotationsOfType([AnnotationSchema::class, AttributeSchema::class], true);

Expand Down Expand Up @@ -55,4 +84,45 @@ public function __invoke(Analysis $analysis)
}
}
}

/**
* @param Analysis $analysis
* @return void
*/
private function expandSchemaEnum(Analysis $analysis): void {
/** @var AnnotationSchema[] $schemas */
$schemas = $analysis->getAnnotationsOfType([
AnnotationsAdditionalProperties::class,
AttributesAdditionalProperties::class,
AnnotationsItems::class,
AttributesItems::class,
AnnotationsJsonContent::class,
AttributesJsonContent::class,
AnnotationsProperty::class,
AttributesProperty::class,
AnnotationSchema::class,
AttributeSchema::class,
AttributesServerVariable::class,
AnnotationsServerVariable::class,
AnnotationsXmlContent::class,
AttributesXmlContent::class
], true);

foreach ($schemas as $schema) {
if ($schema->enum !== Generator::UNDEFINED && is_string($schema->enum)) {
$source = $schema->enum;
// Convert to Enum value if it is an Enum class string
if (is_a($schema->enum, 'UnitEnum', true)) {
$enums = [];
foreach ($source::cases() as $case) {
$enums[] = $case->value ?? $case->name;
}

$schema->enum = $enums;
} else {
throw new InvalidArgumentException("Unexpected enum value, requires specifying the Enum class string: $source");
}
}
}
}
}

0 comments on commit 820b159

Please sign in to comment.