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

+1-59
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

+1-2
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

+1-2
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

+1-5
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

+1-5
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

+1-2
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

+1-2
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

+1-2
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

+1-2
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

+1-2
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

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

+1-5
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ public function listInvalidProperties()
194194
*/
195195
public function valid()
196196
{
197-
if (!parent::valid()) {
198-
return false;
199-
}
200-
201-
return true;
197+
return count($this->listInvalidProperties()) === 0;
202198
}
203199

204200

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

+1-2
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/ClassModel.php

+1-2
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/Client.php

+1-2
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/Dog.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,7 @@ public function listInvalidProperties()
194194
*/
195195
public function valid()
196196
{
197-
if (!parent::valid()) {
198-
return false;
199-
}
200-
201-
return true;
197+
return count($this->listInvalidProperties()) === 0;
202198
}
203199

204200

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

+1-6
Original file line numberDiff line numberDiff line change
@@ -244,12 +244,7 @@ public function listInvalidProperties()
244244
*/
245245
public function valid()
246246
{
247-
248-
$allowedValues = $this->getJustSymbolAllowableValues();
249-
if (!is_null($this->container['just_symbol']) && !in_array($this->container['just_symbol'], $allowedValues, true)) {
250-
return false;
251-
}
252-
return true;
247+
return count($this->listInvalidProperties()) === 0;
253248
}
254249

255250

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

+1-21
Original file line numberDiff line numberDiff line change
@@ -323,27 +323,7 @@ public function listInvalidProperties()
323323
*/
324324
public function valid()
325325
{
326-
327-
$allowedValues = $this->getEnumStringAllowableValues();
328-
if (!is_null($this->container['enum_string']) && !in_array($this->container['enum_string'], $allowedValues, true)) {
329-
return false;
330-
}
331-
if ($this->container['enum_string_required'] === null) {
332-
return false;
333-
}
334-
$allowedValues = $this->getEnumStringRequiredAllowableValues();
335-
if (!is_null($this->container['enum_string_required']) && !in_array($this->container['enum_string_required'], $allowedValues, true)) {
336-
return false;
337-
}
338-
$allowedValues = $this->getEnumIntegerAllowableValues();
339-
if (!is_null($this->container['enum_integer']) && !in_array($this->container['enum_integer'], $allowedValues, true)) {
340-
return false;
341-
}
342-
$allowedValues = $this->getEnumNumberAllowableValues();
343-
if (!is_null($this->container['enum_number']) && !in_array($this->container['enum_number'], $allowedValues, true)) {
344-
return false;
345-
}
346-
return true;
326+
return count($this->listInvalidProperties()) === 0;
347327
}
348328

349329

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

+1-56
Original file line numberDiff line numberDiff line change
@@ -340,62 +340,7 @@ public function listInvalidProperties()
340340
*/
341341
public function valid()
342342
{
343-
344-
if ($this->container['integer'] > 100) {
345-
return false;
346-
}
347-
if ($this->container['integer'] < 10) {
348-
return false;
349-
}
350-
if ($this->container['int32'] > 200) {
351-
return false;
352-
}
353-
if ($this->container['int32'] < 20) {
354-
return false;
355-
}
356-
if ($this->container['number'] === null) {
357-
return false;
358-
}
359-
if ($this->container['number'] > 543.2) {
360-
return false;
361-
}
362-
if ($this->container['number'] < 32.1) {
363-
return false;
364-
}
365-
if ($this->container['float'] > 987.6) {
366-
return false;
367-
}
368-
if ($this->container['float'] < 54.3) {
369-
return false;
370-
}
371-
if ($this->container['double'] > 123.4) {
372-
return false;
373-
}
374-
if ($this->container['double'] < 67.8) {
375-
return false;
376-
}
377-
if (!preg_match("/[a-z]/i", $this->container['string'])) {
378-
return false;
379-
}
380-
if ($this->container['byte'] === null) {
381-
return false;
382-
}
383-
if (!preg_match("/^(?:[A-Za-z0-9+\/]{4})*(?:[A-Za-z0-9+\/]{2}==|[A-Za-z0-9+\/]{3}=)?$/", $this->container['byte'])) {
384-
return false;
385-
}
386-
if ($this->container['date'] === null) {
387-
return false;
388-
}
389-
if ($this->container['password'] === null) {
390-
return false;
391-
}
392-
if (mb_strlen($this->container['password']) > 64) {
393-
return false;
394-
}
395-
if (mb_strlen($this->container['password']) < 10) {
396-
return false;
397-
}
398-
return true;
343+
return count($this->listInvalidProperties()) === 0;
399344
}
400345

401346

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

+1-2
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/MapTest.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,7 @@ public function listInvalidProperties()
221221
*/
222222
public function valid()
223223
{
224-
225-
return true;
224+
return count($this->listInvalidProperties()) === 0;
226225
}
227226

228227

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

+1-2
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/Model200Response.php

+1-2
Original file line numberDiff line numberDiff line change
@@ -207,8 +207,7 @@ public function listInvalidProperties()
207207
*/
208208
public function valid()
209209
{
210-
211-
return true;
210+
return count($this->listInvalidProperties()) === 0;
212211
}
213212

214213

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

+1-2
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/ModelReturn.php

+1-2
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/Name.php

+1-5
Original file line numberDiff line numberDiff line change
@@ -222,11 +222,7 @@ public function listInvalidProperties()
222222
*/
223223
public function valid()
224224
{
225-
226-
if ($this->container['name'] === null) {
227-
return false;
228-
}
229-
return true;
225+
return count($this->listInvalidProperties()) === 0;
230226
}
231227

232228

0 commit comments

Comments
 (0)