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