|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/** |
|
4
|
|
|
* GpsLab component. |
|
5
|
|
|
* |
|
6
|
|
|
* @author Peter Gribanov <[email protected]> |
|
7
|
|
|
* @copyright Copyright (c) 2011, Peter Gribanov |
|
8
|
|
|
* @license http://opensource.org/licenses/MIT |
|
9
|
|
|
*/ |
|
10
|
|
|
|
|
11
|
|
|
namespace GpsLab\Component\Middleware\Exception; |
|
12
|
|
|
|
|
13
|
|
|
use Symfony\Component\Validator\ConstraintViolationInterface; |
|
14
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
|
15
|
|
|
use Symfony\Component\Validator\Exception\ValidatorException; |
|
16
|
|
|
|
|
17
|
|
|
class InvalidMessageException extends ValidatorException |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* @var ConstraintViolationListInterface |
|
21
|
|
|
*/ |
|
22
|
|
|
protected $violations; |
|
23
|
|
|
|
|
24
|
|
|
/** |
|
25
|
|
|
* @param ConstraintViolationListInterface $violations |
|
26
|
|
|
* @param int $code |
|
27
|
|
|
* @param \Exception|null $previous |
|
28
|
|
|
*/ |
|
29
|
4 |
|
public function __construct(ConstraintViolationListInterface $violations, $code = 0, \Exception $previous = null) |
|
30
|
|
|
{ |
|
31
|
4 |
|
$this->violations = $violations; |
|
32
|
4 |
|
$message = ''; |
|
33
|
|
|
|
|
34
|
|
|
// get first message from violations |
|
35
|
4 |
|
foreach ($violations as $violation) { |
|
36
|
|
|
/* @var $violation ConstraintViolationInterface */ |
|
37
|
2 |
|
$message = $violation->getMessage(); |
|
38
|
2 |
|
break; |
|
39
|
4 |
|
} |
|
40
|
|
|
|
|
41
|
4 |
|
parent::__construct($message, $code, $previous); |
|
42
|
4 |
|
} |
|
43
|
|
|
|
|
44
|
|
|
/** |
|
45
|
|
|
* @return ConstraintViolationListInterface |
|
46
|
|
|
*/ |
|
47
|
3 |
|
public function getViolations() |
|
48
|
|
|
{ |
|
49
|
3 |
|
return $this->violations; |
|
50
|
|
|
} |
|
51
|
|
|
|
|
52
|
|
|
/** |
|
53
|
|
|
* @return string[] |
|
54
|
|
|
*/ |
|
55
|
3 |
|
public function getMessages() |
|
56
|
|
|
{ |
|
57
|
3 |
|
$messages = []; |
|
58
|
|
|
|
|
59
|
3 |
|
foreach ($this->violations as $violation) { |
|
60
|
|
|
/* @var $violation ConstraintViolationInterface */ |
|
61
|
2 |
|
$messages[$violation->getPropertyPath()] = $violation->getMessage(); |
|
62
|
3 |
|
} |
|
63
|
|
|
|
|
64
|
3 |
|
return $messages; |
|
65
|
|
|
} |
|
66
|
|
|
} |
|
67
|
|
|
|