FormErrorNormalizer   A
last analyzed

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 19
cts 19
cp 1
rs 10
c 0
b 0
f 0

3 Methods

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