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

api-platform / core / 19337755357

13 Nov 2025 04:03PM UTC coverage: 0.0%. Remained the same
19337755357

push

github

soyuka
Merge 4.2

0 of 298 new or added lines in 18 files covered. (0.0%)

61 existing lines in 11 files now uncovered.

0 of 56962 relevant lines covered (0.0%)

0.0 hits per line

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

0.0
/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
        // }
UNCOV
46
    }
×
47

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

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

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

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

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

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

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

84
        return $data;
×
85
    }
86

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

NEW
98
        return true;
×
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