• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

api-platform / core / 10739011304

06 Sep 2024 01:11PM UTC coverage: 7.159% (-0.5%) from 7.645%
10739011304

push

github

web-flow
 feat(laravel): eloquent filters (search, date, equals, or) (#6593)

* feat(laravel): Eloquent filters search date or

* feat(laravel): eloquent filters order range equals afterdate

* fix(laravel): order afterDate filters

* temp

* test(laravel): filter with eloquent

---------

Co-authored-by: Nathan <nathan@les-tilleuls.coop>

0 of 144 new or added lines in 16 files covered. (0.0%)

4792 existing lines in 155 files now uncovered.

11736 of 163930 relevant lines covered (7.16%)

22.8 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.12
/src/GraphQl/Serializer/SerializerContextBuilder.php
1
<?php
2

3
/*
4
 * This file is part of the API Platform project.
5
 *
6
 * (c) Kévin Dunglas <dunglas@gmail.com>
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\GraphQl\Serializer;
15

16
use ApiPlatform\Metadata\GraphQl\Mutation;
17
use ApiPlatform\Metadata\GraphQl\Operation;
18
use ApiPlatform\Metadata\GraphQl\Subscription;
19
use GraphQL\Type\Definition\ResolveInfo;
20
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
21
use Symfony\Component\Serializer\NameConverter\MetadataAwareNameConverter;
22
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
23

24
/**
25
 * Builds the context used by the Symfony Serializer.
26
 *
27
 * @author Alan Poulain <contact@alanpoulain.eu>
28
 */
29
final class SerializerContextBuilder implements SerializerContextBuilderInterface
30
{
31
    public function __construct(private readonly ?NameConverterInterface $nameConverter)
32
    {
UNCOV
33
    }
1,311✔
34

35
    public function create(?string $resourceClass, Operation $operation, array $resolverContext, bool $normalization): array
36
    {
UNCOV
37
        $context = ['resource_class' => $resourceClass, 'operation_name' => $operation->getName(), 'graphql_operation_name' => $operation->getName()];
338✔
38

UNCOV
39
        if (isset($resolverContext['fields'])) {
338✔
UNCOV
40
            $context['no_resolver_data'] = true;
3✔
41
        }
42

UNCOV
43
        $context['operation'] = $operation;
338✔
UNCOV
44
        if ($operation->getInput()) {
338✔
UNCOV
45
            $context['input'] = $operation->getInput();
15✔
46
        }
UNCOV
47
        if ($operation->getOutput()) {
338✔
UNCOV
48
            $context['output'] = $operation->getOutput();
12✔
49
        }
UNCOV
50
        $context = $normalization ? array_merge($operation->getNormalizationContext() ?? [], $context) : array_merge($operation->getDenormalizationContext() ?? [], $context);
338✔
51

UNCOV
52
        if ($normalization) {
338✔
UNCOV
53
            $context['attributes'] = $this->fieldsToAttributes($resourceClass, $operation, $resolverContext, $context);
338✔
54
        }
55

UNCOV
56
        return $context;
338✔
57
    }
58

59
    /**
60
     * Retrieves fields, recursively replaces the "_id" key (the raw id) by "id" (the name of the property expected by the Serializer) and flattens edge and node structures (pagination).
61
     */
62
    private function fieldsToAttributes(?string $resourceClass, Operation $operation, array $resolverContext, array $context): array
63
    {
UNCOV
64
        if (isset($resolverContext['fields'])) {
338✔
UNCOV
65
            $fields = $resolverContext['fields'];
3✔
66
        } else {
67
            /** @var ResolveInfo $info */
UNCOV
68
            $info = $resolverContext['info'];
335✔
UNCOV
69
            $fields = $info->getFieldSelection(\PHP_INT_MAX);
335✔
70
        }
71

UNCOV
72
        $attributes = $this->replaceIdKeys($fields['edges']['node'] ?? $fields['collection'] ?? $fields, $resourceClass, $context);
338✔
73

UNCOV
74
        if ($operation instanceof Subscription || $operation instanceof Mutation) {
338✔
UNCOV
75
            $wrapFieldName = lcfirst($operation->getShortName());
106✔
76

UNCOV
77
            return $attributes[$wrapFieldName] ?? [];
106✔
78
        }
79

UNCOV
80
        return $attributes;
251✔
81
    }
82

83
    private function replaceIdKeys(array $fields, ?string $resourceClass, array $context): array
84
    {
UNCOV
85
        $denormalizedFields = [];
338✔
86

UNCOV
87
        foreach ($fields as $key => $value) {
338✔
UNCOV
88
            if ('_id' === $key) {
338✔
UNCOV
89
                $denormalizedFields['id'] = $fields['_id'];
13✔
90

UNCOV
91
                continue;
13✔
92
            }
93

UNCOV
94
            $denormalizedFields[$this->denormalizePropertyName((string) $key, $resourceClass, $context)] = \is_array($value) ? $this->replaceIdKeys($value, $resourceClass, $context) : $value;
335✔
95
        }
96

UNCOV
97
        return $denormalizedFields;
338✔
98
    }
99

100
    private function denormalizePropertyName(string $property, ?string $resourceClass, array $context): string
101
    {
UNCOV
102
        if (null === $this->nameConverter) {
335✔
103
            return $property;
×
104
        }
UNCOV
105
        if ($this->nameConverter instanceof AdvancedNameConverterInterface || $this->nameConverter instanceof MetadataAwareNameConverter) {
335✔
UNCOV
106
            return $this->nameConverter->denormalize($property, $resourceClass, null, $context);
335✔
107
        }
108

109
        return $this->nameConverter->denormalize($property);
×
110
    }
111
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc