Skip to content

Commit 665b773

Browse files
committed
basic support for generating validation rules
1 parent 9bff78d commit 665b773

File tree

9 files changed

+98
-1
lines changed

9 files changed

+98
-1
lines changed

src/generator/ApiGenerator.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -625,15 +625,23 @@ protected function generateModels()
625625
$type = $type[0];
626626
}
627627

628-
$attributes[] = [
628+
$attributes[$name] = [
629629
'name' => $name,
630630
'type' => $type,
631631
'dbType' => $dbType,
632632
'dbName' => $dbName,
633+
'required' => false,
634+
'readOnly' => $resolvedProperty->readOnly ?? false,
633635
'description' => $resolvedProperty->description,
634636
'faker' => $this->guessModelFaker($name, $type, $resolvedProperty),
635637
];
636638
}
639+
foreach ($schema->required as $property) {
640+
if (!isset($attributes[$property])) {
641+
continue;
642+
}
643+
$attributes[$property]['required'] = true;
644+
}
637645

638646
$models[$schemaName] = [
639647
'name' => $schemaName,

src/generator/default/model.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,56 @@ public static function tableName()
1919
return <?= var_export($tableName) ?>;
2020
}
2121

22+
public function rules()
23+
{
24+
return [
25+
<?php
26+
$safeAttributes = [];
27+
$requiredAttributes = [];
28+
$integerAttributes = [];
29+
$stringAttributes = [];
30+
31+
foreach ($attributes as $attribute) {
32+
if ($attribute['readOnly']) {
33+
continue;
34+
}
35+
if ($attribute['required']) {
36+
$requiredAttributes[$attribute['name']] = $attribute['name'];
37+
}
38+
switch ($attribute['type']) {
39+
case 'integer':
40+
$integerAttributes[$attribute['name']] = $attribute['name'];
41+
break;
42+
case 'string':
43+
$stringAttributes[$attribute['name']] = $attribute['name'];
44+
break;
45+
default:
46+
case 'array':
47+
$safeAttributes[$attribute['name']] = $attribute['name'];
48+
break;
49+
}
50+
}
51+
if (!empty($stringAttributes)) {
52+
echo " [['" . implode("', '", $stringAttributes) . "'], 'trim'],\n";
53+
}
54+
if (!empty($requiredAttributes)) {
55+
echo " [['" . implode("', '", $requiredAttributes) . "'], 'required'],\n";
56+
}
57+
if (!empty($stringAttributes)) {
58+
echo " [['" . implode("', '", $stringAttributes) . "'], 'string'],\n";
59+
}
60+
if (!empty($integerAttributes)) {
61+
echo " [['" . implode("', '", $integerAttributes) . "'], 'integer'],\n";
62+
}
63+
if (!empty($safeAttributes)) {
64+
echo " // TODO define more concreate validation rules!\n";
65+
echo " [['" . implode("','", $safeAttributes) . "'], 'safe'],\n";
66+
}
67+
68+
?>
69+
];
70+
}
71+
2272
<?php foreach ($relations as $relationName => $relation): ?>
2373
public function get<?= ucfirst($relationName) ?>()
2474
{

tests/specs/petstore.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,7 @@ components:
9090
id:
9191
type: integer
9292
format: int64
93+
readOnly: True
9394
name:
9495
type: string
9596
tag:

tests/specs/petstore/models/Pet.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,13 @@ public static function tableName()
1616
return '{{%pets}}';
1717
}
1818

19+
public function rules()
20+
{
21+
return [
22+
[['name', 'tag'], 'trim'],
23+
[['name'], 'required'],
24+
[['name', 'tag'], 'string'],
25+
];
26+
}
27+
1928
}

tests/specs/petstore_arrayref.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@ components:
8686
id:
8787
type: integer
8888
format: int64
89+
readOnly: True
8990
name:
9091
type: string
9192
tag:

tests/specs/petstore_arrayref/models/Pet.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,13 @@ public static function tableName()
1616
return '{{%pets}}';
1717
}
1818

19+
public function rules()
20+
{
21+
return [
22+
[['name', 'tag'], 'trim'],
23+
[['name'], 'required'],
24+
[['name', 'tag'], 'string'],
25+
];
26+
}
27+
1928
}

tests/specs/petstore_namespace/mymodels/Pet.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,13 @@ public static function tableName()
1616
return '{{%pets}}';
1717
}
1818

19+
public function rules()
20+
{
21+
return [
22+
[['name', 'tag'], 'trim'],
23+
[['name'], 'required'],
24+
[['name', 'tag'], 'string'],
25+
];
26+
}
27+
1928
}

tests/specs/petstore_wrapped.yaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@ components:
9898
id:
9999
type: integer
100100
format: int64
101+
readOnly: True
101102
name:
102103
type: string
103104
tag:

tests/specs/petstore_wrapped/models/Pet.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,13 @@ public static function tableName()
1616
return '{{%pets}}';
1717
}
1818

19+
public function rules()
20+
{
21+
return [
22+
[['name', 'tag'], 'trim'],
23+
[['name'], 'required'],
24+
[['name', 'tag'], 'string'],
25+
];
26+
}
27+
1928
}

0 commit comments

Comments
 (0)