From 665b773e14567c5a0012ef639b08ab70fe16420b Mon Sep 17 00:00:00 2001 From: Carsten Brandt Date: Wed, 26 Jun 2019 12:34:41 +0200 Subject: [PATCH] basic support for generating validation rules --- src/generator/ApiGenerator.php | 10 +++- src/generator/default/model.php | 50 +++++++++++++++++++ tests/specs/petstore.yaml | 1 + tests/specs/petstore/models/Pet.php | 9 ++++ tests/specs/petstore_arrayref.yaml | 1 + tests/specs/petstore_arrayref/models/Pet.php | 9 ++++ .../specs/petstore_namespace/mymodels/Pet.php | 9 ++++ tests/specs/petstore_wrapped.yaml | 1 + tests/specs/petstore_wrapped/models/Pet.php | 9 ++++ 9 files changed, 98 insertions(+), 1 deletion(-) diff --git a/src/generator/ApiGenerator.php b/src/generator/ApiGenerator.php index 32782a87..02ce1563 100644 --- a/src/generator/ApiGenerator.php +++ b/src/generator/ApiGenerator.php @@ -625,15 +625,23 @@ protected function generateModels() $type = $type[0]; } - $attributes[] = [ + $attributes[$name] = [ 'name' => $name, 'type' => $type, 'dbType' => $dbType, 'dbName' => $dbName, + 'required' => false, + 'readOnly' => $resolvedProperty->readOnly ?? false, 'description' => $resolvedProperty->description, 'faker' => $this->guessModelFaker($name, $type, $resolvedProperty), ]; } + foreach ($schema->required as $property) { + if (!isset($attributes[$property])) { + continue; + } + $attributes[$property]['required'] = true; + } $models[$schemaName] = [ 'name' => $schemaName, diff --git a/src/generator/default/model.php b/src/generator/default/model.php index 42b8115c..0d26f402 100644 --- a/src/generator/default/model.php +++ b/src/generator/default/model.php @@ -19,6 +19,56 @@ public static function tableName() return ; } + public function rules() + { + return [ + + ]; + } + $relation): ?> public function get() { diff --git a/tests/specs/petstore.yaml b/tests/specs/petstore.yaml index 977c596a..29a79433 100644 --- a/tests/specs/petstore.yaml +++ b/tests/specs/petstore.yaml @@ -90,6 +90,7 @@ components: id: type: integer format: int64 + readOnly: True name: type: string tag: diff --git a/tests/specs/petstore/models/Pet.php b/tests/specs/petstore/models/Pet.php index 97f167c4..70a18e74 100644 --- a/tests/specs/petstore/models/Pet.php +++ b/tests/specs/petstore/models/Pet.php @@ -16,4 +16,13 @@ public static function tableName() return '{{%pets}}'; } + public function rules() + { + return [ + [['name', 'tag'], 'trim'], + [['name'], 'required'], + [['name', 'tag'], 'string'], + ]; + } + } diff --git a/tests/specs/petstore_arrayref.yaml b/tests/specs/petstore_arrayref.yaml index d494b104..630e67f0 100644 --- a/tests/specs/petstore_arrayref.yaml +++ b/tests/specs/petstore_arrayref.yaml @@ -86,6 +86,7 @@ components: id: type: integer format: int64 + readOnly: True name: type: string tag: diff --git a/tests/specs/petstore_arrayref/models/Pet.php b/tests/specs/petstore_arrayref/models/Pet.php index 97f167c4..70a18e74 100644 --- a/tests/specs/petstore_arrayref/models/Pet.php +++ b/tests/specs/petstore_arrayref/models/Pet.php @@ -16,4 +16,13 @@ public static function tableName() return '{{%pets}}'; } + public function rules() + { + return [ + [['name', 'tag'], 'trim'], + [['name'], 'required'], + [['name', 'tag'], 'string'], + ]; + } + } diff --git a/tests/specs/petstore_namespace/mymodels/Pet.php b/tests/specs/petstore_namespace/mymodels/Pet.php index 476e31a6..1536d4a7 100644 --- a/tests/specs/petstore_namespace/mymodels/Pet.php +++ b/tests/specs/petstore_namespace/mymodels/Pet.php @@ -16,4 +16,13 @@ public static function tableName() return '{{%pets}}'; } + public function rules() + { + return [ + [['name', 'tag'], 'trim'], + [['name'], 'required'], + [['name', 'tag'], 'string'], + ]; + } + } diff --git a/tests/specs/petstore_wrapped.yaml b/tests/specs/petstore_wrapped.yaml index a18ad264..dde7427c 100644 --- a/tests/specs/petstore_wrapped.yaml +++ b/tests/specs/petstore_wrapped.yaml @@ -98,6 +98,7 @@ components: id: type: integer format: int64 + readOnly: True name: type: string tag: diff --git a/tests/specs/petstore_wrapped/models/Pet.php b/tests/specs/petstore_wrapped/models/Pet.php index 97f167c4..70a18e74 100644 --- a/tests/specs/petstore_wrapped/models/Pet.php +++ b/tests/specs/petstore_wrapped/models/Pet.php @@ -16,4 +16,13 @@ public static function tableName() return '{{%pets}}'; } + public function rules() + { + return [ + [['name', 'tag'], 'trim'], + [['name'], 'required'], + [['name', 'tag'], 'string'], + ]; + } + }