1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/* |
4
|
|
|
* This file is part of the FOSRestBundle package. |
5
|
|
|
* |
6
|
|
|
* (c) FriendsOfSymfony <http://friendsofsymfony.github.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
|
|
|
namespace FOS\RestBundle\Serializer\Normalizer; |
13
|
|
|
|
14
|
|
|
use JMS\Serializer\Context; |
15
|
|
|
use JMS\Serializer\GraphNavigator; |
16
|
|
|
use JMS\Serializer\Handler\SubscribingHandlerInterface; |
17
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
18
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
19
|
|
|
|
20
|
|
|
class ExceptionHandler extends AbstractExceptionNormalizer implements SubscribingHandlerInterface |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* @return array |
24
|
|
|
*/ |
25
|
2 |
|
public static function getSubscribingMethods() |
26
|
|
|
{ |
27
|
|
|
return [ |
28
|
|
|
[ |
29
|
2 |
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
30
|
2 |
|
'format' => 'json', |
31
|
2 |
|
'type' => \Exception::class, |
32
|
2 |
|
'method' => 'serializeToJson', |
33
|
2 |
|
], |
34
|
|
|
[ |
35
|
2 |
|
'direction' => GraphNavigator::DIRECTION_SERIALIZATION, |
36
|
2 |
|
'format' => 'xml', |
37
|
2 |
|
'type' => \Exception::class, |
38
|
2 |
|
'method' => 'serializeToXml', |
39
|
2 |
|
], |
40
|
2 |
|
]; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param JsonSerializationVisitor $visitor |
45
|
|
|
* @param \Exception $exception |
46
|
|
|
* @param array $type |
47
|
|
|
* @param Context $context |
48
|
|
|
* |
49
|
|
|
* @return array |
50
|
|
|
*/ |
51
|
2 |
|
public function serializeToJson( |
52
|
|
|
JsonSerializationVisitor $visitor, |
53
|
|
|
\Exception $exception, |
54
|
|
|
array $type, |
55
|
|
|
Context $context |
56
|
|
|
) { |
57
|
2 |
|
$data = $this->convertToArray($exception, $context); |
58
|
|
|
|
59
|
2 |
|
return $visitor->visitArray($data, $type, $context); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* @param XmlSerializationVisitor $visitor |
64
|
|
|
* @param \Exception $exception |
65
|
|
|
* @param array $type |
66
|
|
|
* @param Context $context |
67
|
|
|
*/ |
68
|
1 |
|
public function serializeToXml( |
69
|
|
|
XmlSerializationVisitor $visitor, |
70
|
|
|
\Exception $exception, |
71
|
|
|
array $type, |
|
|
|
|
72
|
|
|
Context $context |
73
|
|
|
) { |
74
|
1 |
|
$data = $this->convertToArray($exception, $context); |
75
|
|
|
|
76
|
1 |
View Code Duplication |
if (null === $visitor->document) { |
77
|
1 |
|
$visitor->document = $visitor->createDocument(null, null, true); |
|
|
|
|
78
|
1 |
|
} |
79
|
|
|
|
80
|
1 |
|
foreach ($data as $key => $value) { |
81
|
1 |
|
$entryNode = $visitor->document->createElement($key); |
82
|
1 |
|
$visitor->getCurrentNode()->appendChild($entryNode); |
83
|
1 |
|
$visitor->setCurrentNode($entryNode); |
84
|
|
|
|
85
|
1 |
|
$node = $context->getNavigator()->accept($value, null, $context); |
86
|
1 |
|
if (null !== $node) { |
87
|
1 |
|
$visitor->getCurrentNode()->appendChild($node); |
88
|
1 |
|
} |
89
|
|
|
|
90
|
1 |
|
$visitor->revertCurrentNode(); |
91
|
1 |
|
} |
92
|
1 |
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* @param \Exception $exception |
96
|
|
|
* @param Context $context |
97
|
|
|
* |
98
|
|
|
* @return array |
99
|
3 |
|
*/ |
100
|
|
|
protected function convertToArray(\Exception $exception, Context $context) |
101
|
3 |
|
{ |
102
|
|
|
$data = []; |
103
|
3 |
|
|
104
|
3 |
|
$templateData = $context->attributes->get('template_data'); |
|
|
|
|
105
|
3 |
|
if ($templateData->isDefined()) { |
106
|
3 |
|
$templateData = $templateData->get(); |
107
|
|
|
if (array_key_exists('status_code', $templateData)) { |
108
|
3 |
|
$data['code'] = $statusCode = $templateData['status_code']; |
109
|
|
|
} |
110
|
3 |
|
} |
111
|
|
|
|
112
|
|
|
$data['message'] = $this->getExceptionMessage($exception, isset($statusCode) ? $statusCode : null); |
113
|
|
|
|
114
|
|
|
return $data; |
115
|
|
|
} |
116
|
|
|
} |
117
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.