7
7
8
8
namespace cebe \yii2openapi \lib ;
9
9
10
- use cebe \yii2openapi \lib \Config ;
11
- use cebe \yii2openapi \lib \CustomSpecAttr ;
12
10
use cebe \yii2openapi \lib \exceptions \InvalidDefinitionException ;
13
11
use cebe \yii2openapi \lib \items \Attribute ;
14
12
use cebe \yii2openapi \lib \items \AttributeRelation ;
20
18
use cebe \yii2openapi \lib \openapi \ComponentSchema ;
21
19
use cebe \yii2openapi \lib \openapi \PropertySchema ;
22
20
use Yii ;
21
+ use yii \base \InvalidConfigException ;
23
22
use yii \helpers \Inflector ;
24
23
use yii \helpers \StringHelper ;
25
- use yii \helpers \VarDumper ;
26
24
use function explode ;
27
25
use function strpos ;
28
26
use function strtolower ;
@@ -63,7 +61,7 @@ class AttributeResolver
63
61
private $ schema ;
64
62
65
63
/**
66
- * @var \cebe\yii2openapi\lib\items\ JunctionSchemas
64
+ * @var JunctionSchemas
67
65
*/
68
66
private $ junctions ;
69
67
@@ -88,14 +86,14 @@ public function __construct(string $schemaName, ComponentSchema $schema, Junctio
88
86
}
89
87
90
88
/**
91
- * @return \cebe\yii2openapi\lib\items\ DbModel
92
- * @throws \cebe\yii2openapi\lib\exceptions\ InvalidDefinitionException
93
- * @throws \yii\base\ InvalidConfigException
89
+ * @return DbModel
90
+ * @throws InvalidDefinitionException
91
+ * @throws InvalidConfigException
94
92
*/
95
- public function resolve ():DbModel
93
+ public function resolve (): DbModel
96
94
{
97
95
foreach ($ this ->schema ->getProperties () as $ property ) {
98
- /** @var $property \cebe\yii2openapi\lib\openapi\ PropertySchema */
96
+ /** @var $property PropertySchema */
99
97
100
98
$ isRequired = $ this ->schema ->isRequiredProperty ($ property ->getName ());
101
99
$ nullableValue = $ property ->getProperty ()->getSerializableData ()->nullable ?? null ;
@@ -130,25 +128,25 @@ public function resolve():DbModel
130
128
}
131
129
132
130
/**
133
- * @param \cebe\yii2openapi\lib\openapi\ PropertySchema $property
134
- * @param bool $isRequired
135
- * @throws \cebe\yii2openapi\lib\exceptions\ InvalidDefinitionException
136
- * @throws \yii\base\ InvalidConfigException
131
+ * @param PropertySchema $property
132
+ * @param bool $isRequired
133
+ * @throws InvalidDefinitionException
134
+ * @throws InvalidConfigException
137
135
*/
138
- protected function resolveJunctionTableProperty (PropertySchema $ property , bool $ isRequired ):void
136
+ protected function resolveJunctionTableProperty (PropertySchema $ property , bool $ isRequired ): void
139
137
{
140
138
if ($ this ->junctions ->isJunctionProperty ($ this ->schemaName , $ property ->getName ())) {
141
139
$ junkAttribute = $ this ->junctions ->byJunctionSchema ($ this ->schemaName )[$ property ->getName ()];
142
140
$ attribute = Yii::createObject (Attribute::class, [$ property ->getName ()]);
143
141
$ attribute ->setRequired ($ isRequired )
144
- ->setDescription ($ property ->getAttr ('description ' , '' ))
145
- ->setReadOnly ($ property ->isReadonly ())
146
- ->setIsPrimary ($ property ->isPrimaryKey ())
147
- ->asReference ($ junkAttribute ['relatedClassName ' ])
148
- ->setPhpType ($ junkAttribute ['phpType ' ])
149
- ->setDbType ($ junkAttribute ['dbType ' ])
150
- ->setForeignKeyColumnName ($ property ->fkColName )
151
- ->setTableName ($ this ->schema ->resolveTableName ($ this ->schemaName ));
142
+ ->setDescription ($ property ->getAttr ('description ' , '' ))
143
+ ->setReadOnly ($ property ->isReadonly ())
144
+ ->setIsPrimary ($ property ->isPrimaryKey ())
145
+ ->asReference ($ junkAttribute ['relatedClassName ' ])
146
+ ->setPhpType ($ junkAttribute ['phpType ' ])
147
+ ->setDbType ($ junkAttribute ['dbType ' ])
148
+ ->setForeignKeyColumnName ($ property ->fkColName )
149
+ ->setTableName ($ this ->schema ->resolveTableName ($ this ->schemaName ));
152
150
$ relation = Yii::createObject (AttributeRelation::class, [
153
151
$ property ->getName (),
154
152
$ junkAttribute ['relatedTableName ' ],
@@ -163,12 +161,12 @@ protected function resolveJunctionTableProperty(PropertySchema $property, bool $
163
161
}
164
162
165
163
/**
166
- * @param \cebe\yii2openapi\lib\openapi\ PropertySchema $property
167
- * @param bool $isRequired
168
- * @throws \cebe\yii2openapi\lib\exceptions\ InvalidDefinitionException
169
- * @throws \yii\base\ InvalidConfigException
164
+ * @param PropertySchema $property
165
+ * @param bool $isRequired
166
+ * @throws InvalidDefinitionException
167
+ * @throws InvalidConfigException
170
168
*/
171
- protected function resolveHasMany2ManyTableProperty (PropertySchema $ property , bool $ isRequired ):void
169
+ protected function resolveHasMany2ManyTableProperty (PropertySchema $ property , bool $ isRequired ): void
172
170
{
173
171
if ($ this ->junctions ->isManyToManyProperty ($ this ->schemaName , $ property ->getName ())) {
174
172
return ;
@@ -198,44 +196,45 @@ protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bo
198
196
199
197
$ this ->relations [Inflector::pluralize ($ junkRef )] =
200
198
Yii::createObject (AttributeRelation::class, [$ junkRef , $ junkAttribute ['junctionTable ' ], $ viaModel ])
201
- ->asHasMany ([$ junkAttribute ['pairProperty ' ] . '_id ' => $ this ->schema ->getPkName ()]);
199
+ ->asHasMany ([$ junkAttribute ['pairProperty ' ] . '_id ' => $ this ->schema ->getPkName ()]);
202
200
return ;
203
201
}
204
202
205
203
$ this ->resolveProperty ($ property , $ isRequired );
206
204
}
207
205
208
206
/**
209
- * @param \cebe\yii2openapi\lib\openapi\ PropertySchema $property
210
- * @param bool $isRequired
211
- * @param bool|null|string $nullableValue if string then its value will be only constant `ARG_ABSENT`. Default `null` is avoided because it can be in passed value in method call
212
- * @throws \cebe\yii2openapi\lib\exceptions\ InvalidDefinitionException
213
- * @throws \yii\base\ InvalidConfigException
207
+ * @param PropertySchema $property
208
+ * @param bool $isRequired
209
+ * @param bool|null|string $nullableValue if string then its value will be only constant `ARG_ABSENT`. Default `null` is avoided because it can be in passed value in method call
210
+ * @throws InvalidDefinitionException
211
+ * @throws InvalidConfigException
214
212
*/
215
213
protected function resolveProperty (
216
214
PropertySchema $ property ,
217
- bool $ isRequired ,
218
- $ nullableValue = 'ARG_ABSENT '
219
- ):void {
215
+ bool $ isRequired ,
216
+ $ nullableValue = 'ARG_ABSENT '
217
+ ): void
218
+ {
220
219
if ($ nullableValue === 'ARG_ABSENT ' ) {
221
220
$ nullableValue = $ property ->getProperty ()->getSerializableData ()->nullable ?? null ;
222
221
}
223
222
$ attribute = Yii::createObject (Attribute::class, [$ property ->getName ()]);
224
223
$ attribute ->setRequired ($ isRequired )
225
- ->setDescription ($ property ->getAttr ('description ' , '' ))
226
- ->setReadOnly ($ property ->isReadonly ())
227
- ->setDefault ($ property ->guessDefault ())
228
- ->setXDbType ($ property ->getAttr (CustomSpecAttr::DB_TYPE ))
229
- ->setXDbDefaultExpression ($ property ->getAttr (CustomSpecAttr::DB_DEFAULT_EXPRESSION ))
230
- ->setNullable ($ nullableValue )
231
- ->setIsPrimary ($ property ->isPrimaryKey ())
232
- ->setForeignKeyColumnName ($ property ->fkColName )
233
- ->setTableName ($ this ->schema ->resolveTableName ($ this ->schemaName ));
224
+ ->setDescription ($ property ->getAttr ('description ' , '' ))
225
+ ->setReadOnly ($ property ->isReadonly ())
226
+ ->setDefault ($ property ->guessDefault ())
227
+ ->setXDbType ($ property ->getAttr (CustomSpecAttr::DB_TYPE ))
228
+ ->setXDbDefaultExpression ($ property ->getAttr (CustomSpecAttr::DB_DEFAULT_EXPRESSION ))
229
+ ->setNullable ($ nullableValue )
230
+ ->setIsPrimary ($ property ->isPrimaryKey ())
231
+ ->setForeignKeyColumnName ($ property ->fkColName )
232
+ ->setTableName ($ this ->schema ->resolveTableName ($ this ->schemaName ));
234
233
if ($ property ->isReference ()) {
235
234
if ($ property ->isVirtual ()) {
236
235
throw new InvalidDefinitionException ('References not supported for virtual attributes ' );
237
236
}
238
-
237
+
239
238
if ($ property ->isNonDbReference ()) {
240
239
$ attribute ->asNonDbReference ($ property ->getRefClassName ());
241
240
$ relation = Yii::createObject (
@@ -260,17 +259,17 @@ protected function resolveProperty(
260
259
[$ min , $ max ] = $ fkProperty ->guessMinMax ();
261
260
$ attribute ->asReference ($ relatedClassName );
262
261
$ attribute ->setPhpType ($ fkProperty ->guessPhpType ())
263
- ->setDbType ($ fkProperty ->guessDbType (true ))
264
- ->setSize ($ fkProperty ->getMaxLength ())
265
- ->setDescription ($ property ->getRefSchema ()->getDescription ())
266
- ->setDefault ($ fkProperty ->guessDefault ())
267
- ->setLimits ($ min , $ max , $ fkProperty ->getMinLength ());
262
+ ->setDbType ($ fkProperty ->guessDbType (true ))
263
+ ->setSize ($ fkProperty ->getMaxLength ())
264
+ ->setDescription ($ property ->getRefSchema ()->getDescription ())
265
+ ->setDefault ($ fkProperty ->guessDefault ())
266
+ ->setLimits ($ min , $ max , $ fkProperty ->getMinLength ());
268
267
269
268
$ relation = Yii::createObject (
270
269
AttributeRelation::class,
271
270
[$ property ->getName (), $ relatedTableName , $ relatedClassName ]
272
271
)
273
- ->asHasOne ([$ fkProperty ->getName () => $ attribute ->columnName ]);
272
+ ->asHasOne ([$ fkProperty ->getName () => $ attribute ->columnName ]);
274
273
$ relation ->onUpdateFkConstraint = $ property ->onUpdateFkConstraint ;
275
274
$ relation ->onDeleteFkConstraint = $ property ->onDeleteFkConstraint ;
276
275
if ($ property ->isRefPointerToSelf ()) {
@@ -281,10 +280,10 @@ protected function resolveProperty(
281
280
if (!$ property ->isReference () && !$ property ->hasRefItems ()) {
282
281
[$ min , $ max ] = $ property ->guessMinMax ();
283
282
$ attribute ->setIsVirtual ($ property ->isVirtual ())
284
- ->setPhpType ($ property ->guessPhpType ())
285
- ->setDbType ($ property ->guessDbType ())
286
- ->setSize ($ property ->getMaxLength ())
287
- ->setLimits ($ min , $ max , $ property ->getMinLength ());
283
+ ->setPhpType ($ property ->guessPhpType ())
284
+ ->setDbType ($ property ->guessDbType ())
285
+ ->setSize ($ property ->getMaxLength ())
286
+ ->setLimits ($ min , $ max , $ property ->getMinLength ());
288
287
if ($ property ->hasEnum ()) {
289
288
$ attribute ->setEnumValues ($ property ->getAttr ('enum ' ));
290
289
}
@@ -321,7 +320,7 @@ protected function resolveProperty(
321
320
AttributeRelation::class,
322
321
[$ property ->getName (), $ relatedTableName , $ relatedClassName ]
323
322
)
324
- ->asHasMany ([$ fkProperty ->getName () => $ fkProperty ->getName ()])->asSelfReference ();
323
+ ->asHasMany ([$ fkProperty ->getName () => $ fkProperty ->getName ()])->asSelfReference ();
325
324
return ;
326
325
}
327
326
$ foreignPk = Inflector::camel2id ($ fkProperty ->getName (), '_ ' ) . '_id ' ;
@@ -330,7 +329,7 @@ protected function resolveProperty(
330
329
AttributeRelation::class,
331
330
[$ property ->getName (), $ relatedTableName , $ relatedClassName ]
332
331
)
333
- ->asHasMany ([$ foreignPk => $ this ->schema ->getPkName ()]);
332
+ ->asHasMany ([$ foreignPk => $ this ->schema ->getPkName ()]);
334
333
return ;
335
334
}
336
335
$ relatedClassName = $ property ->getRefClassName ();
@@ -349,7 +348,7 @@ protected function resolveProperty(
349
348
AttributeRelation::class,
350
349
[$ property ->getName (), $ relatedTableName , $ relatedClassName ]
351
350
)
352
- ->asHasMany ([Inflector::camel2id ($ this ->schemaName , '_ ' ) . '_id ' => $ this ->schema ->getPkName ()]);
351
+ ->asHasMany ([Inflector::camel2id ($ this ->schemaName , '_ ' ) . '_id ' => $ this ->schema ->getPkName ()]);
353
352
return ;
354
353
}
355
354
if ($ this ->schema ->isNonDb () && $ attribute ->isReference ()) {
@@ -369,14 +368,15 @@ protected function resolveProperty(
369
368
* @param string $relatedTableName
370
369
* @param ComponentSchema $refSchema
371
370
* @return bool
372
- * @throws \yii\base\ InvalidConfigException
371
+ * @throws InvalidConfigException
373
372
*/
374
373
protected function catchManyToMany (
375
- string $ propertyName ,
376
- string $ relatedSchemaName ,
377
- string $ relatedTableName ,
374
+ string $ propertyName ,
375
+ string $ relatedSchemaName ,
376
+ string $ relatedTableName ,
378
377
ComponentSchema $ refSchema
379
- ):bool {
378
+ ): bool
379
+ {
380
380
if (strtolower (Inflector::id2camel ($ propertyName , '_ ' ))
381
381
!== strtolower (Inflector::pluralize ($ relatedSchemaName ))) {
382
382
return false ;
@@ -408,9 +408,9 @@ protected function catchManyToMany(
408
408
}
409
409
410
410
/**
411
- * @throws \yii\base\ InvalidConfigException
411
+ * @throws InvalidConfigException
412
412
*/
413
- protected function guessFakerStub (Attribute $ attribute , PropertySchema $ property ):?string
413
+ protected function guessFakerStub (Attribute $ attribute , PropertySchema $ property ): ?string
414
414
{
415
415
$ resolver = Yii::createObject (['class ' => FakerStubResolver::class], [$ attribute , $ property , $ this ->config ]);
416
416
return $ resolver ->resolve ();
@@ -419,9 +419,9 @@ protected function guessFakerStub(Attribute $attribute, PropertySchema $property
419
419
/**
420
420
* @param array $indexes
421
421
* @return array|DbIndex[]
422
- * @throws \cebe\yii2openapi\lib\exceptions\ InvalidDefinitionException
422
+ * @throws InvalidDefinitionException
423
423
*/
424
- protected function prepareIndexes (array $ indexes ):array
424
+ protected function prepareIndexes (array $ indexes ): array
425
425
{
426
426
$ dbIndexes = [];
427
427
foreach ($ indexes as $ index ) {
@@ -472,12 +472,12 @@ protected function prepareIndexes(array $indexes):array
472
472
}
473
473
474
474
/**
475
- * @param \cebe\yii2openapi\lib\openapi\ PropertySchema $property
476
- * @param \cebe\yii2openapi\lib\items\ Attribute $attribute
475
+ * @param PropertySchema $property
476
+ * @param Attribute $attribute
477
477
* @return void
478
- * @throws \yii\base\ InvalidConfigException
478
+ * @throws InvalidConfigException
479
479
*/
480
- protected function resolvePropertyRef (PropertySchema $ property , Attribute $ attribute ):void
480
+ protected function resolvePropertyRef (PropertySchema $ property , Attribute $ attribute ): void
481
481
{
482
482
$ fkProperty = new PropertySchema (
483
483
$ property ->getRefSchema ()->getSchema (),
@@ -486,11 +486,11 @@ protected function resolvePropertyRef(PropertySchema $property, Attribute $attri
486
486
);
487
487
[$ min , $ max ] = $ fkProperty ->guessMinMax ();
488
488
$ attribute ->setPhpType ($ fkProperty ->guessPhpType ())
489
- ->setDbType ($ fkProperty ->guessDbType (true ))
490
- ->setSize ($ fkProperty ->getMaxLength ())
491
- ->setDescription ($ fkProperty ->getAttr ('description ' ))
492
- ->setDefault ($ fkProperty ->guessDefault ())
493
- ->setLimits ($ min , $ max , $ fkProperty ->getMinLength ());
489
+ ->setDbType ($ fkProperty ->guessDbType (true ))
490
+ ->setSize ($ fkProperty ->getMaxLength ())
491
+ ->setDescription ($ fkProperty ->getAttr ('description ' ))
492
+ ->setDefault ($ fkProperty ->guessDefault ())
493
+ ->setLimits ($ min , $ max , $ fkProperty ->getMinLength ());
494
494
$ this ->attributes [$ property ->getName ()] =
495
495
$ attribute ->setFakerStub ($ this ->guessFakerStub ($ attribute , $ fkProperty ));
496
496
}
0 commit comments