Completed
Push — master ( 183b0a...e3c17c )
by Kévin
03:20
created

PropertyFilter::getDescription()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 10
Code Lines 6

Duplication

Lines 10
Ratio 100 %

Importance

Changes 0
Metric Value
dl 10
loc 10
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 6
nc 1
nop 1
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\Serializer\Filter;
15
16
use Symfony\Component\HttpFoundation\Request;
17
18
/**
19
 * Property filter.
20
 *
21
 * @author Baptiste Meyer <[email protected]>
22
 */
23 View Code Duplication
final class PropertyFilter implements FilterInterface
0 ignored issues
show
Duplication introduced by
This class seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
24
{
25
    private $overrideDefaultProperties;
26
    private $parameterName;
27
28
    public function __construct(string $parameterName = 'properties', bool $overrideDefaultProperties = false)
29
    {
30
        $this->overrideDefaultProperties = $overrideDefaultProperties;
31
        $this->parameterName = $parameterName;
32
    }
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function apply(Request $request, bool $normalization, array $attributes, array &$context)
38
    {
39
        if (!is_array($properties = $request->query->get($this->parameterName))) {
40
            return;
41
        }
42
43
        if (!$this->overrideDefaultProperties && isset($context['attributes'])) {
44
            $properties = array_merge_recursive((array) $context['attributes'], $properties);
45
        }
46
47
        $context['attributes'] = $properties;
48
    }
49
50
    /**
51
     * {@inheritdoc}
52
     */
53
    public function getDescription(string $resourceClass): array
54
    {
55
        return [
56
            $this->parameterName.'[]' => [
57
                'property' => null,
58
                'type' => 'string',
59
                'required' => false,
60
            ],
61
        ];
62
    }
63
}
64