Completed
Pull Request — master (#1711)
by
unknown
09:00
created

FormErrorNormalizer::setTranslationDomain()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 6
ccs 3
cts 3
cp 1
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 3
nc 1
nop 1
crap 1
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 19
    private $translationDomain = 'validators';
29
30 19
    public function __construct(TranslatorInterface $translator)
31 19
    {
32
        $this->translator = $translator;
33
    }
34
35
    public function setTranslationDomain($translationDomain)
36 2
    {
37
        $this->translationDomain = $translationDomain;
38
39 2
        return $this;
40 2
    }
41 2
42 2
    /**
43
     * {@inheritdoc}
44
     */
45
    public function normalize($object, $format = null, array $context = [])
46
    {
47
        return [
48 10
            'code' => isset($context['status_code']) ? $context['status_code'] : null,
49
            'message' => 'Validation Failed',
50 10
            'errors' => $this->convertFormToArray($object),
51
        ];
52
    }
53
54
    /**
55
     * {@inheritdoc}
56 2
     */
57
    public function supportsNormalization($data, $format = null)
58 2
    {
59
        return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid();
60 2
    }
61 2
62 2
    /**
63
     * This code has been taken from JMSSerializer.
64 2
     */
65 2
    private function convertFormToArray(FormInterface $data)
66 2
    {
67
        $form = $errors = [];
68 2
69 2
        foreach ($data->getErrors() as $error) {
70 2
            $errors[] = $this->getErrorMessage($error);
0 ignored issues
show
Documentation introduced by
$error is of type object<Symfony\Component...ormErrorIterator>|false, but the function expects a object<Symfony\Component\Form\FormError>.

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:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
71 2
        }
72 2
73 2
        if ($errors) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $errors of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
74
            $form['errors'] = $errors;
75 2
        }
76 2
77 2
        $children = [];
78
        foreach ($data->all() as $child) {
79 2
            if ($child instanceof FormInterface) {
80
                $children[$child->getName()] = $this->convertFormToArray($child);
81
            }
82 2
        }
83
84 2
        if ($children) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $children of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
85
            $form['children'] = $children;
86
        }
87
88 2
        return $form;
89
    }
90
91
    private function getErrorMessage(FormError $error)
92
    {
93
        if (null !== $error->getMessagePluralization()) {
94
            return $this->translator->transChoice($error->getMessageTemplate(), $error->getMessagePluralization(), $error->getMessageParameters(), $this->translationDomain);
95
        }
96
97
        return $this->translator->trans($error->getMessageTemplate(), $error->getMessageParameters(), $this->translationDomain);
98
    }
99
}
100