Skip to content

Commit 4e77f4d

Browse files
authored
Merge pull request #39 from SOHELAHMED7/159-bug-giiapi-generated-rules-emailid
Resolve: Bug gii/api generated rules: email_id cebe#159
2 parents 50f5ed1 + 9c4b921 commit 4e77f4d

File tree

11 files changed

+411
-1
lines changed

11 files changed

+411
-1
lines changed

src/lib/ValidationRulesBuilder.php

+2-1
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,8 @@ private function addRulesByAttributeName(Attribute $attribute):void
142142
'~(url|site|website|href|link)~i' => 'url',
143143
];
144144
foreach ($patterns as $pattern => $validator) {
145-
if (preg_match($pattern, strtolower($attribute->columnName))) {
145+
if (empty($attribute->reference) # ignore column name based rules in case of reference/relation # https://github.com/cebe/yii2-openapi/issues/159
146+
&& preg_match($pattern, strtolower($attribute->columnName))) {
146147
$key = $attribute->columnName . '_' . $validator;
147148
$this->rules[$key] = new ValidationRule([$attribute->columnName], $validator);
148149
return;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
return [
4+
'openApiPath' => '@specs/issue_fix/159_bug_giiapi_generated_rules_emailid/index.yaml',
5+
'generateUrls' => false,
6+
'generateModels' => true,
7+
'excludeModels' => [
8+
'Error',
9+
],
10+
'generateControllers' => false,
11+
'generateMigrations' => false,
12+
'generateModelFaker' => true, // `generateModels` must be `true` in orde to use `generateModelFaker` as `true`
13+
];
14+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
2+
openapi: 3.0.3
3+
4+
info:
5+
title: 'Proxy-Service'
6+
version: 1.0.0
7+
8+
components:
9+
10+
schemas:
11+
12+
Mailing:
13+
description: Mailing
14+
type: object
15+
required:
16+
- id
17+
- name
18+
properties:
19+
id:
20+
type: integer
21+
readOnly: true
22+
name:
23+
description: name
24+
type: string
25+
maxLength: 128
26+
paymentMethodName:
27+
type: string
28+
29+
Contact:
30+
description: Contact
31+
type: object
32+
required:
33+
- id
34+
- mailing
35+
properties:
36+
id:
37+
type: integer
38+
readOnly: true
39+
mailing:
40+
$ref: '#/components/schemas/Mailing'
41+
active:
42+
type: boolean
43+
default: false
44+
nickname:
45+
type: string
46+
47+
paths:
48+
'/':
49+
get:
50+
operationId: opId
51+
summary: summary
52+
responses:
53+
'200':
54+
description: OK
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
use Faker\Factory as FakerFactory;
6+
use Faker\Generator;
7+
use Faker\UniqueGenerator;
8+
9+
/**
10+
* Base fake data generator
11+
*/
12+
abstract class BaseModelFaker
13+
{
14+
/**
15+
* @var Generator
16+
*/
17+
protected $faker;
18+
/**
19+
* @var UniqueGenerator
20+
*/
21+
protected $uniqueFaker;
22+
23+
public function __construct()
24+
{
25+
$this->faker = FakerFactory::create(str_replace('-', '_', \Yii::$app->language));
26+
$this->uniqueFaker = new UniqueGenerator($this->faker);
27+
}
28+
29+
abstract public function generateModel($attributes = []);
30+
31+
public function getFaker():Generator
32+
{
33+
return $this->faker;
34+
}
35+
36+
public function getUniqueFaker():UniqueGenerator
37+
{
38+
return $this->uniqueFaker;
39+
}
40+
41+
public function setFaker(Generator $faker):void
42+
{
43+
$this->faker = $faker;
44+
}
45+
46+
public function setUniqueFaker(UniqueGenerator $faker):void
47+
{
48+
$this->uniqueFaker = $faker;
49+
}
50+
51+
/**
52+
* Generate and return model
53+
* @param array|callable $attributes
54+
* @param UniqueGenerator|null $uniqueFaker
55+
* @return \yii\db\ActiveRecord
56+
* @example MyFaker::makeOne(['user_id' => 1, 'title' => 'foo']);
57+
* @example MyFaker::makeOne( function($model, $faker) {
58+
* $model->scenario = 'create';
59+
* $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]);
60+
* return $model;
61+
* });
62+
*/
63+
public static function makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null)
64+
{
65+
$fakeBuilder = new static();
66+
if ($uniqueFaker !== null) {
67+
$fakeBuilder->setUniqueFaker($uniqueFaker);
68+
}
69+
$model = $fakeBuilder->generateModel($attributes);
70+
return $model;
71+
}
72+
73+
/**
74+
* Generate, save and return model
75+
* @param array|callable $attributes
76+
* @param UniqueGenerator|null $uniqueFaker
77+
* @return \yii\db\ActiveRecord
78+
* @example MyFaker::saveOne(['user_id' => 1, 'title' => 'foo']);
79+
* @example MyFaker::saveOne( function($model, $faker) {
80+
* $model->scenario = 'create';
81+
* $model->setAttributes(['user_id' => 1, 'title' => $faker->sentence]);
82+
* return $model;
83+
* });
84+
*/
85+
public static function saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null)
86+
{
87+
$model = static::makeOne($attributes, $uniqueFaker);
88+
$model->save();
89+
return $model;
90+
}
91+
92+
/**
93+
* Generate and return multiple models
94+
* @param int $number
95+
* @param array|callable $commonAttributes
96+
* @return \yii\db\ActiveRecord[]|array
97+
* @example TaskFaker::make(5, ['project_id'=>1, 'user_id' => 2]);
98+
* @example TaskFaker::make(5, function($model, $faker, $uniqueFaker) {
99+
* $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]);
100+
* return $model;
101+
* });
102+
*/
103+
public static function make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array
104+
{
105+
if ($number < 1) {
106+
return [];
107+
}
108+
$fakeBuilder = new static();
109+
if ($uniqueFaker !== null) {
110+
$fakeBuilder->setUniqueFaker($uniqueFaker);
111+
}
112+
return array_map(function () use ($commonAttributes, $fakeBuilder) {
113+
$model = $fakeBuilder->generateModel($commonAttributes);
114+
return $model;
115+
}, range(0, $number -1));
116+
}
117+
118+
/**
119+
* Generate, save and return multiple models
120+
* @param int $number
121+
* @param array|callable $commonAttributes
122+
* @return \yii\db\ActiveRecord[]|array
123+
* @example TaskFaker::save(5, ['project_id'=>1, 'user_id' => 2]);
124+
* @example TaskFaker::save(5, function($model, $faker, $uniqueFaker) {
125+
* $model->setAttributes(['name' => $uniqueFaker->username, 'state'=>$faker->boolean(20)]);
126+
* return $model;
127+
* });
128+
*/
129+
public static function save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null):array
130+
{
131+
if ($number < 1) {
132+
return [];
133+
}
134+
$fakeBuilder = new static();
135+
if ($uniqueFaker !== null) {
136+
$fakeBuilder->setUniqueFaker($uniqueFaker);
137+
}
138+
return array_map(function () use ($commonAttributes, $fakeBuilder) {
139+
$model = $fakeBuilder->generateModel($commonAttributes);
140+
$model->save();
141+
return $model;
142+
}, range(0, $number -1));
143+
}
144+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
class Contact extends \app\models\base\Contact
6+
{
7+
8+
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
namespace app\models;
3+
4+
use Faker\UniqueGenerator;
5+
6+
/**
7+
* Fake data generator for Contact
8+
* @method static Contact makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
9+
* @method static Contact saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
10+
* @method static Contact[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
11+
* @method static Contact[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
12+
*/
13+
class ContactFaker extends BaseModelFaker
14+
{
15+
16+
/**
17+
* @param array|callable $attributes
18+
* @return Contact|\yii\db\ActiveRecord
19+
* @example
20+
* $model = (new PostFaker())->generateModels(['author_id' => 1]);
21+
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) {
22+
* $model->scenario = 'create';
23+
* $model->author_id = 1;
24+
* return $model;
25+
* });
26+
**/
27+
public function generateModel($attributes = [])
28+
{
29+
$faker = $this->faker;
30+
$uniqueFaker = $this->uniqueFaker;
31+
$model = new Contact();
32+
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
33+
$model->mailing_id = $faker->randomElement(\app\models\Mailing::find()->select("id")->column());
34+
$model->active = $faker->boolean;
35+
$model->nickname = $faker->sentence;
36+
if (!is_callable($attributes)) {
37+
$model->setAttributes($attributes, false);
38+
} else {
39+
$model = $attributes($model, $faker, $uniqueFaker);
40+
}
41+
return $model;
42+
}
43+
44+
public static function dependentOn()
45+
{
46+
return [
47+
// just model class names
48+
'Mailing',
49+
50+
];
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
3+
namespace app\models;
4+
5+
class Mailing extends \app\models\base\Mailing
6+
{
7+
8+
9+
}
10+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
<?php
2+
namespace app\models;
3+
4+
use Faker\UniqueGenerator;
5+
6+
/**
7+
* Fake data generator for Mailing
8+
* @method static Mailing makeOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
9+
* @method static Mailing saveOne($attributes = [], ?UniqueGenerator $uniqueFaker = null);
10+
* @method static Mailing[] make(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
11+
* @method static Mailing[] save(int $number, $commonAttributes = [], ?UniqueGenerator $uniqueFaker = null)
12+
*/
13+
class MailingFaker extends BaseModelFaker
14+
{
15+
16+
/**
17+
* @param array|callable $attributes
18+
* @return Mailing|\yii\db\ActiveRecord
19+
* @example
20+
* $model = (new PostFaker())->generateModels(['author_id' => 1]);
21+
* $model = (new PostFaker())->generateModels(function($model, $faker, $uniqueFaker) {
22+
* $model->scenario = 'create';
23+
* $model->author_id = 1;
24+
* return $model;
25+
* });
26+
**/
27+
public function generateModel($attributes = [])
28+
{
29+
$faker = $this->faker;
30+
$uniqueFaker = $this->uniqueFaker;
31+
$model = new Mailing();
32+
//$model->id = $uniqueFaker->numberBetween(0, 1000000);
33+
$model->name = substr($faker->text(128), 0, 128);
34+
$model->paymentMethodName = $faker->sentence;
35+
if (!is_callable($attributes)) {
36+
$model->setAttributes($attributes, false);
37+
} else {
38+
$model = $attributes($model, $faker, $uniqueFaker);
39+
}
40+
return $model;
41+
}
42+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?php
2+
3+
namespace app\models\base;
4+
5+
/**
6+
* Contact
7+
*
8+
* @property int $id
9+
* @property int $mailing_id Mailing
10+
* @property bool $active
11+
* @property string $nickname
12+
*
13+
* @property \app\models\Mailing $mailing
14+
*/
15+
abstract class Contact extends \yii\db\ActiveRecord
16+
{
17+
public static function tableName()
18+
{
19+
return '{{%contacts}}';
20+
}
21+
22+
public function rules()
23+
{
24+
return [
25+
'trim' => [['nickname'], 'trim'],
26+
'required' => [['mailing_id'], 'required'],
27+
'mailing_id_integer' => [['mailing_id'], 'integer'],
28+
'mailing_id_exist' => [['mailing_id'], 'exist', 'targetRelation' => 'Mailing'],
29+
'active_boolean' => [['active'], 'boolean'],
30+
'active_default' => [['active'], 'default', 'value' => false],
31+
'nickname_string' => [['nickname'], 'string'],
32+
];
33+
}
34+
35+
public function getMailing()
36+
{
37+
return $this->hasOne(\app\models\Mailing::class, ['id' => 'mailing_id']);
38+
}
39+
}

0 commit comments

Comments
 (0)