1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* Copyright 2014 SURFnet bv |
5
|
|
|
* |
6
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License"); |
7
|
|
|
* you may not use this file except in compliance with the License. |
8
|
|
|
* You may obtain a copy of the License at |
9
|
|
|
* |
10
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0 |
11
|
|
|
* |
12
|
|
|
* Unless required by applicable law or agreed to in writing, software |
13
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS, |
14
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
15
|
|
|
* See the License for the specific language governing permissions and |
16
|
|
|
* limitations under the License. |
17
|
|
|
*/ |
18
|
|
|
|
19
|
|
|
namespace Surfnet\StepupBundle\Exception; |
20
|
|
|
|
21
|
|
|
use Symfony\Component\Validator\ConstraintViolationListInterface; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Thrown when a client provided invalid input to the application. |
25
|
|
|
*/ |
26
|
|
|
class BadJsonRequestException extends \RuntimeException |
27
|
|
|
{ |
28
|
|
|
/** |
29
|
|
|
* @var string[] |
30
|
|
|
*/ |
31
|
|
|
private $errors; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* @param ConstraintViolationListInterface $violations |
35
|
|
|
* @param string $violationsRoot The name of the object that was validated. |
36
|
|
|
* @param string[] $errors |
37
|
|
|
* @param string $message |
38
|
|
|
* @return self |
39
|
|
|
*/ |
40
|
|
|
public static function createForViolationsAndErrors( |
41
|
|
|
ConstraintViolationListInterface $violations, |
42
|
|
|
$violationsRoot, |
43
|
|
|
array $errors, |
44
|
|
|
$message = 'JSON could not be reconstituted into valid object.' |
45
|
|
|
) { |
46
|
|
|
$allErrors = array_merge(self::mapViolationsToErrorStrings($violations, $violationsRoot), $errors); |
47
|
|
|
|
48
|
|
|
return new self($allErrors, $message); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* @param string[] $errors |
53
|
|
|
* @param string $message |
54
|
|
|
* @param int $code |
55
|
|
|
* @param \Exception|null $previous |
56
|
|
|
*/ |
57
|
|
|
public function __construct( |
58
|
|
|
array $errors, |
59
|
|
|
$message = 'JSON could not be reconstituted into valid object.', |
60
|
|
|
$code = 0, |
61
|
|
|
\Exception $previous = null |
62
|
|
|
) { |
63
|
|
|
parent::__construct($message, $code, $previous); |
64
|
|
|
|
65
|
|
|
$this->errors = $errors; |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
/** |
69
|
|
|
* @return string[] |
70
|
|
|
*/ |
71
|
|
|
public function getErrors() |
72
|
|
|
{ |
73
|
|
|
return $this->errors; |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/** |
77
|
|
|
* @param ConstraintViolationListInterface $violations |
78
|
|
|
* @param string $root |
79
|
|
|
* @return array |
80
|
|
|
*/ |
81
|
|
|
private static function mapViolationsToErrorStrings(ConstraintViolationListInterface $violations, $root) |
82
|
|
|
{ |
83
|
|
|
$errors = []; |
84
|
|
|
|
85
|
|
|
foreach ($violations as $violation) { |
86
|
|
|
/** @var ConstraintViolationInterface $violation */ |
87
|
|
|
$errors[] = sprintf('%s.%s: %s', $root, $violation->getPropertyPath(), $violation->getMessage()); |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
return $errors; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
|