|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Xsolve\Associate\AssociationCollecting; |
|
4
|
|
|
|
|
5
|
|
|
use Xsolve\Associate\CollectionTraversal\CollectionTraversalStrategyInterface; |
|
6
|
|
|
use Xsolve\Associate\ObjectCollection\ObjectCollectionInterface; |
|
7
|
|
|
use Xsolve\Associate\ObjectCollection\UniqueObjectCollection; |
|
8
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccess; |
|
9
|
|
|
use Symfony\Component\PropertyAccess\PropertyAccessor; |
|
10
|
|
|
|
|
11
|
|
|
class BasicAssociationCollectingStrategy implements AssociationCollectingStrategyInterface |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* @var PropertyAccessor |
|
15
|
|
|
*/ |
|
16
|
|
|
protected $propertyAccessor; |
|
17
|
|
|
|
|
18
|
|
|
/** |
|
19
|
|
|
* @var CollectionTraversalStrategyInterface[] |
|
20
|
|
|
*/ |
|
21
|
|
|
protected $collectionTraversalStrategies = []; |
|
22
|
|
|
|
|
23
|
|
|
public function __construct() |
|
24
|
|
|
{ |
|
25
|
|
|
$this->propertyAccessor = PropertyAccess::createPropertyAccessor(); |
|
26
|
|
|
} |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @param CollectionTraversalStrategyInterface $collectionTraversalStrategy |
|
30
|
|
|
*/ |
|
31
|
|
|
public function addCollectionTraversalStrategy(CollectionTraversalStrategyInterface $collectionTraversalStrategy) |
|
32
|
|
|
{ |
|
33
|
|
|
$this->collectionTraversalStrategies[] = $collectionTraversalStrategy; |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
/** |
|
37
|
|
|
* @param array $objects |
|
38
|
|
|
* @param string $associationName |
|
39
|
|
|
* |
|
40
|
|
|
* @return array |
|
41
|
|
|
*/ |
|
42
|
|
|
public function collect(array $objects, string $associationName): array |
|
43
|
|
|
{ |
|
44
|
|
|
$collectedAssociatedObjects = new UniqueObjectCollection(); |
|
45
|
|
|
|
|
46
|
|
|
foreach ($objects as $object) { |
|
47
|
|
|
$associatedObject = $this->propertyAccessor->getValue($object, $associationName); |
|
48
|
|
|
if (!is_null($associatedObject)) { |
|
49
|
|
|
$this->addAssociatedObjectsToCollection($collectedAssociatedObjects, $associatedObject); |
|
50
|
|
|
} |
|
51
|
|
|
} |
|
52
|
|
|
|
|
53
|
|
|
return $collectedAssociatedObjects->getAll(); |
|
54
|
|
|
} |
|
55
|
|
|
|
|
56
|
|
|
/** |
|
57
|
|
|
* @param ObjectCollectionInterface $objectCollection |
|
58
|
|
|
* @param mixed $propertyValue |
|
59
|
|
|
*/ |
|
60
|
|
|
protected function addAssociatedObjectsToCollection(ObjectCollectionInterface $objectCollection, $propertyValue) |
|
61
|
|
|
{ |
|
62
|
|
|
$collectionTraversalStrategy = $this->getSupportingCollectionTraversalStrategy($propertyValue); |
|
63
|
|
|
if ($collectionTraversalStrategy instanceof CollectionTraversalStrategyInterface) { |
|
64
|
|
|
$collectionTraversalStrategy->traverse($objectCollection, $propertyValue); |
|
65
|
|
|
|
|
66
|
|
|
return; |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
$objectCollection->addOne($propertyValue); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param mixed $propertyValue |
|
74
|
|
|
* |
|
75
|
|
|
* @return CollectionTraversalStrategyInterface|null |
|
76
|
|
|
*/ |
|
77
|
|
|
protected function getSupportingCollectionTraversalStrategy($propertyValue) |
|
78
|
|
|
{ |
|
79
|
|
|
foreach ($this->collectionTraversalStrategies as $collectionTraversalStrategy) { |
|
80
|
|
|
if ($collectionTraversalStrategy->supports($propertyValue)) { |
|
81
|
|
|
return $collectionTraversalStrategy; |
|
82
|
|
|
} |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
} |
|
86
|
|
|
|