Added !is_null condition in setter conditions for non-required properties

This commit is contained in:
Xavi Aparicio 2016-09-14 13:41:05 +02:00
parent 153397e799
commit 31a3436a8e
7 changed files with 30 additions and 30 deletions

View File

@ -261,47 +261,47 @@ 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}} {{^isContainer}}
if (!in_array(${{{name}}}, $allowed_values)) { if ({{^required}}!is_null(${{name}}) && {{/required}}(!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}}
{{#isContainer}} {{#isContainer}}
if (array_diff(${{{name}}}, $allowed_values)) { if ({{^required}}!is_null(${{name}}) && {{/required}}(array_diff(${{{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}}
{{/isEnum}} {{/isEnum}}
{{#hasValidation}} {{#hasValidation}}
{{#maxLength}} {{#maxLength}}
if (strlen(${{name}}) > {{maxLength}}) { if ({{^required}}!is_null(${{name}}) && {{/required}}(strlen(${{name}}) > {{maxLength}})) {
throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.'); throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be smaller than or equal to {{maxLength}}.');
}{{/maxLength}} }{{/maxLength}}
{{#minLength}} {{#minLength}}
if (strlen(${{name}}) < {{minLength}}) { if ({{^required}}!is_null(${{name}}) && {{/required}}(strlen(${{name}}) < {{minLength}})) {
throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.'); throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than or equal to {{minLength}}.');
} }
{{/minLength}} {{/minLength}}
{{#maximum}} {{#maximum}}
if (${{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}) { if ({{^required}}!is_null(${{name}}) && {{/required}}(${{name}} >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) {
throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.'); throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.');
} }
{{/maximum}} {{/maximum}}
{{#minimum}} {{#minimum}}
if (${{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}) { if ({{^required}}!is_null(${{name}}) && {{/required}}(${{name}} <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) {
throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.'); throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.');
} }
{{/minimum}} {{/minimum}}
{{#pattern}} {{#pattern}}
if (!preg_match("{{{pattern}}}", ${{name}})) { if ({{^required}}!is_null(${{name}}) && {{/required}}(!preg_match("{{{pattern}}}", ${{name}}))) {
throw new \InvalidArgumentException("invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}."); throw new \InvalidArgumentException("invalid value for ${{name}} when calling {{classname}}.{{operationId}}, must conform to the pattern {{{pattern}}}.");
} }
{{/pattern}} {{/pattern}}
{{#maxItems}} {{#maxItems}}
if (count(${{name}}) > {{maxItems}}) { if ({{^required}}!is_null(${{name}}) && {{/required}}(count(${{name}}) > {{maxItems}})) {
throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{maxItems}}.'); throw new \InvalidArgumentException('invalid value for ${{name}} when calling {{classname}}.{{operationId}}, number of items must be less than or equal to {{maxItems}}.');
}{{/maxItems}} }{{/maxItems}}
{{#minItems}} {{#minItems}}
if (count(${{name}}) < {{minItems}}) { if ({{^required}}!is_null(${{name}}) && {{/required}}(count(${{name}}) < {{minItems}})) {
throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.'); throw new \InvalidArgumentException('invalid length for ${{name}} when calling {{classname}}.{{operationId}}, number of items must be greater than or equal to {{minItems}}.');
} }
{{/minItems}} {{/minItems}}

View File

@ -214,7 +214,7 @@ class EnumArrays implements ArrayAccess
public function setJustSymbol($just_symbol) public function setJustSymbol($just_symbol)
{ {
$allowed_values = array('>=', '$'); $allowed_values = array('>=', '$');
if (!in_array($just_symbol, $allowed_values)) { if (!is_null($just_symbol) && (!in_array($just_symbol, $allowed_values))) {
throw new \InvalidArgumentException("Invalid value for 'just_symbol', must be one of '>=', '$'"); throw new \InvalidArgumentException("Invalid value for 'just_symbol', must be one of '>=', '$'");
} }
$this->container['just_symbol'] = $just_symbol; $this->container['just_symbol'] = $just_symbol;
@ -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 (array_diff($array_enum, $allowed_values)) { if (!is_null($array_enum) && (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

@ -251,7 +251,7 @@ class EnumTest implements ArrayAccess
public function setEnumString($enum_string) public function setEnumString($enum_string)
{ {
$allowed_values = array('UPPER', 'lower'); $allowed_values = array('UPPER', 'lower');
if (!in_array($enum_string, $allowed_values)) { if (!is_null($enum_string) && (!in_array($enum_string, $allowed_values))) {
throw new \InvalidArgumentException("Invalid value for 'enum_string', must be one of 'UPPER', 'lower'"); throw new \InvalidArgumentException("Invalid value for 'enum_string', must be one of 'UPPER', 'lower'");
} }
$this->container['enum_string'] = $enum_string; $this->container['enum_string'] = $enum_string;
@ -276,7 +276,7 @@ class EnumTest implements ArrayAccess
public function setEnumInteger($enum_integer) public function setEnumInteger($enum_integer)
{ {
$allowed_values = array('1', '-1'); $allowed_values = array('1', '-1');
if (!in_array($enum_integer, $allowed_values)) { if (!is_null($enum_integer) && (!in_array($enum_integer, $allowed_values))) {
throw new \InvalidArgumentException("Invalid value for 'enum_integer', must be one of '1', '-1'"); throw new \InvalidArgumentException("Invalid value for 'enum_integer', must be one of '1', '-1'");
} }
$this->container['enum_integer'] = $enum_integer; $this->container['enum_integer'] = $enum_integer;
@ -301,7 +301,7 @@ class EnumTest implements ArrayAccess
public function setEnumNumber($enum_number) public function setEnumNumber($enum_number)
{ {
$allowed_values = array('1.1', '-1.2'); $allowed_values = array('1.1', '-1.2');
if (!in_array($enum_number, $allowed_values)) { if (!is_null($enum_number) && (!in_array($enum_number, $allowed_values))) {
throw new \InvalidArgumentException("Invalid value for 'enum_number', must be one of '1.1', '-1.2'"); throw new \InvalidArgumentException("Invalid value for 'enum_number', must be one of '1.1', '-1.2'");
} }
$this->container['enum_number'] = $enum_number; $this->container['enum_number'] = $enum_number;

View File

@ -347,10 +347,10 @@ class FormatTest implements ArrayAccess
public function setInteger($integer) public function setInteger($integer)
{ {
if ($integer > 100.0) { if (!is_null($integer) && ($integer > 100.0)) {
throw new \InvalidArgumentException('invalid value for $integer when calling FormatTest., must be smaller than or equal to 100.0.'); throw new \InvalidArgumentException('invalid value for $integer when calling FormatTest., must be smaller than or equal to 100.0.');
} }
if ($integer < 10.0) { if (!is_null($integer) && ($integer < 10.0)) {
throw new \InvalidArgumentException('invalid value for $integer when calling FormatTest., must be bigger than or equal to 10.0.'); throw new \InvalidArgumentException('invalid value for $integer when calling FormatTest., must be bigger than or equal to 10.0.');
} }
@ -376,10 +376,10 @@ class FormatTest implements ArrayAccess
public function setInt32($int32) public function setInt32($int32)
{ {
if ($int32 > 200.0) { if (!is_null($int32) && ($int32 > 200.0)) {
throw new \InvalidArgumentException('invalid value for $int32 when calling FormatTest., must be smaller than or equal to 200.0.'); throw new \InvalidArgumentException('invalid value for $int32 when calling FormatTest., must be smaller than or equal to 200.0.');
} }
if ($int32 < 20.0) { if (!is_null($int32) && ($int32 < 20.0)) {
throw new \InvalidArgumentException('invalid value for $int32 when calling FormatTest., must be bigger than or equal to 20.0.'); throw new \InvalidArgumentException('invalid value for $int32 when calling FormatTest., must be bigger than or equal to 20.0.');
} }
@ -426,10 +426,10 @@ class FormatTest implements ArrayAccess
public function setNumber($number) public function setNumber($number)
{ {
if ($number > 543.2) { if (($number > 543.2)) {
throw new \InvalidArgumentException('invalid value for $number when calling FormatTest., must be smaller than or equal to 543.2.'); throw new \InvalidArgumentException('invalid value for $number when calling FormatTest., must be smaller than or equal to 543.2.');
} }
if ($number < 32.1) { if (($number < 32.1)) {
throw new \InvalidArgumentException('invalid value for $number when calling FormatTest., must be bigger than or equal to 32.1.'); throw new \InvalidArgumentException('invalid value for $number when calling FormatTest., must be bigger than or equal to 32.1.');
} }
@ -455,10 +455,10 @@ class FormatTest implements ArrayAccess
public function setFloat($float) public function setFloat($float)
{ {
if ($float > 987.6) { if (!is_null($float) && ($float > 987.6)) {
throw new \InvalidArgumentException('invalid value for $float when calling FormatTest., must be smaller than or equal to 987.6.'); throw new \InvalidArgumentException('invalid value for $float when calling FormatTest., must be smaller than or equal to 987.6.');
} }
if ($float < 54.3) { if (!is_null($float) && ($float < 54.3)) {
throw new \InvalidArgumentException('invalid value for $float when calling FormatTest., must be bigger than or equal to 54.3.'); throw new \InvalidArgumentException('invalid value for $float when calling FormatTest., must be bigger than or equal to 54.3.');
} }
@ -484,10 +484,10 @@ class FormatTest implements ArrayAccess
public function setDouble($double) public function setDouble($double)
{ {
if ($double > 123.4) { if (!is_null($double) && ($double > 123.4)) {
throw new \InvalidArgumentException('invalid value for $double when calling FormatTest., must be smaller than or equal to 123.4.'); throw new \InvalidArgumentException('invalid value for $double when calling FormatTest., must be smaller than or equal to 123.4.');
} }
if ($double < 67.8) { if (!is_null($double) && ($double < 67.8)) {
throw new \InvalidArgumentException('invalid value for $double when calling FormatTest., must be bigger than or equal to 67.8.'); throw new \InvalidArgumentException('invalid value for $double when calling FormatTest., must be bigger than or equal to 67.8.');
} }
@ -513,7 +513,7 @@ class FormatTest implements ArrayAccess
public function setString($string) public function setString($string)
{ {
if (!preg_match("/[a-z]/i", $string)) { if (!is_null($string) && (!preg_match("/[a-z]/i", $string))) {
throw new \InvalidArgumentException("invalid value for $string when calling FormatTest., must conform to the pattern /[a-z]/i."); throw new \InvalidArgumentException("invalid value for $string when calling FormatTest., must conform to the pattern /[a-z]/i.");
} }
@ -643,10 +643,10 @@ class FormatTest implements ArrayAccess
*/ */
public function setPassword($password) public function setPassword($password)
{ {
if (strlen($password) > 64) { if ((strlen($password) > 64)) {
throw new \InvalidArgumentException('invalid length for $password when calling FormatTest., must be smaller than or equal to 64.'); throw new \InvalidArgumentException('invalid length for $password when calling FormatTest., must be smaller than or equal to 64.');
} }
if (strlen($password) < 10) { if ((strlen($password) < 10)) {
throw new \InvalidArgumentException('invalid length for $password when calling FormatTest., must be bigger than or equal to 10.'); throw new \InvalidArgumentException('invalid length for $password when calling FormatTest., must be bigger than or equal to 10.');
} }

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 (array_diff($map_of_enum_string, $allowed_values)) { if (!is_null($map_of_enum_string) && (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

@ -306,7 +306,7 @@ class Order implements ArrayAccess
public function setStatus($status) public function setStatus($status)
{ {
$allowed_values = array('placed', 'approved', 'delivered'); $allowed_values = array('placed', 'approved', 'delivered');
if (!in_array($status, $allowed_values)) { if (!is_null($status) && (!in_array($status, $allowed_values))) {
throw new \InvalidArgumentException("Invalid value for 'status', must be one of 'placed', 'approved', 'delivered'"); throw new \InvalidArgumentException("Invalid value for 'status', must be one of 'placed', 'approved', 'delivered'");
} }
$this->container['status'] = $status; $this->container['status'] = $status;

View File

@ -339,7 +339,7 @@ class Pet implements ArrayAccess
public function setStatus($status) public function setStatus($status)
{ {
$allowed_values = array('available', 'pending', 'sold'); $allowed_values = array('available', 'pending', 'sold');
if (!in_array($status, $allowed_values)) { if (!is_null($status) && (!in_array($status, $allowed_values))) {
throw new \InvalidArgumentException("Invalid value for 'status', must be one of 'available', 'pending', 'sold'"); throw new \InvalidArgumentException("Invalid value for 'status', must be one of 'available', 'pending', 'sold'");
} }
$this->container['status'] = $status; $this->container['status'] = $status;