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

api-platform / core / 19250029811

10 Nov 2025 11:56PM UTC coverage: 24.566% (+24.6%) from 0.0%
19250029811

push

github

soyuka
ci: ignore no-unnecessary-combinator

14095 of 57375 relevant lines covered (24.57%)

26.48 hits per line

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

81.48
/src/State/Provider/ObjectMapperProvider.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\State\Provider;
15

16
use ApiPlatform\Doctrine\Odm\State\Options as OdmOptions;
17
use ApiPlatform\Doctrine\Orm\State\Options;
18
use ApiPlatform\Metadata\Operation;
19
use ApiPlatform\Metadata\Util\CloneTrait;
20
use ApiPlatform\State\Pagination\ArrayPaginator;
21
use ApiPlatform\State\Pagination\PaginatorInterface;
22
use ApiPlatform\State\ProviderInterface;
23
use Symfony\Component\ObjectMapper\Attribute\Map;
24
use Symfony\Component\ObjectMapper\Metadata\ObjectMapperMetadataFactoryInterface;
25
use Symfony\Component\ObjectMapper\ObjectMapperInterface;
26

27
/**
28
 * @implements ProviderInterface<object>
29
 */
30
final class ObjectMapperProvider implements ProviderInterface
31
{
32
    use CloneTrait;
33

34
    /**
35
     * @param ProviderInterface<object> $decorated
36
     */
37
    public function __construct(
38
        private readonly ?ObjectMapperInterface $objectMapper,
39
        private readonly ProviderInterface $decorated,
40
        private readonly ?ObjectMapperMetadataFactoryInterface $objectMapperMetadata = null,
41
    ) {
42
        // TODO: 4.3 add this deprecation
43
        // if (!$objectMapperMetadata) {
44
        //     trigger_deprecation('api-platform/state', '4.3', 'Not injecting "%s" in "%s" will not be possible anymore in 5.0.', ObjectMapperMetadataFactoryInterface::class, __CLASS__);
45
        // }
46
    }
706✔
47

48
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
49
    {
50
        $data = $this->decorated->provide($operation, $uriVariables, $context);
676✔
51

52
        if (!$this->objectMapper || !\is_object($data)) {
674✔
53
            return $data;
72✔
54
        }
55

56
        $request = $context['request'] ?? null;
622✔
57
        $entityClass = null;
622✔
58
        if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
622✔
59
            $entityClass = $options->getEntityClass();
16✔
60
        }
61

62
        if (($options = $operation->getStateOptions()) && $options instanceof OdmOptions && $options->getDocumentClass()) {
622✔
63
            $entityClass = $options->getDocumentClass();
×
64
        }
65

66
        // Look for Mapping metadata
67
        if ($this->objectMapperMetadata) {
622✔
68
            if (!$this->canBeMapped($operation->getClass()) && (!$entityClass || !$this->canBeMapped($entityClass))) {
622✔
69
                return $data;
614✔
70
            }
71
        } elseif (!(new \ReflectionClass($operation->getClass()))->getAttributes(Map::class) && !(new \ReflectionClass($entityClass))->getAttributes(Map::class)) {
×
72
            return $data;
×
73
        }
74

75
        if ($data instanceof PaginatorInterface) {
8✔
76
            $data = new ArrayPaginator(array_map(fn ($v) => $this->objectMapper->map($v, $operation->getClass()), iterator_to_array($data)), 0, \count($data));
6✔
77
        } else {
78
            $data = $this->objectMapper->map($data, $operation->getClass());
6✔
79
        }
80

81
        $request?->attributes->set('data', $data);
8✔
82
        $request?->attributes->set('previous_data', $this->clone($data));
8✔
83

84
        return $data;
8✔
85
    }
86

87
    private function canBeMapped(string $class): bool
88
    {
89
        try {
90
            $r = new \ReflectionClass($class);
622✔
91
            if (!$r->isInstantiable() || !$this->objectMapperMetadata->create($r->newInstanceWithoutConstructor(), null, ['_api_check_can_be_mapped' => true])) {
622✔
92
                return false;
622✔
93
            }
94
        } catch (\ReflectionException $e) {
×
95
            return false;
×
96
        }
97

98
        return true;
8✔
99
    }
100
}
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