Skip to content
This repository has been archived by the owner on Jun 4, 2024. It is now read-only.

Commit

Permalink
basic support for generating validation rules
Browse files Browse the repository at this point in the history
  • Loading branch information
cebe committed Jun 26, 2019
1 parent 9bff78d commit 665b773
Show file tree
Hide file tree
Showing 9 changed files with 98 additions and 1 deletion.
10 changes: 9 additions & 1 deletion src/generator/ApiGenerator.php
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
50 changes: 50 additions & 0 deletions src/generator/default/model.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,56 @@ public static function tableName()
return <?= var_export($tableName) ?>;
}

public function rules()
{
return [
<?php
$safeAttributes = [];
$requiredAttributes = [];
$integerAttributes = [];
$stringAttributes = [];

foreach ($attributes as $attribute) {
if ($attribute['readOnly']) {
continue;
}
if ($attribute['required']) {
$requiredAttributes[$attribute['name']] = $attribute['name'];
}
switch ($attribute['type']) {
case 'integer':
$integerAttributes[$attribute['name']] = $attribute['name'];
break;
case 'string':
$stringAttributes[$attribute['name']] = $attribute['name'];
break;
default:
case 'array':
$safeAttributes[$attribute['name']] = $attribute['name'];
break;
}
}
if (!empty($stringAttributes)) {
echo " [['" . implode("', '", $stringAttributes) . "'], 'trim'],\n";
}
if (!empty($requiredAttributes)) {
echo " [['" . implode("', '", $requiredAttributes) . "'], 'required'],\n";
}
if (!empty($stringAttributes)) {
echo " [['" . implode("', '", $stringAttributes) . "'], 'string'],\n";
}
if (!empty($integerAttributes)) {
echo " [['" . implode("', '", $integerAttributes) . "'], 'integer'],\n";
}
if (!empty($safeAttributes)) {
echo " // TODO define more concreate validation rules!\n";
echo " [['" . implode("','", $safeAttributes) . "'], 'safe'],\n";
}

?>
];
}

<?php foreach ($relations as $relationName => $relation): ?>
public function get<?= ucfirst($relationName) ?>()
{
Expand Down
1 change: 1 addition & 0 deletions tests/specs/petstore.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ components:
id:
type: integer
format: int64
readOnly: True
name:
type: string
tag:
Expand Down
9 changes: 9 additions & 0 deletions tests/specs/petstore/models/Pet.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ public static function tableName()
return '{{%pets}}';
}

public function rules()
{
return [
[['name', 'tag'], 'trim'],
[['name'], 'required'],
[['name', 'tag'], 'string'],
];
}

}
1 change: 1 addition & 0 deletions tests/specs/petstore_arrayref.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ components:
id:
type: integer
format: int64
readOnly: True
name:
type: string
tag:
Expand Down
9 changes: 9 additions & 0 deletions tests/specs/petstore_arrayref/models/Pet.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ public static function tableName()
return '{{%pets}}';
}

public function rules()
{
return [
[['name', 'tag'], 'trim'],
[['name'], 'required'],
[['name', 'tag'], 'string'],
];
}

}
9 changes: 9 additions & 0 deletions tests/specs/petstore_namespace/mymodels/Pet.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ public static function tableName()
return '{{%pets}}';
}

public function rules()
{
return [
[['name', 'tag'], 'trim'],
[['name'], 'required'],
[['name', 'tag'], 'string'],
];
}

}
1 change: 1 addition & 0 deletions tests/specs/petstore_wrapped.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ components:
id:
type: integer
format: int64
readOnly: True
name:
type: string
tag:
Expand Down
9 changes: 9 additions & 0 deletions tests/specs/petstore_wrapped/models/Pet.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,13 @@ public static function tableName()
return '{{%pets}}';
}

public function rules()
{
return [
[['name', 'tag'], 'trim'],
[['name'], 'required'],
[['name', 'tag'], 'string'],
];
}

}

0 comments on commit 665b773

Please sign in to comment.