30
30
use TheCodingMachine \TDBM \Utils \Annotation \AddInterface ;
31
31
use Zend \Code \Generator \AbstractMemberGenerator ;
32
32
use Zend \Code \Generator \ClassGenerator ;
33
+ use Zend \Code \Generator \DocBlock \Tag ;
34
+ use Zend \Code \Generator \DocBlock \Tag \GenericTag ;
33
35
use Zend \Code \Generator \DocBlock \Tag \ParamTag ;
34
36
use Zend \Code \Generator \DocBlock \Tag \ReturnTag ;
35
37
use Zend \Code \Generator \DocBlock \Tag \ThrowsTag ;
39
41
use Zend \Code \Generator \MethodGenerator ;
40
42
use Zend \Code \Generator \ParameterGenerator ;
41
43
use Zend \Code \Generator \PropertyGenerator ;
44
+ use function implode ;
45
+ use function var_export ;
42
46
43
47
/**
44
48
* This class represents a bean.
@@ -575,7 +579,7 @@ public function generatePhpCode(): ?FileGenerator
575
579
}
576
580
}
577
581
578
- $ relationshipsPathModel = [];
582
+ $ pivotTableMethodsDescriptors = [];
579
583
foreach ($ this ->getMethodDescriptors () as $ methodDescriptor ) {
580
584
if ($ methodDescriptor instanceof DirectForeignKeyMethodDescriptor) {
581
585
[$ method ] = $ methodDescriptor ->getCode ();
@@ -584,8 +588,7 @@ public function generatePhpCode(): ?FileGenerator
584
588
$ class ->addMethodFromGenerator ($ method );
585
589
}
586
590
} elseif ($ methodDescriptor instanceof PivotTableMethodsDescriptor) {
587
- [$ index , $ value ] = $ methodDescriptor ->getRelationshipPathDescriptor ();
588
- $ relationshipsPathModel [$ index ] = $ value ;
591
+ $ pivotTableMethodsDescriptors [] = $ methodDescriptor ;
589
592
[ $ getter , $ adder , $ remover , $ has , $ setter ] = $ methodDescriptor ->getCode ();
590
593
$ methods = $ this ->codeGeneratorListener ->onBaseBeanManyToManyGenerated ($ getter , $ adder , $ remover , $ has , $ setter , $ methodDescriptor , $ this , $ this ->configuration , $ class );
591
594
foreach ($ methods as $ method ) {
@@ -598,8 +601,14 @@ public function generatePhpCode(): ?FileGenerator
598
601
}
599
602
}
600
603
601
- $ pathCode = $ this ->generatePathModelCode ($ relationshipsPathModel );
602
- $ class ->addMethodFromGenerator ($ pathCode );
604
+ $ manyToManyRelationshipCode = $ this ->generateGetManyToManyRelationshipDescriptorCode ($ pivotTableMethodsDescriptors );
605
+ if ($ manyToManyRelationshipCode !== null ) {
606
+ $ class ->addMethodFromGenerator ($ manyToManyRelationshipCode );
607
+ }
608
+ $ manyToManyRelationshipKeysCode = $ this ->generateGetManyToManyRelationshipDescriptorKeysCode ($ pivotTableMethodsDescriptors );
609
+ if ($ manyToManyRelationshipKeysCode !== null ) {
610
+ $ class ->addMethodFromGenerator ($ manyToManyRelationshipKeysCode );
611
+ }
603
612
604
613
$ foreignKeysProperty = new PropertyGenerator ('foreignKeys ' );
605
614
$ foreignKeysProperty ->setStatic (true );
@@ -1327,17 +1336,64 @@ private function generateOnDeleteCode(): ?MethodGenerator
1327
1336
}
1328
1337
1329
1338
/**
1330
- * @param mixed [] $data
1339
+ * @param PivotTableMethodsDescriptor [] $pivotTableMethodsDescriptors
1331
1340
* @return MethodGenerator
1332
1341
*/
1333
- private function generatePathModelCode (array $ data ): MethodGenerator
1342
+ private function generateGetManyToManyRelationshipDescriptorCode (array $ pivotTableMethodsDescriptors ): ? MethodGenerator
1334
1343
{
1335
- $ method = new MethodGenerator ('_getRelationshipPathArray ' );
1336
- $ method ->setVisibility (AbstractMemberGenerator::VISIBILITY_PROTECTED );
1337
- $ method ->setReturnType ('array ' );
1344
+ if (empty ($ pivotTableMethodsDescriptors )) {
1345
+ return null ;
1346
+ }
1347
+
1348
+ $ method = new MethodGenerator ('_getManyToManyRelationshipDescriptor ' );
1349
+ $ method ->setVisibility (AbstractMemberGenerator::VISIBILITY_PUBLIC );
1338
1350
$ method ->setDocBlock ('Get the paths used for many to many relationships methods. ' );
1339
- $ method ->getDocBlock ()->setTag (new ReturnTag (['mixed[] ' ]));
1340
- $ method ->setBody ('return ' . $ this ->psr2VarExport ($ data ).'; ' );
1351
+ $ method ->getDocBlock ()->setTag (new GenericTag ('internal ' ));
1352
+ $ method ->setReturnType (ManyToManyRelationshipPathDescriptor::class);
1353
+
1354
+ $ parameter = new ParameterGenerator ('pathKey ' );
1355
+ $ parameter ->setType ('string ' );
1356
+ $ method ->setParameter ($ parameter );
1357
+
1358
+ $ code = 'switch ($pathKey) { ' ."\n" ;
1359
+ foreach ($ pivotTableMethodsDescriptors as $ pivotTableMethodsDescriptor ) {
1360
+ $ code .= ' case ' .var_export ($ pivotTableMethodsDescriptor ->getManyToManyRelationshipKey (), true ).": \n" ;
1361
+ $ code .= ' return ' .$ pivotTableMethodsDescriptor ->getManyToManyRelationshipInstantiationCode ()."; \n" ;
1362
+ }
1363
+ $ code .= " default: \n" ;
1364
+ $ code .= " return parent::_getManyToManyRelationshipDescriptor( \$pathKey); \n" ;
1365
+ $ code .= "} \n" ;
1366
+
1367
+ $ method ->setBody ($ code );
1368
+
1369
+ return $ method ;
1370
+ }
1371
+
1372
+ /**
1373
+ * @param PivotTableMethodsDescriptor[] $pivotTableMethodsDescriptors
1374
+ * @return MethodGenerator
1375
+ */
1376
+ private function generateGetManyToManyRelationshipDescriptorKeysCode (array $ pivotTableMethodsDescriptors ): ?MethodGenerator
1377
+ {
1378
+ if (empty ($ pivotTableMethodsDescriptors )) {
1379
+ return null ;
1380
+ }
1381
+
1382
+ $ method = new MethodGenerator ('_getManyToManyRelationshipDescriptorKeys ' );
1383
+ $ method ->setVisibility (AbstractMemberGenerator::VISIBILITY_PUBLIC );
1384
+ $ method ->setReturnType ('array ' );
1385
+ $ method ->setDocBlock ('Returns the list of keys supported for many to many relationships ' );
1386
+ $ method ->getDocBlock ()->setTag (new GenericTag ('internal ' ));
1387
+ $ method ->getDocBlock ()->setTag (new ReturnTag ('string[] ' ));
1388
+
1389
+ $ keys = [];
1390
+ foreach ($ pivotTableMethodsDescriptors as $ pivotTableMethodsDescriptor ) {
1391
+ $ keys [] = var_export ($ pivotTableMethodsDescriptor ->getManyToManyRelationshipKey (), true );
1392
+ }
1393
+
1394
+ $ code = 'return array_merge(parent::_getManyToManyRelationshipDescriptorKeys(), [ ' .implode (', ' , $ keys ).']); ' ;
1395
+
1396
+ $ method ->setBody ($ code );
1341
1397
1342
1398
return $ method ;
1343
1399
}
0 commit comments