-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathConstraintViolationException.php
47 lines (37 loc) · 1.6 KB
/
ConstraintViolationException.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
<?php
/*
* Copyright (c) Fusonic GmbH. All rights reserved.
* Licensed under the MIT License. See LICENSE file in the project root for license information.
*/
declare(strict_types=1);
namespace Fusonic\HttpKernelBundle\Exception;
use Symfony\Component\Validator\ConstraintViolation;
use Symfony\Component\Validator\ConstraintViolationInterface;
use Symfony\Component\Validator\ConstraintViolationList;
use Symfony\Component\Validator\ConstraintViolationListInterface;
/**
* Wraps a {@see ConstraintViolationListInterface} into an exception. The intended usage of this exception is
* to throw this exception with the results of calling `validate` on the
* validator ({@see \Symfony\Component\Validator\Validator\ValidatorInterface}).
*/
class ConstraintViolationException extends \RuntimeException
{
public const NAME = 'ConstraintViolation';
public function __construct(private readonly ConstraintViolationListInterface $constraintViolationList)
{
$messages = [];
/** @var ConstraintViolation $constraintViolation */
foreach ($constraintViolationList as $constraintViolation) {
$messages[] = $constraintViolation->getMessage();
}
parent::__construct(\sprintf('%s: %s', self::NAME, implode('; ', $messages)));
}
public function getConstraintViolationList(): ConstraintViolationListInterface
{
return $this->constraintViolationList;
}
public static function fromConstraintViolation(ConstraintViolationInterface $constraintViolation): self
{
return new self(new ConstraintViolationList([$constraintViolation]));
}
}