1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Malef\Associate\DoctrineOrm\Loader; |
4
|
|
|
|
5
|
|
|
use Doctrine\Common\Collections\ArrayCollection; |
6
|
|
|
use Doctrine\Common\Collections\Collection; |
7
|
|
|
use Malef\Associate\AssociateException; |
8
|
|
|
use Malef\Associate\DoctrineOrm\Loader\LoadingStrategy\AssociationLoadingStrategyInterface; |
9
|
|
|
use Malef\Associate\DoctrineOrm\Loader\LoadingStrategy\OneToOneInverseSideAssociationLoadingStrategy; |
10
|
|
|
use Malef\Associate\DoctrineOrm\Loader\LoadingStrategy\ToManyAssociationLoadingStrategy; |
11
|
|
|
use Malef\Associate\DoctrineOrm\Loader\LoadingStrategy\ToOneAssociationLoadingStrategy; |
12
|
|
|
use Malef\Associate\DoctrineOrm\Metadata\AssociationMetadataAdapter; |
13
|
|
|
|
14
|
|
|
class AssociationLoader |
15
|
|
|
{ |
16
|
|
|
/** |
17
|
|
|
* @var Collection|AssociationLoadingStrategyInterface[] |
18
|
|
|
*/ |
19
|
|
|
protected $associationLoadingStrategies; |
20
|
|
|
|
21
|
|
|
public function __construct( |
22
|
|
|
OneToOneInverseSideAssociationLoadingStrategy $oneToOneInverseSideAssociationLoadingStrategy, |
23
|
|
|
ToOneAssociationLoadingStrategy $toOneAssociationLoadingStrategy, |
24
|
|
|
ToManyAssociationLoadingStrategy $toManyAssociationLoadingStrategy |
25
|
|
|
) { |
26
|
|
|
$this->associationLoadingStrategies = new ArrayCollection([ |
27
|
|
|
$oneToOneInverseSideAssociationLoadingStrategy, |
28
|
|
|
$toOneAssociationLoadingStrategy, |
29
|
|
|
$toManyAssociationLoadingStrategy, |
30
|
|
|
]); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @throws AssociateException if no association loading strategy |
35
|
|
|
* or multiple association loading strategies are found |
36
|
|
|
*/ |
37
|
|
|
public function load( |
38
|
|
|
ArrayCollection $sourceEntities, |
39
|
|
|
AssociationMetadataAdapter $associationMetadataAdapter |
40
|
|
|
): void { |
41
|
|
|
$this |
42
|
|
|
->getSupportingAssociationLoadingStrategy($associationMetadataAdapter) |
43
|
|
|
->load($sourceEntities, $associationMetadataAdapter); |
44
|
|
|
} |
45
|
|
|
|
46
|
|
|
protected function getSupportingAssociationLoadingStrategy( |
47
|
|
|
AssociationMetadataAdapter $associationMetadataAdapter |
48
|
|
|
): AssociationLoadingStrategyInterface { |
49
|
|
|
$supportingAssociationLoadingStrategies = $this->associationLoadingStrategies->filter( |
50
|
|
|
function (AssociationLoadingStrategyInterface $associationLoadingStrategy) use ($associationMetadataAdapter) { |
51
|
|
|
return $associationLoadingStrategy->supports($associationMetadataAdapter); |
52
|
|
|
} |
53
|
|
|
); |
54
|
|
|
|
55
|
|
|
if ($supportingAssociationLoadingStrategies->isEmpty()) { |
56
|
|
|
throw new AssociateException('No association loading strategy found.'); |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
if (1 < $supportingAssociationLoadingStrategies->count()) { |
60
|
|
|
throw new AssociateException('Multiple association loading strategies found.'); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
return $supportingAssociationLoadingStrategies->first(); |
64
|
|
|
} |
65
|
|
|
} |
66
|
|
|
|