Skip to content

Commit 8f85154

Browse files
ackintoshwing328
authored andcommitted
[PHP] Improve Model template (#6460)
* 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
1 parent 4a89d23 commit 8f85154

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+1872
-806
lines changed

modules/swagger-codegen/src/main/resources/php/model.mustache

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,20 +21,23 @@
2121

2222
namespace {{modelPackage}};
2323
{{^isEnum}}
24+
{{^parentSchema}}
2425

2526
use \ArrayAccess;
27+
{{/parentSchema}}
2628
{{/isEnum}}
29+
use \{{invokerPackage}}\ObjectSerializer;
2730

2831
/**
2932
* {{classname}} Class Doc Comment
3033
*
31-
* @category Class
34+
* @category Class
3235
{{#description}}
3336
* @description {{description}}
3437
{{/description}}
35-
* @package {{invokerPackage}}
36-
* @author Swagger Codegen team
37-
* @link https://github.com/swagger-api/swagger-codegen
38+
* @package {{invokerPackage}}
39+
* @author Swagger Codegen team
40+
* @link https://github.com/swagger-api/swagger-codegen
3841
*/
3942
{{#isEnum}}{{>model_enum}}{{/isEnum}}{{^isEnum}}{{>model_generic}}{{/isEnum}}
4043
{{/model}}{{/models}}

modules/swagger-codegen/src/main/resources/php/model_generic.mustache

Lines changed: 63 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}implements ModelInterface, ArrayAccess
1+
class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^parentSchema}}implements ModelInterface, ArrayAccess{{/parentSchema}}
22
{
33
const DISCRIMINATOR = {{#discriminator}}'{{discriminator}}'{{/discriminator}}{{^discriminator}}null{{/discriminator}};
44

55
/**
66
* The original name of the model.
7+
*
78
* @var string
89
*/
910
protected static $swaggerModelName = '{{name}}';
1011

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

2022
/**
2123
* Array of property to format mappings. Used for (de)serialization
24+
*
2225
* @var string[]
2326
*/
2427
protected static $swaggerFormats = [
@@ -47,7 +50,9 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
4750
}
4851

4952
/**
50-
* Array of attributes where the key is the local name, and the value is the original name
53+
* Array of attributes where the key is the local name,
54+
* and the value is the original name
55+
*
5156
* @var string[]
5257
*/
5358
protected static $attributeMap = [
@@ -57,6 +62,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
5762

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

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

7683
/**
77-
* Array of attributes where the key is the local name, and the value is the original name
84+
* Array of attributes where the key is the local name,
85+
* and the value is the original name
7886
*
7987
* @return array
8088
*/
@@ -119,6 +127,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
119127
{{#vars}}{{#isEnum}}
120128
/**
121129
* Gets allowable values of the enum
130+
*
122131
* @return string[]
123132
*/
124133
public function {{getter}}AllowableValues()
@@ -130,15 +139,20 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
130139
}
131140
{{/isEnum}}{{/vars}}
132141

142+
{{^parentSchema}}
133143
/**
134144
* Associative array for storing property values
145+
*
135146
* @var mixed[]
136147
*/
137148
protected $container = [];
149+
{{/parentSchema}}
138150

139151
/**
140152
* Constructor
141-
* @param mixed[] $data Associated array of property values initializing the model
153+
*
154+
* @param mixed[] $data Associated array of property values
155+
* initializing the model
142156
*/
143157
public function __construct(array $data = null)
144158
{
@@ -165,25 +179,25 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
165179
public function listInvalidProperties()
166180
{
167181
{{#parent}}
168-
$invalid_properties = parent::listInvalidProperties();
182+
$invalidProperties = parent::listInvalidProperties();
169183
{{/parent}}
170184
{{^parent}}
171-
$invalid_properties = [];
185+
$invalidProperties = [];
172186
{{/parent}}
173187

174188
{{#vars}}
175189
{{#required}}
176190
if ($this->container['{{name}}'] === null) {
177-
$invalid_properties[] = "'{{name}}' can't be null";
191+
$invalidProperties[] = "'{{name}}' can't be null";
178192
}
179193
{{/required}}
180194
{{#isEnum}}
181195
{{^isContainer}}
182-
$allowed_values = $this->{{getter}}AllowableValues();
183-
if (!in_array($this->container['{{name}}'], $allowed_values)) {
184-
$invalid_properties[] = sprintf(
196+
$allowedValues = $this->{{getter}}AllowableValues();
197+
if (!in_array($this->container['{{name}}'], $allowedValues)) {
198+
$invalidProperties[] = sprintf(
185199
"invalid value for '{{name}}', must be one of '%s'",
186-
implode("', '", $allowed_values)
200+
implode("', '", $allowedValues)
187201
);
188202
}
189203

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

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

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

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

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

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

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

234248
{{/minItems}}
235249
{{/hasValidation}}
236250
{{/vars}}
237-
return $invalid_properties;
251+
return $invalidProperties;
238252
}
239253

240254
/**
241-
* validate all the properties in the model
255+
* Validate all the properties in the model
242256
* return true if all passed
243257
*
244258
* @return bool True if all properties are valid
@@ -259,8 +273,8 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
259273
{{/required}}
260274
{{#isEnum}}
261275
{{^isContainer}}
262-
$allowed_values = $this->{{getter}}AllowableValues();
263-
if (!in_array($this->container['{{name}}'], $allowed_values)) {
276+
$allowedValues = $this->{{getter}}AllowableValues();
277+
if (!in_array($this->container['{{name}}'], $allowedValues)) {
264278
return false;
265279
}
266280
{{/isContainer}}
@@ -310,6 +324,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
310324

311325
/**
312326
* Gets {{name}}
327+
*
313328
* @return {{datatype}}
314329
*/
315330
public function {{getter}}()
@@ -319,29 +334,31 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
319334

320335
/**
321336
* Sets {{name}}
337+
*
322338
* @param {{datatype}} ${{name}}{{#description}} {{{description}}}{{/description}}
339+
*
323340
* @return $this
324341
*/
325342
public function {{setter}}(${{name}})
326343
{
327344
{{#isEnum}}
328-
$allowed_values = $this->{{getter}}AllowableValues();
345+
$allowedValues = $this->{{getter}}AllowableValues();
329346
{{^isContainer}}
330-
if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowed_values)) {
347+
if ({{^required}}!is_null(${{name}}) && {{/required}}!in_array(${{{name}}}, $allowedValues)) {
331348
throw new \InvalidArgumentException(
332349
sprintf(
333350
"Invalid value for '{{name}}', must be one of '%s'",
334-
implode("', '", $allowed_values)
351+
implode("', '", $allowedValues)
335352
)
336353
);
337354
}
338355
{{/isContainer}}
339356
{{#isContainer}}
340-
if ({{^required}}!is_null(${{name}}) && {{/required}}array_diff(${{{name}}}, $allowed_values)) {
357+
if ({{^required}}!is_null(${{name}}) && {{/required}}array_diff(${{{name}}}, $allowedValues)) {
341358
throw new \InvalidArgumentException(
342359
sprintf(
343360
"Invalid value for '{{name}}', must be one of '%s'",
344-
implode("', '", $allowed_values)
361+
implode("', '", $allowedValues)
345362
)
346363
);
347364
}
@@ -389,7 +406,9 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
389406
{{/vars}}
390407
/**
391408
* Returns true if offset exists. False otherwise.
392-
* @param integer $offset Offset
409+
*
410+
* @param integer $offset Offset
411+
*
393412
* @return boolean
394413
*/
395414
public function offsetExists($offset)
@@ -399,7 +418,9 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
399418

400419
/**
401420
* Gets offset.
402-
* @param integer $offset Offset
421+
*
422+
* @param integer $offset Offset
423+
*
403424
* @return mixed
404425
*/
405426
public function offsetGet($offset)
@@ -409,8 +430,10 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
409430

410431
/**
411432
* Sets value based on offset.
412-
* @param integer $offset Offset
413-
* @param mixed $value Value to be set
433+
*
434+
* @param integer $offset Offset
435+
* @param mixed $value Value to be set
436+
*
414437
* @return void
415438
*/
416439
public function offsetSet($offset, $value)
@@ -424,7 +447,9 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
424447

425448
/**
426449
* Unsets offset.
427-
* @param integer $offset Offset
450+
*
451+
* @param integer $offset Offset
452+
*
428453
* @return void
429454
*/
430455
public function offsetUnset($offset)
@@ -434,14 +459,18 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}imple
434459

435460
/**
436461
* Gets the string presentation of the object
462+
*
437463
* @return string
438464
*/
439465
public function __toString()
440466
{
441467
if (defined('JSON_PRETTY_PRINT')) { // use JSON pretty print
442-
return json_encode(\{{invokerPackage}}\ObjectSerializer::sanitizeForSerialization($this), JSON_PRETTY_PRINT);
468+
return json_encode(
469+
ObjectSerializer::sanitizeForSerialization($this),
470+
JSON_PRETTY_PRINT
471+
);
443472
}
444473

445-
return json_encode(\{{invokerPackage}}\ObjectSerializer::sanitizeForSerialization($this));
474+
return json_encode(ObjectSerializer::sanitizeForSerialization($this));
446475
}
447476
}

modules/swagger-codegen/src/main/resources/php/partial_header.mustache

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,4 @@
1010
* {{#version}}OpenAPI spec version: {{{version}}}{{/version}}
1111
* {{#infoEmail}}Contact: {{{infoEmail}}}{{/infoEmail}}
1212
* Generated by: https://github.com/swagger-api/swagger-codegen.git
13-
*
1413
*/

samples/client/petstore-security-test/php/SwaggerClient-php/lib/Api/FakeApi.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
1818
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Generated by: https://github.com/swagger-api/swagger-codegen.git
20-
*
2120
*/
2221

2322
/**

samples/client/petstore-security-test/php/SwaggerClient-php/lib/ApiException.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
1818
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Generated by: https://github.com/swagger-api/swagger-codegen.git
20-
*
2120
*/
2221

2322
/**

samples/client/petstore-security-test/php/SwaggerClient-php/lib/Configuration.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
1818
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Generated by: https://github.com/swagger-api/swagger-codegen.git
20-
*
2120
*/
2221

2322
/**

samples/client/petstore-security-test/php/SwaggerClient-php/lib/HeaderSelector.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
* OpenAPI spec version: 1.0.0 *_/ ' \" =end -- \\r\\n \\n \\r
1818
* Contact: [email protected] *_/ ' \" =end -- \\r\\n \\n \\r
1919
* Generated by: https://github.com/swagger-api/swagger-codegen.git
20-
*
2120
*/
2221

2322
/**

0 commit comments

Comments
 (0)