Code Duplication    Length = 41-41 lines in 2 locations

src/Serializer/Filter/GroupFilter.php 1 location

@@ 23-63 (lines=41) @@
20
 *
21
 * @author Baptiste Meyer <[email protected]>
22
 */
23
final class GroupFilter implements FilterInterface
24
{
25
    private $overrideDefaultGroups;
26
    private $parameterName;
27
28
    public function __construct(string $parameterName = 'groups', bool $overrideDefaultGroups = false)
29
    {
30
        $this->overrideDefaultGroups = $overrideDefaultGroups;
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($groups = $request->query->get($this->parameterName))) {
40
            return;
41
        }
42
43
        if (!$this->overrideDefaultGroups && isset($context['groups'])) {
44
            $groups = array_merge((array) $context['groups'], $groups);
45
        }
46
47
        $context['groups'] = $groups;
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

src/Serializer/Filter/PropertyFilter.php 1 location

@@ 23-63 (lines=41) @@
20
 *
21
 * @author Baptiste Meyer <[email protected]>
22
 */
23
final class PropertyFilter implements FilterInterface
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