77
88namespace cebe \yii2openapi \lib ;
99
10- use cebe \yii2openapi \lib \Config ;
11- use cebe \yii2openapi \lib \CustomSpecAttr ;
1210use cebe \yii2openapi \lib \exceptions \InvalidDefinitionException ;
1311use cebe \yii2openapi \lib \items \Attribute ;
1412use cebe \yii2openapi \lib \items \AttributeRelation ;
2018use cebe \yii2openapi \lib \openapi \ComponentSchema ;
2119use cebe \yii2openapi \lib \openapi \PropertySchema ;
2220use Yii ;
21+ use yii \base \InvalidConfigException ;
2322use yii \helpers \Inflector ;
2423use yii \helpers \StringHelper ;
25- use yii \helpers \VarDumper ;
2624use function explode ;
2725use function strpos ;
2826use function strtolower ;
@@ -32,49 +30,34 @@ class AttributeResolver
3230 /**
3331 * @var Attribute[]|array
3432 */
35- private $ attributes = [];
33+ private array $ attributes = [];
3634
3735 /**
3836 * @var AttributeRelation[]|array
3937 */
40- private $ relations = [];
38+ private array $ relations = [];
4139 /**
4240 * @var NonDbRelation[]|array
4341 */
44- private $ nonDbRelations = [];
42+ private array $ nonDbRelations = [];
4543 /**
4644 * @var ManyToManyRelation[]|array
4745 */
48- private $ many2many = [];
46+ private array $ many2many = [];
4947
50- /**
51- * @var string
52- */
53- private $ schemaName ;
48+ private string $ schemaName ;
5449
55- /**
56- * @var string
57- */
58- private $ tableName ;
50+ private string $ tableName ;
5951
60- /**
61- * @var ComponentSchema
62- */
63- private $ schema ;
52+ private ComponentSchema $ schema ;
6453
65- /**
66- * @var \cebe\yii2openapi\lib\items\JunctionSchemas
67- */
68- private $ junctions ;
54+ private JunctionSchemas $ junctions ;
6955
70- /** @var bool */
71- private $ isJunctionSchema ;
56+ private bool $ isJunctionSchema ;
7257
73- /** @var bool */
74- private $ hasMany2Many ;
58+ private bool $ hasMany2Many ;
7559
76- /** @var Config */
77- private $ config ;
60+ private ?Config $ config ;
7861
7962 public function __construct (string $ schemaName , ComponentSchema $ schema , JunctionSchemas $ junctions , ?Config $ config = null )
8063 {
@@ -88,14 +71,14 @@ public function __construct(string $schemaName, ComponentSchema $schema, Junctio
8871 }
8972
9073 /**
91- * @return \cebe\yii2openapi\lib\items\ DbModel
92- * @throws \cebe\yii2openapi\lib\exceptions\ InvalidDefinitionException
93- * @throws \yii\base\ InvalidConfigException
74+ * @return DbModel
75+ * @throws InvalidDefinitionException
76+ * @throws InvalidConfigException
9477 */
95- public function resolve ():DbModel
78+ public function resolve (): DbModel
9679 {
9780 foreach ($ this ->schema ->getProperties () as $ property ) {
98- /** @var $property \cebe\yii2openapi\lib\openapi\ PropertySchema */
81+ /** @var $property PropertySchema */
9982
10083 $ isRequired = $ this ->schema ->isRequiredProperty ($ property ->getName ());
10184 $ nullableValue = $ property ->getProperty ()->getSerializableData ()->nullable ?? null ;
@@ -130,24 +113,24 @@ public function resolve():DbModel
130113 }
131114
132115 /**
133- * @param \cebe\yii2openapi\lib\openapi\ PropertySchema $property
134- * @param bool $isRequired
135- * @throws \cebe\yii2openapi\lib\exceptions\ InvalidDefinitionException
136- * @throws \yii\base\ InvalidConfigException
116+ * @param PropertySchema $property
117+ * @param bool $isRequired
118+ * @throws InvalidDefinitionException
119+ * @throws InvalidConfigException
137120 */
138- protected function resolveJunctionTableProperty (PropertySchema $ property , bool $ isRequired ):void
121+ protected function resolveJunctionTableProperty (PropertySchema $ property , bool $ isRequired ): void
139122 {
140123 if ($ this ->junctions ->isJunctionProperty ($ this ->schemaName , $ property ->getName ())) {
141124 $ junkAttribute = $ this ->junctions ->byJunctionSchema ($ this ->schemaName )[$ property ->getName ()];
142125 $ attribute = Yii::createObject (Attribute::class, [$ property ->getName ()]);
143126 $ 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 );
127+ ->setDescription ($ property ->getAttr ('description ' , '' ))
128+ ->setReadOnly ($ property ->isReadonly ())
129+ ->setIsPrimary ($ property ->isPrimaryKey ())
130+ ->asReference ($ junkAttribute ['relatedClassName ' ])
131+ ->setPhpType ($ junkAttribute ['phpType ' ])
132+ ->setDbType ($ junkAttribute ['dbType ' ])
133+ ->setForeignKeyColumnName ($ property ->fkColName );
151134 $ relation = Yii::createObject (AttributeRelation::class, [
152135 $ property ->getName (),
153136 $ junkAttribute ['relatedTableName ' ],
@@ -162,12 +145,12 @@ protected function resolveJunctionTableProperty(PropertySchema $property, bool $
162145 }
163146
164147 /**
165- * @param \cebe\yii2openapi\lib\openapi\ PropertySchema $property
166- * @param bool $isRequired
167- * @throws \cebe\yii2openapi\lib\exceptions\ InvalidDefinitionException
168- * @throws \yii\base\ InvalidConfigException
148+ * @param PropertySchema $property
149+ * @param bool $isRequired
150+ * @throws InvalidDefinitionException
151+ * @throws InvalidConfigException
169152 */
170- protected function resolveHasMany2ManyTableProperty (PropertySchema $ property , bool $ isRequired ):void
153+ protected function resolveHasMany2ManyTableProperty (PropertySchema $ property , bool $ isRequired ): void
171154 {
172155 if ($ this ->junctions ->isManyToManyProperty ($ this ->schemaName , $ property ->getName ())) {
173156 return ;
@@ -197,43 +180,44 @@ protected function resolveHasMany2ManyTableProperty(PropertySchema $property, bo
197180
198181 $ this ->relations [Inflector::pluralize ($ junkRef )] =
199182 Yii::createObject (AttributeRelation::class, [$ junkRef , $ junkAttribute ['junctionTable ' ], $ viaModel ])
200- ->asHasMany ([$ junkAttribute ['pairProperty ' ] . '_id ' => $ this ->schema ->getPkName ()]);
183+ ->asHasMany ([$ junkAttribute ['pairProperty ' ] . '_id ' => $ this ->schema ->getPkName ()]);
201184 return ;
202185 }
203186
204187 $ this ->resolveProperty ($ property , $ isRequired );
205188 }
206189
207190 /**
208- * @param \cebe\yii2openapi\lib\openapi\ PropertySchema $property
209- * @param bool $isRequired
210- * @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
211- * @throws \cebe\yii2openapi\lib\exceptions\ InvalidDefinitionException
212- * @throws \yii\base\ InvalidConfigException
191+ * @param PropertySchema $property
192+ * @param bool $isRequired
193+ * @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
194+ * @throws InvalidDefinitionException
195+ * @throws InvalidConfigException
213196 */
214197 protected function resolveProperty (
215198 PropertySchema $ property ,
216199 bool $ isRequired ,
217- $ nullableValue = 'ARG_ABSENT '
218- ):void {
200+ $ nullableValue = 'ARG_ABSENT '
201+ ): void
202+ {
219203 if ($ nullableValue === 'ARG_ABSENT ' ) {
220204 $ nullableValue = $ property ->getProperty ()->getSerializableData ()->nullable ?? null ;
221205 }
222206 $ attribute = Yii::createObject (Attribute::class, [$ property ->getName ()]);
223207 $ attribute ->setRequired ($ isRequired )
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 );
208+ ->setDescription ($ property ->getAttr ('description ' , '' ))
209+ ->setReadOnly ($ property ->isReadonly ())
210+ ->setDefault ($ property ->guessDefault ())
211+ ->setXDbType ($ property ->getAttr (CustomSpecAttr::DB_TYPE ))
212+ ->setXDbDefaultExpression ($ property ->getAttr (CustomSpecAttr::DB_DEFAULT_EXPRESSION ))
213+ ->setNullable ($ nullableValue )
214+ ->setIsPrimary ($ property ->isPrimaryKey ())
215+ ->setForeignKeyColumnName ($ property ->fkColName );
232216 if ($ property ->isReference ()) {
233217 if ($ property ->isVirtual ()) {
234218 throw new InvalidDefinitionException ('References not supported for virtual attributes ' );
235219 }
236-
220+
237221 if ($ property ->isNonDbReference ()) {
238222 $ attribute ->asNonDbReference ($ property ->getRefClassName ());
239223 $ relation = Yii::createObject (
@@ -258,17 +242,17 @@ protected function resolveProperty(
258242 [$ min , $ max ] = $ fkProperty ->guessMinMax ();
259243 $ attribute ->asReference ($ relatedClassName );
260244 $ attribute ->setPhpType ($ fkProperty ->guessPhpType ())
261- ->setDbType ($ fkProperty ->guessDbType (true ))
262- ->setSize ($ fkProperty ->getMaxLength ())
263- ->setDescription ($ property ->getRefSchema ()->getDescription ())
264- ->setDefault ($ fkProperty ->guessDefault ())
265- ->setLimits ($ min , $ max , $ fkProperty ->getMinLength ());
245+ ->setDbType ($ fkProperty ->guessDbType (true ))
246+ ->setSize ($ fkProperty ->getMaxLength ())
247+ ->setDescription ($ property ->getRefSchema ()->getDescription ())
248+ ->setDefault ($ fkProperty ->guessDefault ())
249+ ->setLimits ($ min , $ max , $ fkProperty ->getMinLength ());
266250
267251 $ relation = Yii::createObject (
268252 AttributeRelation::class,
269253 [$ property ->getName (), $ relatedTableName , $ relatedClassName ]
270254 )
271- ->asHasOne ([$ fkProperty ->getName () => $ attribute ->columnName ]);
255+ ->asHasOne ([$ fkProperty ->getName () => $ attribute ->columnName ]);
272256 $ relation ->onUpdateFkConstraint = $ property ->onUpdateFkConstraint ;
273257 $ relation ->onDeleteFkConstraint = $ property ->onDeleteFkConstraint ;
274258 if ($ property ->isRefPointerToSelf ()) {
@@ -279,10 +263,10 @@ protected function resolveProperty(
279263 if (!$ property ->isReference () && !$ property ->hasRefItems ()) {
280264 [$ min , $ max ] = $ property ->guessMinMax ();
281265 $ attribute ->setIsVirtual ($ property ->isVirtual ())
282- ->setPhpType ($ property ->guessPhpType ())
283- ->setDbType ($ property ->guessDbType ())
284- ->setSize ($ property ->getMaxLength ())
285- ->setLimits ($ min , $ max , $ property ->getMinLength ());
266+ ->setPhpType ($ property ->guessPhpType ())
267+ ->setDbType ($ property ->guessDbType ())
268+ ->setSize ($ property ->getMaxLength ())
269+ ->setLimits ($ min , $ max , $ property ->getMinLength ());
286270 if ($ property ->hasEnum ()) {
287271 $ attribute ->setEnumValues ($ property ->getAttr ('enum ' ));
288272 }
@@ -319,7 +303,7 @@ protected function resolveProperty(
319303 AttributeRelation::class,
320304 [$ property ->getName (), $ relatedTableName , $ relatedClassName ]
321305 )
322- ->asHasMany ([$ fkProperty ->getName () => $ fkProperty ->getName ()])->asSelfReference ();
306+ ->asHasMany ([$ fkProperty ->getName () => $ fkProperty ->getName ()])->asSelfReference ();
323307 return ;
324308 }
325309 $ foreignPk = Inflector::camel2id ($ fkProperty ->getName (), '_ ' ) . '_id ' ;
@@ -328,7 +312,7 @@ protected function resolveProperty(
328312 AttributeRelation::class,
329313 [$ property ->getName (), $ relatedTableName , $ relatedClassName ]
330314 )
331- ->asHasMany ([$ foreignPk => $ this ->schema ->getPkName ()]);
315+ ->asHasMany ([$ foreignPk => $ this ->schema ->getPkName ()]);
332316 return ;
333317 }
334318 $ relatedClassName = $ property ->getRefClassName ();
@@ -347,7 +331,7 @@ protected function resolveProperty(
347331 AttributeRelation::class,
348332 [$ property ->getName (), $ relatedTableName , $ relatedClassName ]
349333 )
350- ->asHasMany ([Inflector::camel2id ($ this ->schemaName , '_ ' ) . '_id ' => $ this ->schema ->getPkName ()]);
334+ ->asHasMany ([Inflector::camel2id ($ this ->schemaName , '_ ' ) . '_id ' => $ this ->schema ->getPkName ()]);
351335 return ;
352336 }
353337 if ($ this ->schema ->isNonDb () && $ attribute ->isReference ()) {
@@ -367,14 +351,15 @@ protected function resolveProperty(
367351 * @param string $relatedTableName
368352 * @param ComponentSchema $refSchema
369353 * @return bool
370- * @throws \yii\base\ InvalidConfigException
354+ * @throws InvalidConfigException|InvalidDefinitionException
371355 */
372356 protected function catchManyToMany (
373357 string $ propertyName ,
374358 string $ relatedSchemaName ,
375359 string $ relatedTableName ,
376360 ComponentSchema $ refSchema
377- ):bool {
361+ ): bool
362+ {
378363 if (strtolower (Inflector::id2camel ($ propertyName , '_ ' ))
379364 !== strtolower (Inflector::pluralize ($ relatedSchemaName ))) {
380365 return false ;
@@ -406,9 +391,9 @@ protected function catchManyToMany(
406391 }
407392
408393 /**
409- * @throws \yii\base\ InvalidConfigException
394+ * @throws InvalidConfigException
410395 */
411- protected function guessFakerStub (Attribute $ attribute , PropertySchema $ property ):?string
396+ protected function guessFakerStub (Attribute $ attribute , PropertySchema $ property ): ?string
412397 {
413398 $ resolver = Yii::createObject (['class ' => FakerStubResolver::class], [$ attribute , $ property , $ this ->config ]);
414399 return $ resolver ->resolve ();
@@ -417,9 +402,9 @@ protected function guessFakerStub(Attribute $attribute, PropertySchema $property
417402 /**
418403 * @param array $indexes
419404 * @return array|DbIndex[]
420- * @throws \cebe\yii2openapi\lib\exceptions\ InvalidDefinitionException
405+ * @throws InvalidDefinitionException
421406 */
422- protected function prepareIndexes (array $ indexes ):array
407+ protected function prepareIndexes (array $ indexes ): array
423408 {
424409 $ dbIndexes = [];
425410 foreach ($ indexes as $ index ) {
@@ -470,12 +455,12 @@ protected function prepareIndexes(array $indexes):array
470455 }
471456
472457 /**
473- * @param \cebe\yii2openapi\lib\openapi\ PropertySchema $property
474- * @param \cebe\yii2openapi\lib\items\ Attribute $attribute
458+ * @param PropertySchema $property
459+ * @param Attribute $attribute
475460 * @return void
476- * @throws \yii\base\ InvalidConfigException
461+ * @throws InvalidConfigException|InvalidDefinitionException
477462 */
478- protected function resolvePropertyRef (PropertySchema $ property , Attribute $ attribute ):void
463+ protected function resolvePropertyRef (PropertySchema $ property , Attribute $ attribute ): void
479464 {
480465 $ fkProperty = new PropertySchema (
481466 $ property ->getRefSchema ()->getSchema (),
@@ -484,11 +469,11 @@ protected function resolvePropertyRef(PropertySchema $property, Attribute $attri
484469 );
485470 [$ min , $ max ] = $ fkProperty ->guessMinMax ();
486471 $ attribute ->setPhpType ($ fkProperty ->guessPhpType ())
487- ->setDbType ($ fkProperty ->guessDbType (true ))
488- ->setSize ($ fkProperty ->getMaxLength ())
489- ->setDescription ($ fkProperty ->getAttr ('description ' ))
490- ->setDefault ($ fkProperty ->guessDefault ())
491- ->setLimits ($ min , $ max , $ fkProperty ->getMinLength ());
472+ ->setDbType ($ fkProperty ->guessDbType (true ))
473+ ->setSize ($ fkProperty ->getMaxLength ())
474+ ->setDescription ($ fkProperty ->getAttr ('description ' ))
475+ ->setDefault ($ fkProperty ->guessDefault ())
476+ ->setLimits ($ min , $ max , $ fkProperty ->getMinLength ());
492477 $ this ->attributes [$ property ->getName ()] =
493478 $ attribute ->setFakerStub ($ this ->guessFakerStub ($ attribute , $ fkProperty ));
494479 }
0 commit comments