|
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\Handler\FormErrorHandler as JMSFormErrorHandler; |
|
16
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
|
17
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
|
18
|
|
|
use Symfony\Component\Form\Form; |
|
19
|
|
|
use JMS\Serializer\YamlSerializationVisitor; |
|
20
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* Extend the JMS FormErrorHandler to include more informations when using the ViewHandler. |
|
24
|
|
|
*/ |
|
25
|
|
|
class FormErrorHandler extends JMSFormErrorHandler |
|
26
|
|
|
{ |
|
27
|
|
|
private $translator; |
|
|
|
|
|
|
28
|
|
|
|
|
29
|
2 |
|
public function __construct(TranslatorInterface $translator) |
|
30
|
|
|
{ |
|
31
|
2 |
|
$this->translator = $translator; |
|
32
|
2 |
|
parent::__construct($translator); |
|
33
|
2 |
|
} |
|
34
|
|
|
|
|
35
|
1 |
|
public function serializeFormToXml(XmlSerializationVisitor $visitor, Form $form, array $type, Context $context = null) |
|
36
|
|
|
{ |
|
37
|
1 |
|
if ($context) { |
|
38
|
1 |
|
$statusCode = $context->attributes->get('status_code'); |
|
39
|
1 |
|
if ($statusCode->isDefined()) { |
|
40
|
1 |
View Code Duplication |
if (null === $visitor->document) { |
|
|
|
|
|
|
41
|
1 |
|
$visitor->document = $visitor->createDocument(null, null, true); |
|
42
|
1 |
|
} |
|
43
|
|
|
|
|
44
|
1 |
|
$codeNode = $visitor->document->createElement('code'); |
|
45
|
1 |
|
$visitor->getCurrentNode()->appendChild($codeNode); |
|
46
|
1 |
|
$codeNode->appendChild($context->getNavigator()->accept($statusCode->get(), null, $context)); |
|
47
|
|
|
|
|
48
|
1 |
|
$messageNode = $visitor->document->createElement('message'); |
|
49
|
1 |
|
$visitor->getCurrentNode()->appendChild($messageNode); |
|
50
|
1 |
|
$messageNode->appendChild($context->getNavigator()->accept('Validation Failed', null, $context)); |
|
51
|
|
|
|
|
52
|
1 |
|
$errorsNode = $visitor->document->createElement('errors'); |
|
53
|
1 |
|
$visitor->getCurrentNode()->appendChild($errorsNode); |
|
54
|
1 |
|
$visitor->setCurrentNode($errorsNode); |
|
55
|
1 |
|
parent::serializeFormToXml($visitor, $form, $type); |
|
56
|
1 |
|
$visitor->revertCurrentNode(); |
|
57
|
|
|
|
|
58
|
1 |
|
return; |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
1 |
|
return parent::serializeFormToXml($visitor, $form, $type); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
1 |
View Code Duplication |
public function serializeFormToJson(JsonSerializationVisitor $visitor, Form $form, array $type, Context $context = null) |
|
|
|
|
|
|
66
|
|
|
{ |
|
67
|
1 |
|
$isRoot = null === $visitor->getRoot(); |
|
68
|
1 |
|
$result = $this->adaptFormArray(parent::serializeFormToJson($visitor, $form, $type), $context); |
|
|
|
|
|
|
69
|
|
|
|
|
70
|
1 |
|
if ($isRoot) { |
|
71
|
1 |
|
$visitor->setRoot($result); |
|
72
|
1 |
|
} |
|
73
|
|
|
|
|
74
|
1 |
|
return $result; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
View Code Duplication |
public function serializeFormToYml(YamlSerializationVisitor $visitor, Form $form, array $type, Context $context = null) |
|
|
|
|
|
|
78
|
|
|
{ |
|
79
|
|
|
$isRoot = null === $visitor->getRoot(); |
|
|
|
|
|
|
80
|
|
|
$result = $this->adaptFormArray(parent::serializeFormToYml($visitor, $form, $type), $context); |
|
|
|
|
|
|
81
|
|
|
|
|
82
|
|
|
if ($isRoot) { |
|
83
|
|
|
$visitor->setRoot($result); |
|
|
|
|
|
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
return $result; |
|
87
|
|
|
} |
|
88
|
|
|
|
|
89
|
1 |
|
private function adaptFormArray(\ArrayObject $serializedForm, Context $context = null) |
|
90
|
|
|
{ |
|
91
|
1 |
|
$statusCode = $this->getStatusCode($context); |
|
92
|
1 |
|
if (null !== $statusCode) { |
|
93
|
|
|
return [ |
|
94
|
1 |
|
'code' => $statusCode, |
|
95
|
1 |
|
'message' => 'Validation Failed', |
|
96
|
1 |
|
'errors' => $serializedForm, |
|
97
|
1 |
|
]; |
|
98
|
|
|
} |
|
99
|
|
|
|
|
100
|
|
|
return $serializedForm; |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
1 |
|
private function getStatusCode(Context $context = null) |
|
104
|
|
|
{ |
|
105
|
1 |
|
if (null === $context) { |
|
106
|
|
|
return; |
|
107
|
|
|
} |
|
108
|
|
|
|
|
109
|
1 |
|
$statusCode = $context->attributes->get('status_code'); |
|
110
|
1 |
|
if ($statusCode->isDefined()) { |
|
111
|
1 |
|
return $statusCode->get(); |
|
112
|
|
|
} |
|
113
|
|
|
} |
|
114
|
|
|
} |
|
115
|
|
|
|