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 FOS\RestBundle\Util\FormWrapper; |
15
|
|
|
use Symfony\Component\Form\FormError; |
16
|
|
|
use Symfony\Component\Form\FormInterface; |
17
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
18
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* Normalizes invalid Form instances. |
22
|
|
|
* |
23
|
|
|
* @author Ener-Getick <[email protected]> |
24
|
|
|
*/ |
25
|
|
|
class FormErrorNormalizer implements NormalizerInterface |
26
|
|
|
{ |
27
|
|
|
private $translator; |
28
|
|
|
|
29
|
13 |
|
public function __construct(TranslatorInterface $translator) |
30
|
|
|
{ |
31
|
13 |
|
$this->translator = $translator; |
32
|
13 |
|
} |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* {@inheritdoc} |
36
|
|
|
*/ |
37
|
2 |
|
public function normalize($object, $format = null, array $context = []) |
38
|
|
|
{ |
39
|
2 |
|
if ($object instanceof FormWrapper) { |
40
|
1 |
|
$object = $object->form; |
41
|
1 |
|
} |
42
|
|
|
|
43
|
|
|
return [ |
44
|
2 |
|
'code' => isset($context['status_code']) ? $context['status_code'] : null, |
45
|
2 |
|
'message' => 'Validation Failed', |
46
|
2 |
|
'errors' => $this->convertFormToArray($object), |
47
|
2 |
|
]; |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* {@inheritdoc} |
52
|
|
|
*/ |
53
|
7 |
|
public function supportsNormalization($data, $format = null) |
54
|
|
|
{ |
55
|
7 |
|
if ($data instanceof FormWrapper) { |
56
|
1 |
|
$data = $data->form; |
57
|
1 |
|
} |
58
|
|
|
|
59
|
7 |
|
return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid(); |
60
|
|
|
} |
61
|
|
|
|
62
|
|
|
/** |
63
|
|
|
* This code has been taken from JMSSerializer. |
64
|
|
|
*/ |
65
|
2 |
|
private function convertFormToArray(FormInterface $data) |
66
|
|
|
{ |
67
|
2 |
|
$form = $errors = []; |
68
|
|
|
|
69
|
2 |
|
foreach ($data->getErrors() as $error) { |
70
|
2 |
|
$errors[] = $this->getErrorMessage($error); |
|
|
|
|
71
|
2 |
|
} |
72
|
|
|
|
73
|
2 |
|
if ($errors) { |
|
|
|
|
74
|
2 |
|
$form['errors'] = $errors; |
75
|
2 |
|
} |
76
|
|
|
|
77
|
2 |
|
$children = []; |
78
|
2 |
|
foreach ($data->all() as $child) { |
79
|
2 |
|
if ($child instanceof FormInterface) { |
80
|
2 |
|
$children[$child->getName()] = $this->convertFormToArray($child); |
81
|
2 |
|
} |
82
|
2 |
|
} |
83
|
|
|
|
84
|
2 |
|
if ($children) { |
|
|
|
|
85
|
2 |
|
$form['children'] = $children; |
86
|
2 |
|
} |
87
|
|
|
|
88
|
2 |
|
return $form; |
89
|
|
|
} |
90
|
|
|
|
91
|
2 |
|
private function getErrorMessage(FormError $error) |
92
|
|
|
{ |
93
|
2 |
|
if (null !== $error->getMessagePluralization()) { |
94
|
|
|
return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), 'validators'); |
95
|
|
|
} |
96
|
|
|
|
97
|
2 |
|
return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators'); |
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: