Passed
Push — master ( 225f5c...ae5002 )
by Daniel
01:34
created

SymfonyValidatorFactory   A

Complexity

Total Complexity 3

Size/Duplication

Total Lines 26
Duplicated Lines 0 %

Test Coverage

Coverage 0%

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 3
eloc 8
c 1
b 0
f 0
dl 0
loc 26
ccs 0
cts 13
cp 0
rs 10

1 Method

Rating   Name   Duplication   Size   Complexity  
A createValidationResult() 0 16 3
1
<?php
2
3
namespace Selective\Validation\Factory;
4
5
use Selective\Validation\ValidationResult;
6
use Symfony\Component\Validator\ConstraintViolation;
7
use Symfony\Component\Validator\ConstraintViolationList;
8
9
/**
10
 * Symfony validation error collector.
11
 */
12
final class SymfonyValidatorFactory
13
{
14
    /**
15
     * Create validation result from array with errors.
16
     *
17
     * @param ConstraintViolationList $violations The validation errors
18
     * @param ValidationResult|null $result The result
19
     *
20
     * @return ValidationResult The result
21
     */
22
    public static function createValidationResult(
23
        ConstraintViolationList $violations,
24
        ValidationResult $result = null
25
    ): ValidationResult {
26
        if ($result === null) {
27
            $result = new ValidationResult();
28
        }
29
30
        /** @var ConstraintViolation $violation */
31
        foreach ($violations as $violation) {
32
            $field = $violation->getPropertyPath();
33
            $field = str_replace(']', '', str_replace('[', '', str_replace('][', '.', $field)));
34
            $result->addError($field, $violation->getMessage());
35
        }
36
37
        return $result;
38
    }
39
}
40