Merge pull request #3534 from arnested/php-enums-in-containers

[PHP] Fix enum validation in container setters
This commit is contained in:
wing328 2016-08-08 18:29:44 +08:00 committed by GitHub
commit 39e0e22c57
8 changed files with 52 additions and 31 deletions

View File

@ -238,9 +238,16 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
{
{{#isEnum}}
$allowed_values = array({{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}});
{{^isContainer}}
if (!in_array(${{{name}}}, $allowed_values)) {
throw new \InvalidArgumentException("Invalid value for '{{name}}', must be one of {{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}");
}
{{/isContainer}}
{{#isContainer}}
if (array_diff(${{{name}}}, $allowed_values)) {
throw new \InvalidArgumentException("Invalid value for '{{name}}', must be one of {{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}");
}
{{/isContainer}}
{{/isEnum}}
{{#hasValidation}}
{{#maxLength}}

View File

@ -4,7 +4,7 @@ This spec is mainly for testing Petstore server and contains fake endpoints, mod
This PHP package is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project:
- API version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
- Build date: 2016-08-01T11:48:23.775+02:00
- Build date: 2016-08-05T11:24:40.650+02:00
- Build package: class io.swagger.codegen.languages.PhpClientCodegen
## Requirements

View File

@ -4,7 +4,7 @@ This spec is mainly for testing Petstore server and contains fake endpoints, mod
This PHP package is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project:
- API version: 1.0.0
- Build date: 2016-08-01T11:48:17.791+02:00
- Build date: 2016-08-05T11:24:33.550+02:00
- Build package: class io.swagger.codegen.languages.PhpClientCodegen
## Requirements
@ -114,6 +114,7 @@ Class | Method | HTTP request | Description
- [Category](docs/Model/Category.md)
- [Client](docs/Model/Client.md)
- [Dog](docs/Model/Dog.md)
- [EnumArrays](docs/Model/EnumArrays.md)
- [EnumClass](docs/Model/EnumClass.md)
- [EnumTest](docs/Model/EnumTest.md)
- [FormatTest](docs/Model/FormatTest.md)

View File

@ -3,7 +3,7 @@
## Properties
Name | Type | Description | Notes
------------ | ------------- | ------------- | -------------
**just_enum** | **string** | | [optional]
**just_symbol** | **string** | | [optional]
**array_enum** | **string[]** | | [optional]
[[Back to Model list]](../README.md#documentation-for-models) [[Back to API list]](../README.md#documentation-for-api-endpoints) [[Back to README]](../README.md)

View File

@ -66,7 +66,7 @@ class EnumArrays implements ArrayAccess
* @var string[]
*/
protected static $swaggerTypes = array(
'just_enum' => 'string',
'just_symbol' => 'string',
'array_enum' => 'string[]'
);
@ -80,7 +80,7 @@ class EnumArrays implements ArrayAccess
* @var string[]
*/
protected static $attributeMap = array(
'just_enum' => 'just_enum',
'just_symbol' => 'just_symbol',
'array_enum' => 'array_enum'
);
@ -94,7 +94,7 @@ class EnumArrays implements ArrayAccess
* @var string[]
*/
protected static $setters = array(
'just_enum' => 'setJustEnum',
'just_symbol' => 'setJustSymbol',
'array_enum' => 'setArrayEnum'
);
@ -108,7 +108,7 @@ class EnumArrays implements ArrayAccess
* @var string[]
*/
protected static $getters = array(
'just_enum' => 'getJustEnum',
'just_symbol' => 'getJustSymbol',
'array_enum' => 'getArrayEnum'
);
@ -117,8 +117,8 @@ class EnumArrays implements ArrayAccess
return self::$getters;
}
const JUST_ENUM_BIRD = 'bird';
const JUST_ENUM_EAGLE = 'eagle';
const JUST_SYMBOL_ = '>=';
const JUST_SYMBOL_ = '$';
const ARRAY_ENUM_FISH = 'fish';
const ARRAY_ENUM_CRAB = 'crab';
@ -128,11 +128,11 @@ class EnumArrays implements ArrayAccess
* Gets allowable values of the enum
* @return string[]
*/
public function getJustEnumAllowableValues()
public function getJustSymbolAllowableValues()
{
return [
self::JUST_ENUM_BIRD,
self::JUST_ENUM_EAGLE,
self::JUST_SYMBOL_,
self::JUST_SYMBOL_,
];
}
@ -161,7 +161,7 @@ class EnumArrays implements ArrayAccess
*/
public function __construct(array $data = null)
{
$this->container['just_enum'] = isset($data['just_enum']) ? $data['just_enum'] : null;
$this->container['just_symbol'] = isset($data['just_symbol']) ? $data['just_symbol'] : null;
$this->container['array_enum'] = isset($data['array_enum']) ? $data['array_enum'] : null;
}
@ -173,9 +173,9 @@ class EnumArrays implements ArrayAccess
public function listInvalidProperties()
{
$invalid_properties = array();
$allowed_values = array("bird", "eagle");
if (!in_array($this->container['just_enum'], $allowed_values)) {
$invalid_properties[] = "invalid value for 'just_enum', must be one of #{allowed_values}.";
$allowed_values = array(">=", "$");
if (!in_array($this->container['just_symbol'], $allowed_values)) {
$invalid_properties[] = "invalid value for 'just_symbol', must be one of #{allowed_values}.";
}
return $invalid_properties;
@ -189,8 +189,8 @@ class EnumArrays implements ArrayAccess
*/
public function valid()
{
$allowed_values = array("bird", "eagle");
if (!in_array($this->container['just_enum'], $allowed_values)) {
$allowed_values = array(">=", "$");
if (!in_array($this->container['just_symbol'], $allowed_values)) {
return false;
}
return true;
@ -198,26 +198,26 @@ class EnumArrays implements ArrayAccess
/**
* Gets just_enum
* Gets just_symbol
* @return string
*/
public function getJustEnum()
public function getJustSymbol()
{
return $this->container['just_enum'];
return $this->container['just_symbol'];
}
/**
* Sets just_enum
* @param string $just_enum
* Sets just_symbol
* @param string $just_symbol
* @return $this
*/
public function setJustEnum($just_enum)
public function setJustSymbol($just_symbol)
{
$allowed_values = array('bird', 'eagle');
if (!in_array($just_enum, $allowed_values)) {
throw new \InvalidArgumentException("Invalid value for 'just_enum', must be one of 'bird', 'eagle'");
$allowed_values = array('>=', '$');
if (!in_array($just_symbol, $allowed_values)) {
throw new \InvalidArgumentException("Invalid value for 'just_symbol', must be one of '>=', '$'");
}
$this->container['just_enum'] = $just_enum;
$this->container['just_symbol'] = $just_symbol;
return $this;
}
@ -239,7 +239,7 @@ class EnumArrays implements ArrayAccess
public function setArrayEnum($array_enum)
{
$allowed_values = array('fish', 'crab');
if (!in_array($array_enum, $allowed_values)) {
if (array_diff($array_enum, $allowed_values)) {
throw new \InvalidArgumentException("Invalid value for 'array_enum', must be one of 'fish', 'crab'");
}
$this->container['array_enum'] = $array_enum;

View File

@ -203,39 +203,50 @@ class FormatTest implements ArrayAccess
if (!is_null($this->container['integer']) && ($this->container['integer'] > 100.0)) {
$invalid_properties[] = "invalid value for 'integer', must be smaller than or equal to 100.0.";
}
if (!is_null($this->container['integer']) && ($this->container['integer'] < 10.0)) {
$invalid_properties[] = "invalid value for 'integer', must be bigger than or equal to 10.0.";
}
if (!is_null($this->container['int32']) && ($this->container['int32'] > 200.0)) {
$invalid_properties[] = "invalid value for 'int32', must be smaller than or equal to 200.0.";
}
if (!is_null($this->container['int32']) && ($this->container['int32'] < 20.0)) {
$invalid_properties[] = "invalid value for 'int32', must be bigger than or equal to 20.0.";
}
if ($this->container['number'] === null) {
$invalid_properties[] = "'number' can't be null";
}
if (($this->container['number'] > 543.2)) {
$invalid_properties[] = "invalid value for 'number', must be smaller than or equal to 543.2.";
}
if (($this->container['number'] < 32.1)) {
$invalid_properties[] = "invalid value for 'number', must be bigger than or equal to 32.1.";
}
if (!is_null($this->container['float']) && ($this->container['float'] > 987.6)) {
$invalid_properties[] = "invalid value for 'float', must be smaller than or equal to 987.6.";
}
if (!is_null($this->container['float']) && ($this->container['float'] < 54.3)) {
$invalid_properties[] = "invalid value for 'float', must be bigger than or equal to 54.3.";
}
if (!is_null($this->container['double']) && ($this->container['double'] > 123.4)) {
$invalid_properties[] = "invalid value for 'double', must be smaller than or equal to 123.4.";
}
if (!is_null($this->container['double']) && ($this->container['double'] < 67.8)) {
$invalid_properties[] = "invalid value for 'double', must be bigger than or equal to 67.8.";
}
if (!is_null($this->container['string']) && !preg_match("/[a-z]/i", $this->container['string'])) {
$invalid_properties[] = "invalid value for 'string', must be conform to the pattern /[a-z]/i.";
}
if ($this->container['byte'] === null) {
$invalid_properties[] = "'byte' can't be null";
}
@ -248,9 +259,11 @@ class FormatTest implements ArrayAccess
if ((strlen($this->container['password']) > 64)) {
$invalid_properties[] = "invalid value for 'password', the character length must be smaller than or equal to 64.";
}
if ((strlen($this->container['password']) < 10)) {
$invalid_properties[] = "invalid value for 'password', the character length must be bigger than or equal to 10.";
}
return $invalid_properties;
}

View File

@ -212,7 +212,7 @@ class MapTest implements ArrayAccess
public function setMapOfEnumString($map_of_enum_string)
{
$allowed_values = array('UPPER', 'lower');
if (!in_array($map_of_enum_string, $allowed_values)) {
if (array_diff($map_of_enum_string, $allowed_values)) {
throw new \InvalidArgumentException("Invalid value for 'map_of_enum_string', must be one of 'UPPER', 'lower'");
}
$this->container['map_of_enum_string'] = $map_of_enum_string;

View File

@ -264,7 +264,7 @@ class ObjectSerializer
} else {
return null;
}
} elseif (in_array($class, array('integer', 'int', 'void', 'number', 'object', 'double', 'float', 'byte', 'DateTime', 'string', 'mixed', 'boolean', 'bool'))) {
} elseif (in_array($class, array('void', 'bool', 'string', 'double', 'byte', 'mixed', 'integer', 'float', 'int', 'DateTime', 'number', 'boolean', 'object'))) {
settype($data, $class);
return $data;
} elseif ($class === '\SplFileObject') {