Skip to content

Commit

Permalink
[PHP] Improve Model template (#6460)
Browse files Browse the repository at this point in the history
* Update samples

./bin/php-petstore.sh

* Remove unnecessary implements entry

ModelInterface, ArrayAccess are already implemented in parent

* Remove field `container` which is already defined in parent

* Change snake case to lower camel case

- invalid_properties
- allowed_values

* Improve doc commenct style

* Improve description length

* Improve length

* Doc comment short description must start with a capital letter

* Add a line between @param and @return

* Delete an additinal blank line at end of doc comment

* Udpate petstore-security-test
  • Loading branch information
ackintosh authored and wing328 committed Sep 23, 2017
1 parent 4a89d23 commit 8f85154
Show file tree
Hide file tree
Showing 56 changed files with 1,872 additions and 806 deletions.
11 changes: 7 additions & 4 deletions modules/swagger-codegen/src/main/resources/php/model.mustache
Original file line number Diff line number Diff line change
Expand Up @@ -21,20 +21,23 @@

namespace {{modelPackage}};
{{^isEnum}}
{{^parentSchema}}

use \ArrayAccess;
{{/parentSchema}}
{{/isEnum}}
use \{{invokerPackage}}\ObjectSerializer;

/**
* {{classname}} Class Doc Comment
*
* @category Class
* @category Class
{{#description}}
* @description {{description}}
{{/description}}
* @package {{invokerPackage}}
* @author Swagger Codegen team
* @link https://github.com/swagger-api/swagger-codegen
* @package {{invokerPackage}}
* @author Swagger Codegen team
* @link https://github.com/swagger-api/swagger-codegen
*/
{{#isEnum}}{{>model_enum}}{{/isEnum}}{{^isEnum}}{{>model_generic}}{{/isEnum}}
{{/model}}{{/models}}
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}implements ModelInterface, ArrayAccess
class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^parentSchema}}implements ModelInterface, ArrayAccess{{/parentSchema}}
{
const DISCRIMINATOR = {{#discriminator}}'{{discriminator}}'{{/discriminator}}{{^discriminator}}null{{/discriminator}};

/**
* The original name of the model.
*
* @var string
*/
protected static $swaggerModelName = '{{name}}';

/**
* Array of property to type mappings. Used for (de)serialization
*
* @var string[]
*/
protected static $swaggerTypes = [
Expand All @@ -19,6 +21,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple

/**
* Array of property to format mappings. Used for (de)serialization
*
* @var string[]
*/
protected static $swaggerFormats = [
Expand Down Expand Up @@ -47,7 +50,9 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
}

/**
* Array of attributes where the key is the local name, and the value is the original name
* Array of attributes where the key is the local name,
* and the value is the original name
*
* @var string[]
*/
protected static $attributeMap = [
Expand All @@ -57,6 +62,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple

/**
* Array of attributes to setter functions (for deserialization of responses)
*
* @var string[]
*/
protected static $setters = [
Expand All @@ -66,6 +72,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple

/**
* Array of attributes to getter functions (for serialization of requests)
*
* @var string[]
*/
protected static $getters = [
Expand All @@ -74,7 +81,8 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
];

/**
* Array of attributes where the key is the local name, and the value is the original name
* Array of attributes where the key is the local name,
* and the value is the original name
*
* @return array
*/
Expand Down Expand Up @@ -119,6 +127,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
{{#vars}}{{#isEnum}}
/**
* Gets allowable values of the enum
*
* @return string[]
*/
public function {{getter}}AllowableValues()
Expand All @@ -130,15 +139,20 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
}
{{/isEnum}}{{/vars}}

{{^parentSchema}}
/**
* Associative array for storing property values
*
* @var mixed[]
*/
protected $container = [];
{{/parentSchema}}

/**
* Constructor
* @param mixed[] $data Associated array of property values initializing the model
*
* @param mixed[] $data Associated array of property values
* initializing the model
*/
public function __construct(array $data = null)
{
Expand All @@ -165,25 +179,25 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
public function listInvalidProperties()
{
{{#parent}}
$invalid_properties = parent::listInvalidProperties();
$invalidProperties = parent::listInvalidProperties();
{{/parent}}
{{^parent}}
$invalid_properties = [];
$invalidProperties = [];
{{/parent}}

{{#vars}}
{{#required}}
if ($this->container['{{name}}'] === null) {
$invalid_properties[] = "'{{name}}' can't be null";
$invalidProperties[] = "'{{name}}' can't be null";
}
{{/required}}
{{#isEnum}}
{{^isContainer}}
$allowed_values = $this->{{getter}}AllowableValues();
if (!in_array($this->container['{{name}}'], $allowed_values)) {
$invalid_properties[] = sprintf(
$allowedValues = $this->{{getter}}AllowableValues();
if (!in_array($this->container['{{name}}'], $allowedValues)) {
$invalidProperties[] = sprintf(
"invalid value for '{{name}}', must be one of '%s'",
implode("', '", $allowed_values)
implode("', '", $allowedValues)
);
}

Expand All @@ -192,53 +206,53 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
{{#hasValidation}}
{{#maxLength}}
if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(strlen($this->container['{{name}}']) > {{maxLength}})) {
$invalid_properties[] = "invalid value for '{{name}}', the character length must be smaller than or equal to {{{maxLength}}}.";
$invalidProperties[] = "invalid value for '{{name}}', the character length must be smaller than or equal to {{{maxLength}}}.";
}

{{/maxLength}}
{{#minLength}}
if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(strlen($this->container['{{name}}']) < {{minLength}})) {
$invalid_properties[] = "invalid value for '{{name}}', the character length must be bigger than or equal to {{{minLength}}}.";
$invalidProperties[] = "invalid value for '{{name}}', the character length must be bigger than or equal to {{{minLength}}}.";
}

{{/minLength}}
{{#maximum}}
if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}($this->container['{{name}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}})) {
$invalid_properties[] = "invalid value for '{{name}}', must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.";
$invalidProperties[] = "invalid value for '{{name}}', must be smaller than {{^exclusiveMaximum}}or equal to {{/exclusiveMaximum}}{{maximum}}.";
}

{{/maximum}}
{{#minimum}}
if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}($this->container['{{name}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}})) {
$invalid_properties[] = "invalid value for '{{name}}', must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.";
$invalidProperties[] = "invalid value for '{{name}}', must be bigger than {{^exclusiveMinimum}}or equal to {{/exclusiveMinimum}}{{minimum}}.";
}

{{/minimum}}
{{#pattern}}
if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}!preg_match("{{{pattern}}}", $this->container['{{name}}'])) {
$invalid_properties[] = "invalid value for '{{name}}', must be conform to the pattern {{{pattern}}}.";
$invalidProperties[] = "invalid value for '{{name}}', must be conform to the pattern {{{pattern}}}.";
}

{{/pattern}}
{{#maxItems}}
if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(count($this->container['{{name}}']) > {{maxItems}})) {
$invalid_properties[] = "invalid value for '{{name}}', number of items must be less than or equal to {{{maxItems}}}.";
$invalidProperties[] = "invalid value for '{{name}}', number of items must be less than or equal to {{{maxItems}}}.";
}

{{/maxItems}}
{{#minItems}}
if ({{^required}}!is_null($this->container['{{name}}']) && {{/required}}(count($this->container['{{name}}']) < {{minItems}})) {
$invalid_properties[] = "invalid value for '{{name}}', number of items must be greater than or equal to {{{minItems}}}.";
$invalidProperties[] = "invalid value for '{{name}}', number of items must be greater than or equal to {{{minItems}}}.";
}

{{/minItems}}
{{/hasValidation}}
{{/vars}}
return $invalid_properties;
return $invalidProperties;
}

/**
* validate all the properties in the model
* Validate all the properties in the model
* return true if all passed
*
* @return bool True if all properties are valid
Expand All @@ -259,8 +273,8 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
{{/required}}
{{#isEnum}}
{{^isContainer}}
$allowed_values = $this->{{getter}}AllowableValues();
if (!in_array($this->container['{{name}}'], $allowed_values)) {
$allowedValues = $this->{{getter}}AllowableValues();
if (!in_array($this->container['{{name}}'], $allowedValues)) {
return false;
}
{{/isContainer}}
Expand Down Expand Up @@ -310,6 +324,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple

/**
* Gets {{name}}
*
* @return {{datatype}}
*/
public function {{getter}}()
Expand All @@ -319,29 +334,31 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple

/**
* Sets {{name}}
*
* @param {{datatype}} ${{name}}{{#description}} {{{description}}}{{/description}}
*
* @return $this
*/
public function {{setter}}(${{name}})
{
{{#isEnum}}
$allowed_values = $this->{{getter}}AllowableValues();
$allowedValues = $this->{{getter}}AllowableValues();
{{^isContainer}}
if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowed_values)) {
if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowedValues)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for '{{name}}', must be one of '%s'",
implode("', '", $allowed_values)
implode("', '", $allowedValues)
)
);
}
{{/isContainer}}
{{#isContainer}}
if ({{^required}}!is_null(${{name}}) && {{/required}}array_diff(${{{name}}}, $allowed_values)) {
if ({{^required}}!is_null(${{name}}) && {{/required}}array_diff(${{{name}}}, $allowedValues)) {
throw new \InvalidArgumentException(
sprintf(
"Invalid value for '{{name}}', must be one of '%s'",
implode("', '", $allowed_values)
implode("', '", $allowedValues)
)
);
}
Expand Down Expand Up @@ -389,7 +406,9 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
{{/vars}}
/**
* Returns true if offset exists. False otherwise.
* @param integer $offset Offset
*
* @param integer $offset Offset
*
* @return boolean
*/
public function offsetExists($offset)
Expand All @@ -399,7 +418,9 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple

/**
* Gets offset.
* @param integer $offset Offset
*
* @param integer $offset Offset
*
* @return mixed
*/
public function offsetGet($offset)
Expand All @@ -409,8 +430,10 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple

/**
* Sets value based on offset.
* @param integer $offset Offset
* @param mixed $value Value to be set
*
* @param integer $offset Offset
* @param mixed $value Value to be set
*
* @return void
*/
public function offsetSet($offset, $value)
Expand All @@ -424,7 +447,9 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple

/**
* Unsets offset.
* @param integer $offset Offset
*
* @param integer $offset Offset
*
* @return void
*/
public function offsetUnset($offset)
Expand All @@ -434,14 +459,18 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple

/**
* Gets the string presentation of the object
*
* @return string
*/
public function __toString()
{
if (defined('JSON_PRETTY_PRINT')) { // use JSON pretty print
return json_encode(\{{invokerPackage}}\ObjectSerializer::sanitizeForSerialization($this), JSON_PRETTY_PRINT);
return json_encode(
ObjectSerializer::sanitizeForSerialization($this),
JSON_PRETTY_PRINT
);
}

return json_encode(\{{invokerPackage}}\ObjectSerializer::sanitizeForSerialization($this));
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,4 @@
* {{#version}}OpenAPI spec version: {{{version}}}{{/version}}
* {{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}}
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*
*/
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*
*/

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*
*/

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*
*/

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
* Generated by: https://github.com/swagger-api/swagger-codegen.git
*
*/

/**
Expand Down
Loading

0 comments on commit 8f85154

Please sign in to comment.