Skip to content

[php] fixed model_enum.mustache for real usage #11602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
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
@@ -0,0 +1,36 @@
<?php
/**
* EnumInterface
*
* PHP version 7.3
*
* @category Class
* @package {{modelPackage}}
* @author OpenAPI Generator team
* @link https://openapi-generator.tech
*/

{{>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();
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@

namespace {{invokerPackage}};

use {{modelPackage}}\EnumInterface;
use {{modelPackage}}\ModelInterface;

/**
Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,28 @@
class {{classname}}
class {{classname}} implements EnumInterface
{
/**
* Possible values of this enum
*/
{{#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[]
Expand All @@ -25,4 +39,13 @@ class {{classname}}

];
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function getValue()
{
return $this->value;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -60,6 +72,15 @@ public static function getAllowableEnumValues()
self::XYZ
];
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function getValue()
{
return $this->value;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -60,6 +72,15 @@ public static function getAllowableEnumValues()
self::DELIVERED
];
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function getValue()
{
return $this->value;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -60,6 +72,15 @@ public static function getAllowableEnumValues()
self::DELIVERED
];
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function getValue()
{
return $this->value;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -60,6 +72,15 @@ public static function getAllowableEnumValues()
self::NUMBER_2
];
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function getValue()
{
return $this->value;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand All @@ -60,6 +72,15 @@ public static function getAllowableEnumValues()
self::NUMBER_2
];
}

/**
* @return mixed
*/
#[\ReturnTypeWillChange]
public function getValue()
{
return $this->value;
}
}


Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@

namespace OpenAPI\Client;

use OpenAPI\Client\Model\EnumInterface;
use OpenAPI\Client\Model\ModelInterface;

/**
Expand Down Expand Up @@ -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);
Expand Down