Passed
Push — master ( b3bd65...19c08e )
by Alan
03:15
created

ExistsFilter::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 4
dl 0
loc 5
rs 10
c 0
b 0
f 0
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\Filter;
15
16
use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\ExistsFilterInterface;
17
use ApiPlatform\Core\Bridge\Doctrine\Common\Filter\ExistsFilterTrait;
18
use Doctrine\Common\Persistence\ManagerRegistry;
19
use Doctrine\ODM\MongoDB\Aggregation\Builder;
20
use Doctrine\ODM\MongoDB\Mapping\ClassMetadata;
21
use Psr\Log\LoggerInterface;
22
23
/**
24
 * Filters the collection by whether a property value exists or not.
25
 *
26
 * For each property passed, if the resource does not have such property or if
27
 * the value is not one of ( "true" | "false" | "1" | "0" ) the property is ignored.
28
 *
29
 * A query parameter with key but no value is treated as `true`, e.g.:
30
 * Request: GET /products?exists[brand]
31
 * Interpretation: filter products which have a brand
32
 *
33
 * @experimental
34
 *
35
 * @author Teoh Han Hui <[email protected]>
36
 * @author Alan Poulain <[email protected]>
37
 */
38
final class ExistsFilter extends AbstractFilter implements ExistsFilterInterface
39
{
40
    use ExistsFilterTrait;
41
42
    public function __construct(ManagerRegistry $managerRegistry, LoggerInterface $logger = null, string $existsParameterName = self::QUERY_PARAMETER_KEY, array $properties = null)
43
    {
44
        parent::__construct($managerRegistry, $logger, $properties);
45
46
        $this->existsParameterName = $existsParameterName;
47
    }
48
49
    /**
50
     * {@inheritdoc}
51
     */
52
    public function apply(Builder $aggregationBuilder, string $resourceClass, string $operationName = null, array &$context = [])
53
    {
54
        if (!\is_array($context['filters'][$this->existsParameterName] ?? null)) {
55
            $context['exists_deprecated_syntax'] = true;
56
            parent::apply($aggregationBuilder, $resourceClass, $operationName, $context);
57
58
            return;
59
        }
60
61
        foreach ($context['filters'][$this->existsParameterName] as $property => $value) {
62
            $this->filterProperty($property, $value, $aggregationBuilder, $resourceClass, $operationName, $context);
63
        }
64
    }
65
66
    /**
67
     * {@inheritdoc}
68
     */
69
    protected function filterProperty(string $property, $value, Builder $aggregationBuilder, string $resourceClass, string $operationName = null, array &$context = [])
70
    {
71
        if (
72
            (($context['exists_deprecated_syntax'] ?? false) && !isset($value[self::QUERY_PARAMETER_KEY])) ||
73
            !$this->isPropertyEnabled($property, $resourceClass) ||
74
            !$this->isPropertyMapped($property, $resourceClass, true) ||
75
            !$this->isNullableField($property, $resourceClass)
76
        ) {
77
            return;
78
        }
79
80
        $value = $this->normalizeValue($value, $property);
81
        if (null === $value) {
82
            return;
83
        }
84
85
        $matchField = $property;
86
87
        if ($this->isPropertyNested($property, $resourceClass)) {
88
            [$matchField] = $this->addLookupsForNestedProperty($property, $aggregationBuilder, $resourceClass);
89
        }
90
91
        $aggregationBuilder->match()->field($matchField)->{$value ? 'notEqual' : 'equals'}(null);
92
    }
93
94
    /**
95
     * {@inheritdoc}
96
     */
97
    protected function isNullableField(string $property, string $resourceClass): bool
98
    {
99
        $propertyParts = $this->splitPropertyParts($property, $resourceClass);
100
        $metadata = $this->getNestedMetadata($resourceClass, $propertyParts['associations']);
101
102
        $field = $propertyParts['field'];
103
104
        return $metadata instanceof ClassMetadata && $metadata->hasField($field) ? $metadata->isNullable($field) : false;
105
    }
106
}
107