Completed
Push — master ( 64ef74...18870f )
by Christian
10s
created

FormErrorNormalizer   A

Complexity

Total Complexity 11

Size/Duplication

Total Lines 51
Duplicated Lines 0 %

Coupling/Cohesion

Components 0
Dependencies 1

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 11
lcom 0
cbo 1
dl 0
loc 51
ccs 24
cts 24
cp 1
rs 10
c 0
b 0
f 0

3 Methods

Rating   Name   Duplication   Size   Complexity  
A normalize() 0 8 2
A supportsNormalization() 0 4 3
B convertFormToArray() 0 25 6
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
18
/**
19
 * Normalizes invalid Form instances.
20
 *
21
 * @author Ener-Getick <[email protected]>
22
 */
23
class FormErrorNormalizer implements NormalizerInterface
24
{
25
    /**
26
     * {@inheritdoc}
27
     */
28 19
    public function normalize($object, $format = null, array $context = [])
29
    {
30 19
        return [
31 19
            'code' => isset($context['status_code']) ? $context['status_code'] : null,
32
            'message' => 'Validation Failed',
33
            'errors' => $this->convertFormToArray($object),
34
        ];
35
    }
36 2
37
    /**
38
     * {@inheritdoc}
39 2
     */
40 2
    public function supportsNormalization($data, $format = null)
41 2
    {
42 2
        return $data instanceof FormInterface && $data->isSubmitted() && !$data->isValid();
43
    }
44
45
    /**
46
     * This code has been taken from JMSSerializer.
47
     */
48 10
    private function convertFormToArray(FormInterface $data)
49
    {
50 10
        $form = $errors = [];
51
52
        foreach ($data->getErrors() as $error) {
53
            $errors[] = $error->getMessage();
54
        }
55
56 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...
57
            $form['errors'] = $errors;
58 2
        }
59
60 2
        $children = [];
61 2
        foreach ($data->all() as $child) {
62 2
            if ($child instanceof FormInterface) {
63
                $children[$child->getName()] = $this->convertFormToArray($child);
64 2
            }
65 2
        }
66 2
67
        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...
68 2
            $form['children'] = $children;
69 2
        }
70 2
71 2
        return $form;
72 2
    }
73
}
74