Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
import org.apache.commons.lang3.StringUtils;
import org.openapitools.codegen.CliOption;
import org.openapitools.codegen.CodegenConstants;
import org.openapitools.codegen.CodegenModel;
import org.openapitools.codegen.CodegenType;
import org.openapitools.codegen.SupportingFile;
import org.openapitools.codegen.meta.GeneratorMetadata;
import org.openapitools.codegen.meta.Stability;
import org.openapitools.codegen.meta.features.*;
import org.openapitools.codegen.model.ModelMap;
import org.openapitools.codegen.model.ModelsMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand All @@ -33,6 +36,7 @@
import java.util.Collections;
import java.util.EnumSet;
import java.util.List;
import java.util.Map;

public class PhpNextgenClientCodegen extends AbstractPhpCodegen {
@SuppressWarnings("hiding")
Expand Down Expand Up @@ -122,4 +126,32 @@ public void processOpts() {
supportingFiles.add(new SupportingFile(".phplint.mustache", "", ".phplint.yml"));
supportingFiles.add(new SupportingFile("git_push.sh.mustache", "", "git_push.sh"));
}

@Override
public Map<String, ModelsMap> postProcessAllModels(Map<String, ModelsMap> objs) {
final Map<String, ModelsMap> processed = super.postProcessAllModels(objs);

for (Map.Entry<String, ModelsMap> entry : processed.entrySet()) {
entry.setValue(postProcessModelsMap(entry.getValue()));
}

return processed;
}

private ModelsMap postProcessModelsMap(ModelsMap objs) {
for (ModelMap m : objs.getModels()) {
CodegenModel model = m.getModel();

if (model.isEnum) {
for (Map<String, Object> enumVars : (List<Map<String, Object>>) model.getAllowableValues().get("enumVars")) {
if ((Boolean) enumVars.get("isString")) {
model.vendorExtensions.putIfAbsent("x-php-enum-type", "string");
} else {
model.vendorExtensions.putIfAbsent("x-php-enum-type", "int");
}
}
}
}
return objs;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -79,12 +79,10 @@ class ObjectSerializer
$getter = $data::getters()[$property];
$value = $data->$getter();
if ($value !== null && !in_array($openAPIType, [{{&primitives}}], true)) {
$callable = [$openAPIType, 'getAllowableEnumValues'];
if (is_callable($callable)) {
/** array $callable */
$allowedEnumTypes = $callable();
if (!in_array($value, $allowedEnumTypes, true)) {
$imploded = implode("', '", $allowedEnumTypes);
if ($openAPIType instanceof \BackedEnum) {
$data = $openAPIType::tryFrom($data);
if ($data === null) {
$imploded = implode("', '", array_map(fn($case) => $case->value, $openAPIType::cases()));
throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
}
}
Expand Down Expand Up @@ -488,9 +486,10 @@ class ObjectSerializer
}


if (method_exists($class, 'getAllowableEnumValues')) {
if (!in_array($data, $class::getAllowableEnumValues(), true)) {
$imploded = implode("', '", $class::getAllowableEnumValues());
if ($class instanceof \BackedEnum) {
$data = $class::tryFrom($data);
if ($data === null) {
$imploded = implode("', '", array_map(fn($case) => $case->value, $class::cases()));
throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'");
}
return $data;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,28 +1,9 @@
class {{classname}}
enum {{classname}}: {{vendorExtensions.x-php-enum-type}}
{
/**
* Possible values of this enum
*/
{{#allowableValues}}
{{#enumVars}}
public const {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}};
case {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}};

{{/enumVars}}
{{/allowableValues}}
/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
{{#allowableValues}}
{{#enumVars}}
self::{{^isString}}NUMBER_{{/isString}}{{{name}}}{{^-last}},
{{/-last}}
{{/enumVars}}
{{/allowableValues}}

];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,14 @@
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class EnumClass
enum EnumClass: string
{
/**
* Possible values of this enum
*/
public const ABC = '_abc';
case ABC = '_abc';

public const EFG = '-efg';
case EFG = '-efg';

public const XYZ = '(xyz)';
case XYZ = '(xyz)';

/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::ABC,
self::EFG,
self::XYZ
];
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,14 @@
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class OuterEnum
enum OuterEnum: string
{
/**
* Possible values of this enum
*/
public const PLACED = 'placed';
case PLACED = 'placed';

public const APPROVED = 'approved';
case APPROVED = 'approved';

public const DELIVERED = 'delivered';
case DELIVERED = 'delivered';

/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::PLACED,
self::APPROVED,
self::DELIVERED
];
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,14 @@
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class OuterEnumDefaultValue
enum OuterEnumDefaultValue: string
{
/**
* Possible values of this enum
*/
public const PLACED = 'placed';
case PLACED = 'placed';

public const APPROVED = 'approved';
case APPROVED = 'approved';

public const DELIVERED = 'delivered';
case DELIVERED = 'delivered';

/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::PLACED,
self::APPROVED,
self::DELIVERED
];
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,14 @@
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class OuterEnumInteger
enum OuterEnumInteger: int
{
/**
* Possible values of this enum
*/
public const NUMBER_0 = 0;
case NUMBER_0 = 0;

public const NUMBER_1 = 1;
case NUMBER_1 = 1;

public const NUMBER_2 = 2;
case NUMBER_2 = 2;

/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::NUMBER_0,
self::NUMBER_1,
self::NUMBER_2
];
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,14 @@
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class OuterEnumIntegerDefaultValue
enum OuterEnumIntegerDefaultValue: int
{
/**
* Possible values of this enum
*/
public const NUMBER_0 = 0;
case NUMBER_0 = 0;

public const NUMBER_1 = 1;
case NUMBER_1 = 1;

public const NUMBER_2 = 2;
case NUMBER_2 = 2;

/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::NUMBER_0,
self::NUMBER_1,
self::NUMBER_2
];
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -37,26 +37,12 @@
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/
class SingleRefType
enum SingleRefType: string
{
/**
* Possible values of this enum
*/
public const ADMIN = 'admin';

public const USER = 'user';

/**
* Gets allowable values of the enum
* @return string[]
*/
public static function getAllowableEnumValues()
{
return [
self::ADMIN,
self::USER
];
}
case ADMIN = 'admin';

case USER = 'user';

}


Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,10 @@ public static function sanitizeForSerialization($data, $type = null, $format = n
$getter = $data::getters()[$property];
$value = $data->$getter();
if ($value !== null && !in_array($openAPIType, ['\DateTime', '\SplFileObject', 'array', 'bool', 'boolean', 'byte', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) {
$callable = [$openAPIType, 'getAllowableEnumValues'];
if (is_callable($callable)) {
/** array $callable */
$allowedEnumTypes = $callable();
if (!in_array($value, $allowedEnumTypes, true)) {
$imploded = implode("', '", $allowedEnumTypes);
if ($openAPIType instanceof \BackedEnum) {
$data = $openAPIType::tryFrom($data);
if ($data === null) {
$imploded = implode("', '", array_map(fn($case) => $case->value, $openAPIType::cases()));
throw new \InvalidArgumentException("Invalid value for enum '$openAPIType', must be one of: '$imploded'");
}
}
Expand Down Expand Up @@ -497,9 +495,10 @@ public static function deserialize($data, $class, $httpHeaders = null)
}


if (method_exists($class, 'getAllowableEnumValues')) {
if (!in_array($data, $class::getAllowableEnumValues(), true)) {
$imploded = implode("', '", $class::getAllowableEnumValues());
if ($class instanceof \BackedEnum) {
$data = $class::tryFrom($data);
if ($data === null) {
$imploded = implode("', '", array_map(fn($case) => $case->value, $class::cases()));
throw new \InvalidArgumentException("Invalid value for enum '$class', must be one of: '$imploded'");
}
return $data;
Expand Down