1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
namespace AlgoWeb\PODataLaravel\Models\ObjectMap\Entities\Associations; |
5
|
|
|
|
6
|
|
|
|
7
|
|
|
use Illuminate\Support\Str; |
8
|
|
|
|
9
|
|
|
abstract class AssociationFactory |
10
|
|
|
{ |
11
|
|
|
public static $marshalPolymorphics = true; |
12
|
|
|
public static function getAssocationFromStubs(AssociationStubBase $stubOne, AssociationStubBase $stubTwo): Association |
13
|
|
|
{ |
14
|
|
|
return self::checkAssocations($stubOne, $stubTwo) ?? self::buildAssocationFromStubs($stubOne, $stubTwo); |
15
|
|
|
} |
16
|
|
|
|
17
|
|
|
private static function buildAssocationFromStubs(AssociationStubBase $stubOne, AssociationStubBase $stubTwo): Association |
18
|
|
|
{ |
19
|
|
|
|
20
|
|
|
$oneFirst = $stubOne->getKeyField()->getIsKeyField(); |
21
|
|
|
$twoFirst = $stubTwo->getKeyField()->getIsKeyField(); |
22
|
|
|
$first = $oneFirst === $twoFirst ? -1 === $stubOne->compare($stubTwo) : $oneFirst; |
23
|
|
|
|
24
|
|
|
$association = new AssociationMonomorphic(); |
25
|
|
|
if($stubTwo->getTargType() == null){ |
26
|
|
|
dd($stubTwo); |
27
|
|
|
} |
28
|
|
|
if($stubOne->getTargType() == null && self::$marshalPolymorphics){ |
29
|
|
|
$stubOne = self::marshalPolyToMono($stubOne, $stubTwo); |
30
|
|
|
} |
31
|
|
|
$input[intval(!$first)] = $stubOne; |
|
|
|
|
32
|
|
|
$input[intval($first)] = $stubTwo; |
33
|
|
|
$association->setFirst($input[0]); |
34
|
|
|
$association->setLast($input[1]); |
35
|
|
|
return $association; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
private static function marshalPolyToMono(AssociationStubBase $stub, AssociationStubBase $stubTwo): AssociationStubBase{ |
39
|
|
|
$oldName = $stub->getRelationName(); |
40
|
|
|
//$stubOne->addAssociation($association); |
41
|
|
|
$stubNew = clone $stub; |
42
|
|
|
$relPolyTypeName = substr($stubTwo->getBaseType(), strrpos($stubTwo->getBaseType(), '\\')+1); |
43
|
|
|
$relPolyTypeName = Str::plural($relPolyTypeName, 1); |
44
|
|
|
$stubNew->setRelationName($stub->getRelationName() . '_' . $relPolyTypeName); |
45
|
|
|
$stubNew->setTargType($stubTwo->getBaseType()); |
46
|
|
|
$stubNew->setForeignFieldName($stubTwo->getKeyFieldName()); |
47
|
|
|
$entity = $stub->getEntity(); |
48
|
|
|
$stubs = $entity->getStubs(); |
49
|
|
|
if(array_key_exists($oldName, $stubs)){ |
50
|
|
|
//unset($stubs[$oldName]); |
51
|
|
|
} |
52
|
|
|
$stubs[$stubNew->getRelationName()] = $stubNew; |
53
|
|
|
$entity->setStubs($stubs); |
54
|
|
|
return $stubNew; |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
private static function checkAssocations(AssociationStubBase $stubOne, AssociationStubBase $stubTwo): ?Association{ |
58
|
|
|
$assocOne = $stubOne->getAssocations(); |
59
|
|
|
foreach($assocOne as $association){ |
60
|
|
|
$isFirst = $association->getFirst() === $stubOne; |
61
|
|
|
if($association->{$isFirst ? 'getLast' : 'getFirst'}() == $stubTwo){ |
62
|
|
|
return $association; |
63
|
|
|
} |
64
|
|
|
} |
65
|
|
|
return null; |
66
|
|
|
} |
67
|
|
|
} |