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

api-platform / core / 6978770879

24 Nov 2023 09:02AM UTC coverage: 37.284% (-0.1%) from 37.409%
6978770879

push

github

soyuka
Merge 3.2

79 of 149 new or added lines in 21 files covered. (53.02%)

16 existing lines in 8 files now uncovered.

10287 of 27591 relevant lines covered (37.28%)

20.53 hits per line

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

8.7
/src/Symfony/Validator/Serializer/ValidationExceptionNormalizer.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\Symfony\Validator\Serializer;
15

16
use ApiPlatform\Serializer\CacheableSupportsMethodInterface;
17
use ApiPlatform\Symfony\Validator\Exception\ValidationException;
18
use Symfony\Component\Serializer\NameConverter\AdvancedNameConverterInterface;
19
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
20
use Symfony\Component\Serializer\Normalizer\NormalizerInterface;
21
use Symfony\Component\Serializer\Serializer;
22

23
class ValidationExceptionNormalizer implements NormalizerInterface, CacheableSupportsMethodInterface
24
{
25
    public function __construct(private readonly NormalizerInterface $decorated, private readonly ?NameConverterInterface $nameConverter)
26
    {
27
    }
87✔
28

29
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
30
    {
31
        $messages = [];
×
32
        foreach ($object->getConstraintViolationList() as $violation) {
×
33
            $class = \is_object($root = $violation->getRoot()) ? $root::class : null;
×
34

35
            if ($this->nameConverter instanceof AdvancedNameConverterInterface) {
×
36
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath(), $class, $format);
×
37
            } elseif ($this->nameConverter instanceof NameConverterInterface) {
×
38
                $propertyPath = $this->nameConverter->normalize($violation->getPropertyPath());
×
39
            } else {
40
                $propertyPath = $violation->getPropertyPath();
×
41
            }
42

43
            $messages[] = ($propertyPath ? "{$propertyPath}: " : '').$violation->getMessage();
×
44
        }
45

46
        $str = implode("\n", $messages);
×
47
        $object->setDetail($str);
×
48

49
        return $this->decorated->normalize($object, $format, $context);
×
50
    }
51

52
    public function supportsNormalization(mixed $data, string $format = null, array $context = []): bool
53
    {
NEW
54
        return $data instanceof ValidationException && $this->decorated->supportsNormalization($data, $format, $context);
×
55
    }
56

57
    public function hasCacheableSupportsMethod(): bool
58
    {
59
        if (method_exists(Serializer::class, 'getSupportedTypes')) {
×
60
            trigger_deprecation(
×
61
                'api-platform/core',
×
62
                '3.1',
×
63
                'The "%s()" method is deprecated, use "getSupportedTypes()" instead.',
×
64
                __METHOD__
×
65
            );
×
66
        }
67

NEW
68
        return false;
×
69
    }
70

71
    public function getSupportedTypes($format): array
72
    {
73
        return [ValidationException::class => false];
76✔
74
    }
75
}
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