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}} {{#isEnum}}
$allowed_values = array({{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}); $allowed_values = array({{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}});
{{^isContainer}}
if (!in_array(${{{name}}}, $allowed_values)) { if (!in_array(${{{name}}}, $allowed_values)) {
throw new \InvalidArgumentException("Invalid value for '{{name}}', must be one of {{#allowableValues}}{{#values}}'{{{this}}}'{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}}"); 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}} {{/isEnum}}
{{#hasValidation}} {{#hasValidation}}
{{#maxLength}} {{#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: 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 - 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 - Build package: class io.swagger.codegen.languages.PhpClientCodegen
## Requirements ## 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: This PHP package is automatically generated by the [Swagger Codegen](https://github.com/swagger-api/swagger-codegen) project:
- API version: 1.0.0 - 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 - Build package: class io.swagger.codegen.languages.PhpClientCodegen
## Requirements ## Requirements
@ -114,6 +114,7 @@ Class | Method | HTTP request | Description
- [Category](docs/Model/Category.md) - [Category](docs/Model/Category.md)
- [Client](docs/Model/Client.md) - [Client](docs/Model/Client.md)
- [Dog](docs/Model/Dog.md) - [Dog](docs/Model/Dog.md)
- [EnumArrays](docs/Model/EnumArrays.md)
- [EnumClass](docs/Model/EnumClass.md) - [EnumClass](docs/Model/EnumClass.md)
- [EnumTest](docs/Model/EnumTest.md) - [EnumTest](docs/Model/EnumTest.md)
- [FormatTest](docs/Model/FormatTest.md) - [FormatTest](docs/Model/FormatTest.md)

View File

@ -3,7 +3,7 @@
## Properties ## Properties
Name | Type | Description | Notes Name | Type | Description | Notes
------------ | ------------- | ------------- | ------------- ------------ | ------------- | ------------- | -------------
**just_enum** | **string** | | [optional] **just_symbol** | **string** | | [optional]
**array_enum** | **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) [[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[] * @var string[]
*/ */
protected static $swaggerTypes = array( protected static $swaggerTypes = array(
'just_enum' => 'string', 'just_symbol' => 'string',
'array_enum' => 'string[]' 'array_enum' => 'string[]'
); );
@ -80,7 +80,7 @@ class EnumArrays implements ArrayAccess
* @var string[] * @var string[]
*/ */
protected static $attributeMap = array( protected static $attributeMap = array(
'just_enum' => 'just_enum', 'just_symbol' => 'just_symbol',
'array_enum' => 'array_enum' 'array_enum' => 'array_enum'
); );
@ -94,7 +94,7 @@ class EnumArrays implements ArrayAccess
* @var string[] * @var string[]
*/ */
protected static $setters = array( protected static $setters = array(
'just_enum' => 'setJustEnum', 'just_symbol' => 'setJustSymbol',
'array_enum' => 'setArrayEnum' 'array_enum' => 'setArrayEnum'
); );
@ -108,7 +108,7 @@ class EnumArrays implements ArrayAccess
* @var string[] * @var string[]
*/ */
protected static $getters = array( protected static $getters = array(
'just_enum' => 'getJustEnum', 'just_symbol' => 'getJustSymbol',
'array_enum' => 'getArrayEnum' 'array_enum' => 'getArrayEnum'
); );
@ -117,8 +117,8 @@ class EnumArrays implements ArrayAccess
return self::$getters; return self::$getters;
} }
const JUST_ENUM_BIRD = 'bird'; const JUST_SYMBOL_ = '>=';
const JUST_ENUM_EAGLE = 'eagle'; const JUST_SYMBOL_ = '$';
const ARRAY_ENUM_FISH = 'fish'; const ARRAY_ENUM_FISH = 'fish';
const ARRAY_ENUM_CRAB = 'crab'; const ARRAY_ENUM_CRAB = 'crab';
@ -128,11 +128,11 @@ class EnumArrays implements ArrayAccess
* Gets allowable values of the enum * Gets allowable values of the enum
* @return string[] * @return string[]
*/ */
public function getJustEnumAllowableValues() public function getJustSymbolAllowableValues()
{ {
return [ return [
self::JUST_ENUM_BIRD, self::JUST_SYMBOL_,
self::JUST_ENUM_EAGLE, self::JUST_SYMBOL_,
]; ];
} }
@ -161,7 +161,7 @@ class EnumArrays implements ArrayAccess
*/ */
public function __construct(array $data = null) 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; $this->container['array_enum'] = isset($data['array_enum']) ? $data['array_enum'] : null;
} }
@ -173,9 +173,9 @@ class EnumArrays implements ArrayAccess
public function listInvalidProperties() public function listInvalidProperties()
{ {
$invalid_properties = array(); $invalid_properties = array();
$allowed_values = array("bird", "eagle"); $allowed_values = array(">=", "$");
if (!in_array($this->container['just_enum'], $allowed_values)) { if (!in_array($this->container['just_symbol'], $allowed_values)) {
$invalid_properties[] = "invalid value for 'just_enum', must be one of #{allowed_values}."; $invalid_properties[] = "invalid value for 'just_symbol', must be one of #{allowed_values}.";
} }
return $invalid_properties; return $invalid_properties;
@ -189,8 +189,8 @@ class EnumArrays implements ArrayAccess
*/ */
public function valid() public function valid()
{ {
$allowed_values = array("bird", "eagle"); $allowed_values = array(">=", "$");
if (!in_array($this->container['just_enum'], $allowed_values)) { if (!in_array($this->container['just_symbol'], $allowed_values)) {
return false; return false;
} }
return true; return true;
@ -198,26 +198,26 @@ class EnumArrays implements ArrayAccess
/** /**
* Gets just_enum * Gets just_symbol
* @return string * @return string
*/ */
public function getJustEnum() public function getJustSymbol()
{ {
return $this->container['just_enum']; return $this->container['just_symbol'];
} }
/** /**
* Sets just_enum * Sets just_symbol
* @param string $just_enum * @param string $just_symbol
* @return $this * @return $this
*/ */
public function setJustEnum($just_enum) public function setJustSymbol($just_symbol)
{ {
$allowed_values = array('bird', 'eagle'); $allowed_values = array('>=', '$');
if (!in_array($just_enum, $allowed_values)) { if (!in_array($just_symbol, $allowed_values)) {
throw new \InvalidArgumentException("Invalid value for 'just_enum', must be one of 'bird', 'eagle'"); 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; return $this;
} }
@ -239,7 +239,7 @@ class EnumArrays implements ArrayAccess
public function setArrayEnum($array_enum) public function setArrayEnum($array_enum)
{ {
$allowed_values = array('fish', 'crab'); $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'"); throw new \InvalidArgumentException("Invalid value for 'array_enum', must be one of 'fish', 'crab'");
} }
$this->container['array_enum'] = $array_enum; $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)) { 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."; $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)) { 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."; $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)) { 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."; $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)) { 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."; $invalid_properties[] = "invalid value for 'int32', must be bigger than or equal to 20.0.";
} }
if ($this->container['number'] === null) { if ($this->container['number'] === null) {
$invalid_properties[] = "'number' can't be null"; $invalid_properties[] = "'number' can't be null";
} }
if (($this->container['number'] > 543.2)) { if (($this->container['number'] > 543.2)) {
$invalid_properties[] = "invalid value for 'number', must be smaller than or equal to 543.2."; $invalid_properties[] = "invalid value for 'number', must be smaller than or equal to 543.2.";
} }
if (($this->container['number'] < 32.1)) { if (($this->container['number'] < 32.1)) {
$invalid_properties[] = "invalid value for 'number', must be bigger than or equal to 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)) { 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."; $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)) { 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."; $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)) { 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."; $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)) { 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."; $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'])) { 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."; $invalid_properties[] = "invalid value for 'string', must be conform to the pattern /[a-z]/i.";
} }
if ($this->container['byte'] === null) { if ($this->container['byte'] === null) {
$invalid_properties[] = "'byte' can't be null"; $invalid_properties[] = "'byte' can't be null";
} }
@ -248,9 +259,11 @@ class FormatTest implements ArrayAccess
if ((strlen($this->container['password']) > 64)) { if ((strlen($this->container['password']) > 64)) {
$invalid_properties[] = "invalid value for 'password', the character length must be smaller than or equal to 64."; $invalid_properties[] = "invalid value for 'password', the character length must be smaller than or equal to 64.";
} }
if ((strlen($this->container['password']) < 10)) { if ((strlen($this->container['password']) < 10)) {
$invalid_properties[] = "invalid value for 'password', the character length must be bigger than or equal to 10."; $invalid_properties[] = "invalid value for 'password', the character length must be bigger than or equal to 10.";
} }
return $invalid_properties; return $invalid_properties;
} }

View File

@ -212,7 +212,7 @@ class MapTest implements ArrayAccess
public function setMapOfEnumString($map_of_enum_string) public function setMapOfEnumString($map_of_enum_string)
{ {
$allowed_values = array('UPPER', 'lower'); $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'"); 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; $this->container['map_of_enum_string'] = $map_of_enum_string;

View File

@ -264,7 +264,7 @@ class ObjectSerializer
} else { } else {
return null; 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); settype($data, $class);
return $data; return $data;
} elseif ($class === '\SplFileObject') { } elseif ($class === '\SplFileObject') {