diff --git a/modules/openapi-generator/src/main/resources/php/EnumInterface.mustache b/modules/openapi-generator/src/main/resources/php/EnumInterface.mustache new file mode 100644 index 000000000000..b731ee93fc89 --- /dev/null +++ b/modules/openapi-generator/src/main/resources/php/EnumInterface.mustache @@ -0,0 +1,36 @@ +partial_header}} +/** + * NOTE: This class is auto generated by OpenAPI Generator (https://openapi-generator.tech). + * https://openapi-generator.tech + * Do not edit the class manually. + */ + +namespace {{modelPackage}}; + +/** + * Interface abstracting enum access. + * + * @package {{modelPackage}} + * @author OpenAPI Generator team + */ +interface EnumInterface +{ + public static function getAllowableEnumValues(): array; + + /** + * @return mixed + */ + public function getValue(); +} diff --git a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache index 7d48d3557c34..51040326df4a 100644 --- a/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache +++ b/modules/openapi-generator/src/main/resources/php/ObjectSerializer.mustache @@ -19,6 +19,7 @@ namespace {{invokerPackage}}; +use {{modelPackage}}\EnumInterface; use {{modelPackage}}\ModelInterface; /** @@ -78,9 +79,9 @@ 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 */ + if ($value instanceof EnumInterface) { + $value = $value->getValue(); + $callable = [$openAPIType, 'getAllowableEnumValues']; $allowedEnumTypes = $callable(); if (!in_array($value, $allowedEnumTypes, true)) { $imploded = implode("', '", $allowedEnumTypes); diff --git a/modules/openapi-generator/src/main/resources/php/model_enum.mustache b/modules/openapi-generator/src/main/resources/php/model_enum.mustache index 88036f76ffc9..82d148d68577 100644 --- a/modules/openapi-generator/src/main/resources/php/model_enum.mustache +++ b/modules/openapi-generator/src/main/resources/php/model_enum.mustache @@ -1,4 +1,4 @@ -class {{classname}} +class {{classname}} implements EnumInterface { /** * Possible values of this enum @@ -6,9 +6,23 @@ class {{classname}} {{#allowableValues}} {{#enumVars}} const {{^isString}}NUMBER_{{/isString}}{{{name}}} = {{{value}}}; - {{/enumVars}} {{/allowableValues}} + + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true)) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } + /** * Gets allowable values of the enum * @return string[] @@ -25,4 +39,13 @@ class {{classname}} ]; } + + /** + * @return mixed + */ + #[\ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php index 8fe1b2880723..a33e48c4c010 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/EnumClass.php @@ -37,17 +37,29 @@ * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class EnumClass +class EnumClass implements EnumInterface { /** * Possible values of this enum */ const ABC = '_abc'; - const EFG = '-efg'; - const XYZ = '(xyz)'; + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true)) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } + /** * Gets allowable values of the enum * @return string[] @@ -60,6 +72,15 @@ public static function getAllowableEnumValues() self::XYZ ]; } + + /** + * @return mixed + */ + #[\ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php index b1b834319a8d..0c0b2a5994ab 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnum.php @@ -37,17 +37,29 @@ * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnum +class OuterEnum implements EnumInterface { /** * Possible values of this enum */ const PLACED = 'placed'; - const APPROVED = 'approved'; - const DELIVERED = 'delivered'; + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true)) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } + /** * Gets allowable values of the enum * @return string[] @@ -60,6 +72,15 @@ public static function getAllowableEnumValues() self::DELIVERED ]; } + + /** + * @return mixed + */ + #[\ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php index 5d88f63550c6..97e6af3e1bce 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumDefaultValue.php @@ -37,17 +37,29 @@ * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnumDefaultValue +class OuterEnumDefaultValue implements EnumInterface { /** * Possible values of this enum */ const PLACED = 'placed'; - const APPROVED = 'approved'; - const DELIVERED = 'delivered'; + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true)) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } + /** * Gets allowable values of the enum * @return string[] @@ -60,6 +72,15 @@ public static function getAllowableEnumValues() self::DELIVERED ]; } + + /** + * @return mixed + */ + #[\ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php index 5bb6ee6f26e0..8903613e680a 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumInteger.php @@ -37,17 +37,29 @@ * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnumInteger +class OuterEnumInteger implements EnumInterface { /** * Possible values of this enum */ const NUMBER_0 = 0; - const NUMBER_1 = 1; - const NUMBER_2 = 2; + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true)) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } + /** * Gets allowable values of the enum * @return string[] @@ -60,6 +72,15 @@ public static function getAllowableEnumValues() self::NUMBER_2 ]; } + + /** + * @return mixed + */ + #[\ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php index f7ae2d6b29db..ad82dd385ca4 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/Model/OuterEnumIntegerDefaultValue.php @@ -37,17 +37,29 @@ * @author OpenAPI Generator team * @link https://openapi-generator.tech */ -class OuterEnumIntegerDefaultValue +class OuterEnumIntegerDefaultValue implements EnumInterface { /** * Possible values of this enum */ const NUMBER_0 = 0; - const NUMBER_1 = 1; - const NUMBER_2 = 2; + + /** + * @var mixed + */ + private $value; + + public function __construct($value) + { + if (!in_array($value, self::getAllowableEnumValues(), true)) { + throw new \LogicException(sprintf('%s is not a allowed value for enum', $value)); + } + $this->value = $value; + } + /** * Gets allowable values of the enum * @return string[] @@ -60,6 +72,15 @@ public static function getAllowableEnumValues() self::NUMBER_2 ]; } + + /** + * @return mixed + */ + #[\ReturnTypeWillChange] + public function getValue() + { + return $this->value; + } } diff --git a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php index 840e4d934149..d21ded32c011 100644 --- a/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php +++ b/samples/client/petstore/php/OpenAPIClient-php/lib/ObjectSerializer.php @@ -28,6 +28,7 @@ namespace OpenAPI\Client; +use OpenAPI\Client\Model\EnumInterface; use OpenAPI\Client\Model\ModelInterface; /** @@ -87,9 +88,9 @@ 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', 'double', 'float', 'int', 'integer', 'mixed', 'number', 'object', 'string', 'void'], true)) { - $callable = [$openAPIType, 'getAllowableEnumValues']; - if (is_callable($callable)) { - /** array $callable */ + if ($value instanceof EnumInterface) { + $value = $value->getValue(); + $callable = [$openAPIType, 'getAllowableEnumValues']; $allowedEnumTypes = $callable(); if (!in_array($value, $allowedEnumTypes, true)) { $imploded = implode("', '", $allowedEnumTypes);