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 Symfony\Component\Form\FormError; |
15
|
|
|
use Symfony\Component\Form\FormInterface; |
16
|
|
|
use Symfony\Component\Serializer\Normalizer\NormalizerInterface; |
17
|
|
|
use Symfony\Component\Translation\TranslatorInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Normalizes invalid Form instances. |
21
|
|
|
* |
22
|
|
|
* @author Ener-Getick <[email protected]> |
23
|
|
|
*/ |
24
|
|
|
class FormErrorNormalizer implements NormalizerInterface |
25
|
|
|
{ |
26
|
|
|
private $translator; |
27
|
|
|
|
28
|
17 |
|
public function __construct(TranslatorInterface $translator) |
29
|
|
|
{ |
30
|
17 |
|
$this->translator = $translator; |
31
|
17 |
|
} |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* {@inheritdoc} |
35
|
|
|
*/ |
36
|
2 |
|
public function normalize($object, $format = null, array $context = []) |
37
|
|
|
{ |
38
|
|
|
return [ |
39
|
2 |
|
'code' => isset($context['status_code']) ? $context['status_code'] : null, |
40
|
2 |
|
'message' => 'Validation Failed', |
41
|
2 |
|
'errors' => $this->convertFormToArray($object), |
42
|
2 |
|
]; |
43
|
|
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* {@inheritdoc} |
47
|
|
|
*/ |
48
|
9 |
|
public function supportsNormalization($data, $format = null) |
49
|
|
|
{ |
50
|
9 |
|
return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid(); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* This code has been taken from JMSSerializer. |
55
|
|
|
*/ |
56
|
2 |
|
private function convertFormToArray(FormInterface $data) |
57
|
|
|
{ |
58
|
2 |
|
$form = $errors = []; |
59
|
|
|
|
60
|
2 |
|
foreach ($data->getErrors() as $error) { |
61
|
2 |
|
$errors[] = $this->getErrorMessage($error); |
|
|
|
|
62
|
2 |
|
} |
63
|
|
|
|
64
|
2 |
|
if ($errors) { |
|
|
|
|
65
|
2 |
|
$form['errors'] = $errors; |
66
|
2 |
|
} |
67
|
|
|
|
68
|
2 |
|
$children = []; |
69
|
2 |
|
foreach ($data->all() as $child) { |
70
|
2 |
|
if ($child instanceof FormInterface) { |
71
|
2 |
|
$children[$child->getName()] = $this->convertFormToArray($child); |
72
|
2 |
|
} |
73
|
2 |
|
} |
74
|
|
|
|
75
|
2 |
|
if ($children) { |
|
|
|
|
76
|
2 |
|
$form['children'] = $children; |
77
|
2 |
|
} |
78
|
|
|
|
79
|
2 |
|
return $form; |
80
|
|
|
} |
81
|
|
|
|
82
|
2 |
|
private function getErrorMessage(FormError $error) |
83
|
|
|
{ |
84
|
2 |
|
if (null !== $error->getMessagePluralization()) { |
85
|
|
|
return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), 'validators'); |
86
|
|
|
} |
87
|
|
|
|
88
|
2 |
|
return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), 'validators'); |
89
|
|
|
} |
90
|
|
|
} |
91
|
|
|
|
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: