Completed
Push — master ( 665986...1514e7 )
by Guilh
08:09 queued 05:04
created

FormErrorNormalizer::supportsNormalization()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 3
eloc 2
nc 3
nop 2
crap 3
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);
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...
62 2
        }
63
64 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...
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) {
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...
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