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\Handler\SubscribingHandlerInterface; |
17
|
|
|
use JMS\Serializer\JsonSerializationVisitor; |
18
|
|
|
use JMS\Serializer\Visitor\SerializationVisitorInterface; |
19
|
|
|
use JMS\Serializer\XmlSerializationVisitor; |
20
|
|
|
use Symfony\Component\Form\Form; |
21
|
|
|
use JMS\Serializer\YamlSerializationVisitor; |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Extend the JMS FormErrorHandler to include more information when using the ViewHandler. |
25
|
|
|
* |
26
|
|
|
* @internal |
27
|
|
|
*/ |
28
|
|
|
class FormErrorHandler implements SubscribingHandlerInterface |
29
|
|
|
{ |
30
|
|
|
private $formErrorHandler; |
31
|
|
|
|
32
|
3 |
|
public function __construct(JMSFormErrorHandler $formErrorHandler) |
33
|
|
|
{ |
34
|
3 |
|
$this->formErrorHandler = $formErrorHandler; |
35
|
3 |
|
} |
36
|
|
|
|
37
|
4 |
|
public static function getSubscribingMethods(): array |
38
|
|
|
{ |
39
|
4 |
|
return JMSFormErrorHandler::getSubscribingMethods(); |
40
|
|
|
} |
41
|
|
|
|
42
|
1 |
|
public function serializeFormToXml(XmlSerializationVisitor $visitor, Form $form, array $type, Context $context = null) |
43
|
|
|
{ |
44
|
1 |
|
if ($context) { |
45
|
1 |
|
if ($context->hasAttribute('status_code')) { |
46
|
1 |
|
$document = $visitor->getDocument(true); |
|
|
|
|
47
|
1 |
|
if (!$visitor->getCurrentNode()) { |
48
|
1 |
|
$visitor->createRoot(); |
49
|
|
|
} |
50
|
|
|
|
51
|
1 |
|
$codeNode = $document->createElement('code'); |
52
|
1 |
|
$visitor->getCurrentNode()->appendChild($codeNode); |
53
|
1 |
|
$codeNode->appendChild($context->getNavigator()->accept($context->getAttribute('status_code'), null, $context)); |
|
|
|
|
54
|
|
|
|
55
|
1 |
|
$messageNode = $document->createElement('message'); |
56
|
1 |
|
$visitor->getCurrentNode()->appendChild($messageNode); |
57
|
1 |
|
$messageNode->appendChild($context->getNavigator()->accept('Validation Failed', null, $context)); |
|
|
|
|
58
|
|
|
|
59
|
1 |
|
$errorsNode = $document->createElement('errors'); |
60
|
1 |
|
$visitor->getCurrentNode()->appendChild($errorsNode); |
61
|
1 |
|
$visitor->setCurrentNode($errorsNode); |
62
|
|
|
|
63
|
1 |
|
$errorNodes = $this->formErrorHandler->serializeFormToXml($visitor, $form, $type); |
64
|
1 |
|
$errorsNode->appendChild($errorNodes); |
65
|
|
|
|
66
|
1 |
|
$visitor->revertCurrentNode(); |
67
|
|
|
|
68
|
1 |
|
return $visitor->getCurrentNode(); |
69
|
|
|
} |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $this->formErrorHandler->serializeFormToXml($visitor, $form, $type); |
73
|
|
|
} |
74
|
|
|
|
75
|
1 |
View Code Duplication |
public function serializeFormToJson(JsonSerializationVisitor $visitor, Form $form, array $type, Context $context = null) |
|
|
|
|
76
|
|
|
{ |
77
|
1 |
|
$isRoot = !interface_exists(SerializationVisitorInterface::class) && null === $visitor->getRoot(); |
|
|
|
|
78
|
1 |
|
$result = $this->adaptFormArray($this->formErrorHandler->serializeFormToJson($visitor, $form, $type), $context); |
79
|
|
|
|
80
|
1 |
|
if ($isRoot) { |
81
|
|
|
$visitor->setRoot($result); |
|
|
|
|
82
|
|
|
} |
83
|
|
|
|
84
|
1 |
|
return $result; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
View Code Duplication |
public function serializeFormToYml(YamlSerializationVisitor $visitor, Form $form, array $type, Context $context = null) |
|
|
|
|
88
|
|
|
{ |
89
|
|
|
$isRoot = null === $visitor->getRoot(); |
90
|
|
|
$result = $this->adaptFormArray($this->formErrorHandler->serializeFormToYml($visitor, $form, $type), $context); |
|
|
|
|
91
|
|
|
|
92
|
|
|
if ($isRoot) { |
93
|
|
|
$visitor->setRoot($result); |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
return $result; |
97
|
|
|
} |
98
|
|
|
|
99
|
|
|
public function __call($name, $arguments) |
100
|
|
|
{ |
101
|
|
|
return call_user_func_array([$this->formErrorHandler, $name], $arguments); |
102
|
|
|
} |
103
|
|
|
|
104
|
1 |
|
private function adaptFormArray(\ArrayObject $serializedForm, Context $context = null) |
105
|
|
|
{ |
106
|
1 |
|
$statusCode = $this->getStatusCode($context); |
107
|
1 |
|
if (null !== $statusCode) { |
108
|
|
|
return [ |
109
|
1 |
|
'code' => $statusCode, |
110
|
1 |
|
'message' => 'Validation Failed', |
111
|
1 |
|
'errors' => $serializedForm, |
112
|
|
|
]; |
113
|
|
|
} |
114
|
|
|
|
115
|
|
|
return $serializedForm; |
116
|
|
|
} |
117
|
|
|
|
118
|
1 |
|
private function getStatusCode(Context $context = null) |
119
|
|
|
{ |
120
|
1 |
|
if (null === $context) { |
121
|
|
|
return; |
122
|
|
|
} |
123
|
|
|
|
124
|
1 |
|
if ($context->hasAttribute('status_code')) { |
125
|
1 |
|
return $context->getAttribute('status_code'); |
126
|
|
|
} |
127
|
|
|
} |
128
|
|
|
} |
129
|
|
|
|
This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.
If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.
In this case you can add the
@ignore
PhpDoc annotation to the duplicate definition and it will be ignored.