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

api-platform / core / 7196499749

13 Dec 2023 02:17PM UTC coverage: 37.359% (+1.4%) from 36.003%
7196499749

push

github

web-flow
ci: conflict sebastian/comparator (#6032)

* ci: conflict sebastian/comparator

* for lowest

10295 of 27557 relevant lines covered (37.36%)

28.14 hits per line

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

35.71
/src/Symfony/Validator/Exception/ValidationException.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\Exception;
15

16
use ApiPlatform\JsonLd\ContextBuilderInterface;
17
use ApiPlatform\Metadata\Error as ErrorOperation;
18
use ApiPlatform\Metadata\ErrorResource;
19
use ApiPlatform\Metadata\Exception\ProblemExceptionInterface;
20
use ApiPlatform\Metadata\Util\CompositeIdentifierParser;
21
use ApiPlatform\Validator\Exception\ValidationException as BaseValidationException;
22
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;
23
use Symfony\Component\Serializer\Annotation\Groups;
24
use Symfony\Component\Serializer\Annotation\SerializedName;
25
use Symfony\Component\Validator\ConstraintViolationListInterface;
26
use Symfony\Component\WebLink\Link;
27

28
/**
29
 * Thrown when a validation error occurs.
30
 *
31
 * @author Kévin Dunglas <dunglas@gmail.com>
32
 */
33
#[ErrorResource(
34
    uriTemplate: '/validation_errors/{id}',
35
    status: 422,
36
    openapi: false,
37
    uriVariables: ['id'],
38
    provider: 'api_platform.validator.state.error_provider',
39
    shortName: 'ConstraintViolationList',
40
    operations: [
41
        new ErrorOperation(
42
            name: '_api_validation_errors_problem',
43
            outputFormats: ['json' => ['application/problem+json']],
44
            normalizationContext: ['groups' => ['json'],
45
                'skip_null_values' => true,
46
                'rfc_7807_compliant_errors' => true,
47
            ]),
48
        new ErrorOperation(
49
            name: '_api_validation_errors_hydra',
50
            outputFormats: ['jsonld' => ['application/problem+json']],
51
            links: [new Link(rel: ContextBuilderInterface::JSONLD_NS.'error', href: 'http://www.w3.org/ns/hydra/error')],
52
            normalizationContext: [
53
                'groups' => ['jsonld'],
54
                'skip_null_values' => true,
55
                'rfc_7807_compliant_errors' => true,
56
            ]
57
        ),
58
        new ErrorOperation(
59
            name: '_api_validation_errors_jsonapi',
60
            outputFormats: ['jsonapi' => ['application/vnd.api+json']],
61
            normalizationContext: ['groups' => ['jsonapi'], 'skip_null_values' => true, 'rfc_7807_compliant_errors' => true]
62
        ),
63
    ],
64
    graphQlOperations: []
65
)]
66
final class ValidationException extends BaseValidationException implements ConstraintViolationListAwareExceptionInterface, \Stringable, ProblemExceptionInterface, HttpExceptionInterface
67
{
68
    private int $status = 422;
69

70
    public function __construct(private readonly ConstraintViolationListInterface $constraintViolationList, string $message = '', int $code = 0, \Throwable $previous = null, string $errorTitle = null)
71
    {
72
        parent::__construct($message ?: $this->__toString(), $code, $previous, $errorTitle);
20✔
73
    }
74

75
    public function getId(): string
76
    {
77
        $ids = [];
×
78
        foreach ($this->getConstraintViolationList() as $violation) {
×
79
            $ids[] = $violation->getCode();
×
80
        }
81

82
        $id = 1 < \count($ids) ? CompositeIdentifierParser::stringify(identifiers: $ids) : ($ids[0] ?? null);
×
83

84
        if (!$id) {
×
85
            return spl_object_hash($this);
×
86
        }
87

88
        return $id;
×
89
    }
90

91
    #[SerializedName('hydra:title')]
92
    #[Groups(['jsonld'])]
93
    public function getHydraTitle(): string
94
    {
95
        return $this->errorTitle ?? 'An error occurred';
×
96
    }
97

98
    #[Groups(['jsonld'])]
99
    #[SerializedName('hydra:description')]
100
    public function getHydraDescription(): string
101
    {
102
        return $this->detail;
×
103
    }
104

105
    #[Groups(['jsonld', 'json'])]
106
    public function getType(): string
107
    {
108
        return '/validation_errors/'.$this->getId();
×
109
    }
110

111
    #[Groups(['jsonld', 'json'])]
112
    public function getTitle(): ?string
113
    {
114
        return $this->errorTitle ?? 'An error occurred';
×
115
    }
116

117
    #[Groups(['jsonld', 'json'])]
118
    private string $detail;
119

120
    public function getDetail(): ?string
121
    {
122
        return $this->detail;
×
123
    }
124

125
    public function setDetail(string $detail): void
126
    {
127
        $this->detail = $detail;
×
128
    }
129

130
    #[Groups(['jsonld', 'json'])]
131
    public function getStatus(): ?int
132
    {
133
        return $this->status;
×
134
    }
135

136
    public function setStatus(int $status): void
137
    {
138
        $this->status = $status;
×
139
    }
140

141
    #[Groups(['jsonld', 'json'])]
142
    public function getInstance(): ?string
143
    {
144
        return null;
×
145
    }
146

147
    #[SerializedName('violations')]
148
    #[Groups(['json', 'jsonld'])]
149
    public function getConstraintViolationList(): ConstraintViolationListInterface
150
    {
151
        return $this->constraintViolationList;
12✔
152
    }
153

154
    public function __toString(): string
155
    {
156
        $message = '';
20✔
157
        foreach ($this->constraintViolationList as $violation) {
20✔
158
            if ('' !== $message) {
8✔
159
                $message .= "\n";
4✔
160
            }
161
            if ($propertyPath = $violation->getPropertyPath()) {
8✔
162
                $message .= "$propertyPath: ";
8✔
163
            }
164

165
            $message .= $violation->getMessage();
8✔
166
        }
167

168
        return $message;
20✔
169
    }
170

171
    public function getStatusCode(): int
172
    {
173
        return $this->status;
×
174
    }
175

176
    public function getHeaders(): array
177
    {
178
        return [];
×
179
    }
180
}
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

© 2026 Coveralls, Inc