Completed
Push — master ( 7e9231...419bd7 )
by Guillaume
15:48
created

FormErrorsService   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 37
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 2

Importance

Changes 0
Metric Value
wmc 7
lcom 1
cbo 2
dl 0
loc 37
rs 10
c 0
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A __construct() 0 4 1
B getFormErrors() 0 17 6
1
<?php
2
namespace Starkerxp\StructureBundle\Services;
3
4
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
5
use Symfony\Component\Form\Form;
6
7
8
class FormErrorsService
9
{
10
11
    /**
12
     * @var Translator
13
     */
14
    private $translator;
15
16
    /**
17
     * FormErrorsService constructor.
18
     * @param $translator
19
     */
20
    public function __construct(Translator $translator)
21
    {
22
        $this->translator = $translator;
23
    }
24
25
26
    public function getFormErrors(Form $form)
27
    {
28
        $errors = [];
29
        foreach ($form->getErrors() as $error) {
30
            $errors[$form->getName()][] = $error->getMessage();
31
        }
32
        foreach ($form as $child) {
33
            if (!$child->isValid()) {
34
                foreach ($child->getErrors() as $error) {
35
                    $message = !empty($this->translator) ? $this->translator->trans($error->getMessage()) : $error->getMessage();
36
                    $errors[$child->getName()][] = $message;
37
                }
38
            }
39
        }
40
41
        return $errors;
42
    }
43
44
}
45