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

api-platform / core / 16050929464

03 Jul 2025 12:51PM UTC coverage: 22.065% (+0.2%) from 21.821%
16050929464

push

github

soyuka
chore: todo improvement

11516 of 52192 relevant lines covered (22.06%)

22.08 hits per line

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

94.74
/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\ObjectMapperInterface;
25

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

33
    /**
34
     * @param ProviderInterface<object> $decorated
35
     */
36
    public function __construct(
37
        private readonly ?ObjectMapperInterface $objectMapper,
38
        private readonly ProviderInterface $decorated,
39
    ) {
40
    }
497✔
41

42
    public function provide(Operation $operation, array $uriVariables = [], array $context = []): object|array|null
43
    {
44
        $data = $this->decorated->provide($operation, $uriVariables, $context);
469✔
45

46
        if (!$this->objectMapper || !\is_object($data)) {
467✔
47
            return $data;
67✔
48
        }
49

50
        $request = $context['request'] ?? null;
410✔
51
        $entityClass = null;
410✔
52
        if (($options = $operation->getStateOptions()) && $options instanceof Options && $options->getEntityClass()) {
410✔
53
            $entityClass = $options->getEntityClass();
8✔
54
        }
55

56
        if (($options = $operation->getStateOptions()) && $options instanceof OdmOptions && $options->getDocumentClass()) {
410✔
57
            $entityClass = $options->getDocumentClass();
×
58
        }
59

60
        $entityClass ??= $data::class;
410✔
61

62
        if (!(new \ReflectionClass($entityClass))->getAttributes(Map::class)) {
410✔
63
            return $data;
408✔
64
        }
65

66
        if ($data instanceof PaginatorInterface) {
2✔
67
            $data = new ArrayPaginator(array_map(fn ($v) => $this->objectMapper->map($v), iterator_to_array($data)), 0, \count($data));
2✔
68
        } else {
69
            $data = $this->objectMapper->map($data);
2✔
70
        }
71

72
        $request?->attributes->set('data', $data);
2✔
73
        $request?->attributes->set('previous_data', $this->clone($data));
2✔
74

75
        return $data;
2✔
76
    }
77
}
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