|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the API Platform project. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Kévin Dunglas <[email protected]> |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
declare(strict_types=1); |
|
13
|
|
|
|
|
14
|
|
|
namespace ApiPlatform\Core\Bridge\Doctrine\MongoDbOdm; |
|
15
|
|
|
|
|
16
|
|
|
use ApiPlatform\Core\Exception\InvalidArgumentException; |
|
17
|
|
|
use Doctrine\ODM\MongoDB\Aggregation\Builder; |
|
18
|
|
|
|
|
19
|
|
|
/** |
|
20
|
|
|
* Helper trait regarding a property in a MongoDB document using the resource metadata. |
|
21
|
|
|
* |
|
22
|
|
|
* @author Alan Poulain <[email protected]> |
|
23
|
|
|
*/ |
|
24
|
|
|
trait PropertyHelperTrait |
|
25
|
|
|
{ |
|
26
|
|
|
/** |
|
27
|
|
|
* Adds the necessary lookups for a nested property. |
|
28
|
|
|
* |
|
29
|
|
|
* @throws InvalidArgumentException If property is not nested |
|
30
|
|
|
* |
|
31
|
|
|
* @return array An array where the first element is the $alias of the lookup, |
|
32
|
|
|
* the second element is the $field name |
|
33
|
|
|
* the third element is the $associations array |
|
34
|
|
|
*/ |
|
35
|
|
|
protected function addLookupsForNestedProperty(string $property, Builder $aggregationBuilder, string $resourceClass): array |
|
36
|
|
|
{ |
|
37
|
|
|
$propertyParts = $this->splitPropertyParts($property, $resourceClass); |
|
|
|
|
|
|
38
|
|
|
$association = $propertyParts['associations'][0] ?? null; |
|
39
|
|
|
|
|
40
|
|
|
if (null === $association) { |
|
41
|
|
|
throw new InvalidArgumentException(sprintf('Cannot add lookups for property "%s" - property is not nested.', $property)); |
|
42
|
|
|
} |
|
43
|
|
|
|
|
44
|
|
|
$alias = $association; |
|
45
|
|
|
if ($this->getClassMetadata($resourceClass)->hasReference($association)) { |
|
|
|
|
|
|
46
|
|
|
$alias = "${association}_lkup"; |
|
47
|
|
|
$aggregationBuilder->lookup($association)->alias($alias); |
|
48
|
|
|
} |
|
49
|
|
|
|
|
50
|
|
|
// assocation.property => association_lkup.property |
|
51
|
|
|
return [substr_replace($property, $alias, 0, \strlen($association)), $propertyParts['field'], $propertyParts['associations']]; |
|
52
|
|
|
} |
|
53
|
|
|
} |
|
54
|
|
|
|