|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xsolve\Associate\BufferedCollector; |
|
4
|
|
|
|
|
5
|
|
|
use Xsolve\Associate\CollectorInterface; |
|
6
|
|
|
use Xsolve\Associate\Metadata\MetadataWrapperProvider; |
|
7
|
|
|
use Xsolve\Associate\ObjectCollection\UniqueObjectCollection; |
|
8
|
|
|
|
|
9
|
|
|
class BufferedCollector implements BufferedCollectorInterface |
|
10
|
|
|
{ |
|
11
|
|
|
/** |
|
12
|
|
|
* @var MetadataWrapperProvider |
|
13
|
|
|
*/ |
|
14
|
|
|
protected $metadataWrapperProvider; |
|
15
|
|
|
|
|
16
|
|
|
/** |
|
17
|
|
|
* @var CollectorInterface |
|
18
|
|
|
*/ |
|
19
|
|
|
protected $collector; |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @var BufferedCollect[] |
|
23
|
|
|
*/ |
|
24
|
|
|
protected $bufferedCollects = []; |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* @param MetadataWrapperProvider $metadataWrapperProvider |
|
28
|
|
|
* @param CollectorInterface $collector |
|
29
|
|
|
*/ |
|
30
|
|
|
public function __construct( |
|
31
|
|
|
MetadataWrapperProvider $metadataWrapperProvider, |
|
32
|
|
|
CollectorInterface $collector |
|
33
|
|
|
) { |
|
34
|
|
|
$this->metadataWrapperProvider = $metadataWrapperProvider; |
|
35
|
|
|
$this->collector = $collector; |
|
36
|
|
|
} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param array $objects |
|
40
|
|
|
* @param string[] $associationPath |
|
41
|
|
|
* |
|
42
|
|
|
* @return \Closure |
|
43
|
|
|
*/ |
|
44
|
|
|
public function createCollectClosure(array $objects, array $associationPath): \Closure |
|
45
|
|
|
{ |
|
46
|
|
|
$bufferedCollect = new BufferedCollect($objects, $associationPath); |
|
47
|
|
|
$this->bufferedCollects[] = $bufferedCollect; |
|
48
|
|
|
|
|
49
|
|
|
return function () use ($bufferedCollect) { |
|
50
|
|
|
if (!empty($this->bufferedCollects)) { |
|
51
|
|
|
$this->resolveSimilar($bufferedCollect); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $bufferedCollect->getAssociatedEntities(); |
|
55
|
|
|
}; |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @param BufferedCollect $resolvedBufferedCollect |
|
60
|
|
|
*/ |
|
61
|
|
|
protected function resolveSimilar(BufferedCollect $resolvedBufferedCollect) |
|
62
|
|
|
{ |
|
63
|
|
|
$resolvedAssociationPath = $resolvedBufferedCollect->getAssociationPath(); |
|
64
|
|
|
|
|
65
|
|
|
$objects = new UniqueObjectCollection(); |
|
66
|
|
|
$similarBufferedCollects = []; |
|
67
|
|
|
foreach ($this->bufferedCollects as $bufferedCollectKey => $bufferedCollect) { |
|
68
|
|
|
if ($bufferedCollect->getAssociationPath()->isEqual($resolvedAssociationPath)) { |
|
69
|
|
|
$similarBufferedCollects[] = $bufferedCollect; |
|
70
|
|
|
$objects->addMany($bufferedCollect->getObjects()); |
|
71
|
|
|
unset($this->bufferedCollects[$bufferedCollectKey]); |
|
72
|
|
|
} |
|
73
|
|
|
} |
|
74
|
|
|
|
|
75
|
|
|
// TODO We would only need to fetch as far as last Doctrine association is in case of mixed associations. |
|
76
|
|
|
|
|
77
|
|
|
$this->collector->collect($objects->getAll(), $resolvedAssociationPath->getAssociationNames()); |
|
78
|
|
|
foreach ($similarBufferedCollects as $bufferedCollect) { |
|
79
|
|
|
$bufferedCollect->setAssociatedEntities( |
|
80
|
|
|
$this->collector->collect($bufferedCollect->getObjects(), $resolvedAssociationPath->getAssociationNames()) |
|
81
|
|
|
); |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|