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