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

FormErrorsService::getFormErrors()   B

Complexity

Conditions 6
Paths 10

Size

Total Lines 17
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 17
rs 8.8571
c 0
b 0
f 0
cc 6
eloc 10
nc 10
nop 1
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