Skip to content

Commit 85f5f42

Browse files
authored
Merge pull request #47 from DoclerLabs/readd-enum-consts
readd enum consts
2 parents a4084a4 + c78ca47 commit 85f5f42

File tree

17 files changed

+165
-1
lines changed

17 files changed

+165
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,10 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [6.1.0] - 2021-08-27
8+
### Added
9+
- Readded enums as consts only
10+
711
## [6.0.2] - 2021-08-12
812
### Fixed
913
- The array of serializers in BodySerializer depends on both requests and responses content type

example/gen/src/Request/FindPetsByStatusRequest.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
class FindPetsByStatusRequest implements RequestInterface
1414
{
15+
public const STATUS_AVAILABLE = 'available';
16+
17+
public const STATUS_PENDING = 'pending';
18+
19+
public const STATUS_SOLD = 'sold';
20+
1521
private ?string $status = null;
1622

1723
private string $contentType = '';

example/gen/src/Schema/Order.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@
1313

1414
class Order implements SerializableInterface, JsonSerializable
1515
{
16+
public const STATUS_PLACED = 'placed';
17+
18+
public const STATUS_APPROVED = 'approved';
19+
20+
public const STATUS_DELIVERED = 'delivered';
21+
1622
private ?int $id = null;
1723

1824
private ?int $petId = null;

example/gen/src/Schema/Pet.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,12 @@
1212

1313
class Pet implements SerializableInterface, JsonSerializable
1414
{
15+
public const STATUS_AVAILABLE = 'available';
16+
17+
public const STATUS_PENDING = 'pending';
18+
19+
public const STATUS_SOLD = 'sold';
20+
1521
private ?int $id = null;
1622

1723
private string $name;

example/test-example.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
use OpenApi\PetStoreClient\SwaggerPetstoreOpenAPI3ClientFactory;
1616

1717
// https://petstore3.swagger.io/api/v3/openapi.json
18-
$client = new Client(['base_uri' => 'https://petstore.swagger.io/v2/pet']);
18+
$client = new Client(['base_uri' => 'https://petstore.swagger.io/v2/']);
1919
$factory = new SwaggerPetstoreOpenAPI3ClientFactory();
2020
$client = $factory->create($client);
2121

src/Entity/Field.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ class Field
2323
private bool $nullable;
2424
private ?Field $arrayItem = null;
2525
private array $objectProperties = [];
26+
private array $enumValues = [];
2627
private string $format = '';
2728
/** @phpstan-ignore-next-line cannot use strict type before PHP8 with "mixed" pseudo type */
2829
private $default;
@@ -74,6 +75,18 @@ public function setObjectProperties(array $objectProperties): self
7475
return $this;
7576
}
7677

78+
public function getEnumValues(): ?array
79+
{
80+
return $this->enumValues;
81+
}
82+
83+
public function setEnumValues(array $enumValues): self
84+
{
85+
$this->enumValues = $enumValues;
86+
87+
return $this;
88+
}
89+
7790
public function getFormat(): ?string
7891
{
7992
return $this->format;

src/Generator/MutatorAccessorClassGeneratorAbstract.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
use DoclerLabs\ApiClientGenerator\Entity\Constraint\ConstraintInterface;
1111
use DoclerLabs\ApiClientGenerator\Entity\Field;
1212
use DoclerLabs\ApiClientGenerator\Input\Specification;
13+
use DoclerLabs\ApiClientGenerator\Naming\SchemaNaming;
1314
use DoclerLabs\ApiClientGenerator\Output\Php\PhpFileCollection;
1415
use PhpParser\Node\Stmt\ClassMethod;
1516
use PhpParser\Node\Stmt\Property;
@@ -98,6 +99,23 @@ protected function getGetMethodName(Field $field): string
9899
return sprintf('get%s', ucfirst($field->getPhpVariableName()));
99100
}
100101

102+
protected function generateEnumStatements(Field $field): array
103+
{
104+
$statements = [];
105+
$enumValues = $field->getEnumValues();
106+
if (!empty($enumValues)) {
107+
foreach ($enumValues as $enumValue) {
108+
$constName = SchemaNaming::getEnumConstName($field, $enumValue);
109+
$statements[] = $this->builder->constant(
110+
$constName,
111+
$this->builder->val($enumValue)
112+
);
113+
}
114+
}
115+
116+
return $statements;
117+
}
118+
101119
protected function generateConstraints(Field $root): array
102120
{
103121
$stmts = [];

src/Generator/RequestGenerator.php

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function generateRequest(PhpFileCollection $fileRegistry, Operation $o
3636
$classBuilder = $this->builder
3737
->class($className)
3838
->implement('RequestInterface')
39+
->addStmts($this->generateEnums($request))
3940
->addStmts($this->generateProperties($request))
4041
->addStmt($this->generateConstructor($request))
4142
->addStmt($this->generateGetContentType())
@@ -47,6 +48,20 @@ protected function generateRequest(PhpFileCollection $fileRegistry, Operation $o
4748
$this->registerFile($fileRegistry, $classBuilder, self::SUBDIRECTORY, self::NAMESPACE_SUBPATH);
4849
}
4950

51+
protected function generateEnums(Request $request): array
52+
{
53+
$statements = [];
54+
foreach ($request->getFields() as $fields) {
55+
foreach ($fields as $field) {
56+
foreach ($this->generateEnumStatements($field) as $statement) {
57+
$statements[] = $statement;
58+
}
59+
}
60+
}
61+
62+
return $statements;
63+
}
64+
5065
protected function generateProperties(Request $request): array
5166
{
5267
$statements = [];

src/Generator/SchemaGenerator.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ protected function generateSchema(Field $field, PhpFileCollection $fileRegistry)
3636
$classBuilder = $this->builder
3737
->class($className)
3838
->implement('SerializableInterface', 'JsonSerializable')
39+
->addStmts($this->generateEnumConsts($field))
3940
->addStmts($this->generateProperties($field))
4041
->addStmt($this->generateConstructor($field))
4142
->addStmts($this->generateSetMethods($field))
@@ -46,6 +47,18 @@ protected function generateSchema(Field $field, PhpFileCollection $fileRegistry)
4647
$this->registerFile($fileRegistry, $classBuilder, self::SUBDIRECTORY, self::NAMESPACE_SUBPATH);
4748
}
4849

50+
protected function generateEnumConsts(Field $root): array
51+
{
52+
$statements = [];
53+
foreach ($root->getObjectProperties() as $propertyField) {
54+
foreach ($this->generateEnumStatements($propertyField) as $statement) {
55+
$statements[] = $statement;
56+
}
57+
}
58+
59+
return $statements;
60+
}
61+
4962
protected function generateProperties(Field $root): array
5063
{
5164
$statements = [];

src/Input/Factory/FieldFactory.php

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,11 @@ public function create(
118118
$field->setArrayItem($arrayItem);
119119
} elseif (!empty($objectProperties)) {
120120
$field->setObjectProperties($objectProperties);
121+
} elseif (isset($schema->enum)) {
122+
if (!FieldType::isSpecificationTypeString($type)) {
123+
throw new InvalidSpecificationException('Only string enum fields are currently supported');
124+
}
125+
$field->setEnumValues($schema->enum);
121126
}
122127

123128
if (isset($schema->format)) {

0 commit comments

Comments
 (0)