Skip to content

Commit f9ae84d

Browse files
ackintoshwing328
authored andcommitted
[PHP] Improve duplicated validation logic (#7954)
* Improve duplicated validation logic * Update the samples
1 parent 4470615 commit f9ae84d

36 files changed

+36
-235
lines changed

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

Lines changed: 1 addition & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -259,65 +259,7 @@ class {{classname}} {{#parentSchema}}extends {{{parent}}} {{/parentSchema}}{{^pa
259259
*/
260260
public function valid()
261261
{
262-
{{#parent}}
263-
if (!parent::valid()) {
264-
return false;
265-
}
266-
{{/parent}}
267-
268-
{{#vars}}
269-
{{#required}}
270-
if ($this->container['{{name}}'] === null) {
271-
return false;
272-
}
273-
{{/required}}
274-
{{#isEnum}}
275-
{{^isContainer}}
276-
$allowedValues = $this->{{getter}}AllowableValues();
277-
if (!is_null($this->container['{{name}}']) && !in_array($this->container['{{name}}'], $allowedValues, true)) {
278-
return false;
279-
}
280-
{{/isContainer}}
281-
{{/isEnum}}
282-
{{#hasValidation}}
283-
{{#maxLength}}
284-
if (mb_strlen($this->container['{{name}}']) > {{maxLength}}) {
285-
return false;
286-
}
287-
{{/maxLength}}
288-
{{#minLength}}
289-
if (mb_strlen($this->container['{{name}}']) < {{minLength}}) {
290-
return false;
291-
}
292-
{{/minLength}}
293-
{{#maximum}}
294-
if ($this->container['{{name}}'] >{{#exclusiveMaximum}}={{/exclusiveMaximum}} {{maximum}}) {
295-
return false;
296-
}
297-
{{/maximum}}
298-
{{#minimum}}
299-
if ($this->container['{{name}}'] <{{#exclusiveMinimum}}={{/exclusiveMinimum}} {{minimum}}) {
300-
return false;
301-
}
302-
{{/minimum}}
303-
{{#pattern}}
304-
if (!preg_match("{{{pattern}}}", $this->container['{{name}}'])) {
305-
return false;
306-
}
307-
{{/pattern}}
308-
{{#maxItems}}
309-
if (count($this->container['{{name}}']) > {{maxItems}}) {
310-
return false;
311-
}
312-
{{/maxItems}}
313-
{{#minItems}}
314-
if (count($this->container['{{name}}']) < {{minItems}}) {
315-
return false;
316-
}
317-
{{/minItems}}
318-
{{/hasValidation}}
319-
{{/vars}}
320-
return true;
262+
return count($this->listInvalidProperties()) === 0;
321263
}
322264

323265
{{#vars}}

samples/client/petstore-security-test/php/SwaggerClient-php/lib/Model/ModelReturn.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -201,8 +201,7 @@ public function listInvalidProperties()
201201
*/
202202
public function valid()
203203
{
204-
205-
return true;
204+
return count($this->listInvalidProperties()) === 0;
206205
}
207206

208207

samples/client/petstore/php/SwaggerClient-php/lib/Model/AdditionalPropertiesClass.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,8 +206,7 @@ public function listInvalidProperties()
206206
*/
207207
public function valid()
208208
{
209-
210-
return true;
209+
return count($this->listInvalidProperties()) === 0;
211210
}
212211

213212

samples/client/petstore/php/SwaggerClient-php/lib/Model/Animal.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -213,11 +213,7 @@ public function listInvalidProperties()
213213
*/
214214
public function valid()
215215
{
216-
217-
if ($this->container['class_name'] === null) {
218-
return false;
219-
}
220-
return true;
216+
return count($this->listInvalidProperties()) === 0;
221217
}
222218

223219

samples/client/petstore/php/SwaggerClient-php/lib/Model/AnimalFarm.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,7 @@ public function listInvalidProperties()
199199
*/
200200
public function valid()
201201
{
202-
if (!parent::valid()) {
203-
return false;
204-
}
205-
206-
return true;
202+
return count($this->listInvalidProperties()) === 0;
207203
}
208204

209205
/**

samples/client/petstore/php/SwaggerClient-php/lib/Model/ApiResponse.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ public function listInvalidProperties()
212212
*/
213213
public function valid()
214214
{
215-
216-
return true;
215+
return count($this->listInvalidProperties()) === 0;
217216
}
218217

219218

samples/client/petstore/php/SwaggerClient-php/lib/Model/ArrayOfArrayOfNumberOnly.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ public function listInvalidProperties()
200200
*/
201201
public function valid()
202202
{
203-
204-
return true;
203+
return count($this->listInvalidProperties()) === 0;
205204
}
206205

207206

samples/client/petstore/php/SwaggerClient-php/lib/Model/ArrayOfNumberOnly.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,8 +200,7 @@ public function listInvalidProperties()
200200
*/
201201
public function valid()
202202
{
203-
204-
return true;
203+
return count($this->listInvalidProperties()) === 0;
205204
}
206205

207206

samples/client/petstore/php/SwaggerClient-php/lib/Model/ArrayTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ public function listInvalidProperties()
212212
*/
213213
public function valid()
214214
{
215-
216-
return true;
215+
return count($this->listInvalidProperties()) === 0;
217216
}
218217

219218

samples/client/petstore/php/SwaggerClient-php/lib/Model/Capitalization.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,7 @@ public function listInvalidProperties()
230230
*/
231231
public function valid()
232232
{
233-
234-
return true;
233+
return count($this->listInvalidProperties()) === 0;
235234
}
236235

237236

0 commit comments

Comments
 (0)