• 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

90.91
/src/Hydra/Serializer/ConstraintViolationListNormalizer.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\Hydra\Serializer;
15

16
use ApiPlatform\Api\UrlGeneratorInterface;
17
use ApiPlatform\Serializer\AbstractConstraintViolationListNormalizer;
18
use Symfony\Component\Serializer\NameConverter\NameConverterInterface;
19

20
/**
21
 * Converts {@see \Symfony\Component\Validator\ConstraintViolationListInterface} to a Hydra error representation.
22
 *
23
 * @author Kévin Dunglas <dunglas@gmail.com>
24
 */
25
final class ConstraintViolationListNormalizer extends AbstractConstraintViolationListNormalizer
26
{
27
    public const FORMAT = 'jsonld';
28

29
    public function __construct(private readonly UrlGeneratorInterface $urlGenerator, array $serializePayloadFields = null, NameConverterInterface $nameConverter = null)
30
    {
31
        parent::__construct($serializePayloadFields, $nameConverter);
117✔
32
    }
33

34
    /**
35
     * {@inheritdoc}
36
     */
37
    public function normalize(mixed $object, string $format = null, array $context = []): array|string|int|float|bool|\ArrayObject|null
38
    {
39
        [$messages, $violations] = $this->getMessagesAndViolations($object);
27✔
40

41
        // TODO: in api platform 4 this will be the default, as right now we serialize a ValidationException instead of a ConstraintViolationList
42
        if ($context['rfc_7807_compliant_errors'] ?? false) {
27✔
NEW
43
            return $violations;
×
44
        }
45

46
        return [
27✔
47
            '@context' => $this->urlGenerator->generate('api_jsonld_context', ['shortName' => 'ConstraintViolationList']),
27✔
48
            '@type' => 'ConstraintViolationList',
27✔
49
            'hydra:title' => $context['title'] ?? 'An error occurred',
27✔
50
            'hydra:description' => $messages ? implode("\n", $messages) : (string) $object,
27✔
51
            'violations' => $violations,
27✔
52
        ];
27✔
53
    }
54
}
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