add validation to model

This commit is contained in:
abcsun
2016-05-05 11:41:14 +08:00
parent f4ef2b0325
commit 22ea2d87e0
17 changed files with 987 additions and 60 deletions

View File

@@ -135,6 +135,51 @@ class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}implements ArrayA
}
}
/**
* show all the invalid properties with reasons.
*
* @return array invalid properties with reasons
*/
public function list_invalid_properties()
{
$invalid_properties = array();
{{#vars}}{{#required}}
if ($this->{{name}} === null) {
$invalid_properties[] = "'${{name}}' can't be null";
}{{/required}}
{{#isEnum}}$allowed_values = array({{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}});
if (!in_array($this->{{name}}, $allowed_values))) {
$invalid_properties[] = "invalid value for '${{name}}', must be one of #{allowed_values}.";
}{{/isEnum}}
{{#hasValidation}}
{{#maxLength}}
if (strlen($this->{{name}}) > {{maxLength}}) {
$invalid_properties[] = "invalid value for '${{name}}', the character length must be smaller than or equal to {{{maxLength}}}.";
}
{{/maxLength}}
{{#minLength}}
if (strlen($this->{{name}}) < {{minLength}}) {
$invalid_properties[] = "invalid value for '${{name}}', the character length must be bigger than or equal to {{{minLength}}}.";
}
{{/minLength}}
{{#maximum}}
if ($this->{{name}} > {{maximum}}) {
$invalid_properties[] = "invalid value for '${{name}}', must be smaller than or equal to {{maximum}}.";
}
{{/maximum}}
{{#minimum}}
if ($this->{{name}} < {{minimum}}) {
$invalid_properties[] = "invalid value for '${{name}}', must be bigger than or equal to {{minimum}}.";
}
{{/minimum}}
{{#pattern}}
if (!preg_match("{{pattern}}", $this->{{name}})) {
$invalid_properties[] = "invalid value for '${{name}}', must be conform to the pattern {{pattern}}.";
}
{{/pattern}}{{/hasValidation}}{{/vars}}
return $invalid_properties;
}
/**
* validate all the parameters in the model
* return true if all passed
@@ -143,12 +188,14 @@ class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}implements ArrayA
*/
public function valid()
{
{{#vars}}
{{#required}}
{{#vars}}{{#required}}
if ($this->{{name}} === null) {
return false;
}
{{/required}}
}{{/required}}
{{#isEnum}}$allowed_values = array({{#allowableValues}}{{#values}}"{{{this}}}"{{^-last}}, {{/-last}}{{/values}}{{/allowableValues}});
if (!in_array($this->{{name}}, $allowed_values))) {
return false;
}{{/isEnum}}
{{#hasValidation}}
{{#maxLength}}
if (strlen($this->{{name}}) > {{maxLength}}) {
@@ -174,9 +221,7 @@ class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}implements ArrayA
if (!preg_match("{{pattern}}", $this->{{name}})) {
return false;
}
{{/pattern}}
{{/hasValidation}}
{{/vars}}
{{/pattern}}{{/hasValidation}}{{/vars}}
return true;
}
@@ -224,10 +269,9 @@ class {{classname}} {{#parent}}extends {{{parent}}} {{/parent}}implements ArrayA
{{/minimum}}
{{#pattern}}
if (!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 be conform to the pattern {{pattern}}.');
}
{{/pattern}}
{{/hasValidation}}
{{/pattern}}{{/hasValidation}}
$this->{{name}} = ${{name}};
return $this;
}