Completed
Pull Request — master (#311)
by
unknown
03:07
created

ValidatorStep::getViolations()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
ccs 2
cts 2
cp 1
cc 1
eloc 2
nc 1
nop 0
crap 1
1
<?php
2
3
namespace Ddeboer\DataImport\Step;
4
5
use Ddeboer\DataImport\Exception\ValidationException;
6
use Symfony\Component\Validator\Constraints;
7
use Symfony\Component\Validator\Constraint;
8
use Symfony\Component\Validator\Validator\ValidatorInterface;
9
10
/**
11
 * @author Markus Bachmann <[email protected]>
12
 */
13
class ValidatorStep implements PriorityStep
14
{
15
    /**
16
     * @var array
17
     */
18
    private $constraints = [];
19
20
    /**
21
     * @var array
22
     */
23
    private $violations = [];
24
25
    /**
26
     * @var boolean
27
     */
28
    private $throwExceptions = false;
29
30
    /**
31
     * @var integer
32
     */
33
    private $line = 1;
34
35
    /**
36
     * @var ValidatorInterface
37
     */
38
    private $validator;
39
40
    private $possibleOptions = [ 'groups', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage' ];
41
42
    /**
43
     * @param ValidatorInterface $validator
44
     */
45 4
    public function __construct(ValidatorInterface $validator)
46
    {
47 4
        $this->validator = $validator;
48 4
    }
49
50
    /**
51
     * @param string     $field
52
     * @param Constraint $constraint
53
     *
54
     * @return $this
55
     */
56 3
    public function add($field, Constraint $constraint)
57
    {
58 3
        if (!isset($this->constraints[$field])) {
59 3
            $this->constraints['fields'][$field] = [];
60 3
        }
61
62 3
        $this->constraints['fields'][$field][] = $constraint;
63
64 3
        return $this;
65
    }
66
67
    /**
68
     * @param boolean $flag
69
     */
70 1
    public function throwExceptions($flag = true)
71
    {
72 1
        $this->throwExceptions = $flag;
73 1
    }
74
75
    /**
76
     * @return array
77
     */
78 2
    public function getViolations()
79
    {
80 2
        return $this->violations;
81
    }
82
83
    /**
84
     * Add additional options for the constraints
85
     * @param string $option
86
     * @param $optionValue
87
     */
88 1
    public function addOption($option, $optionValue)
89
    {
90 1
        if (!isset($this->possibleOptions[$option])) {
91 1
            return;
92
        }
93
94
        $this->constraints[$option] = $optionValue;
95
    }
96
97
    /**
98
     * {@inheritdoc}
99
     */
100 3
    public function process(&$item)
101
    {
102 3
        $constraints = new Constraints\Collection($this->constraints);
103 3
        $list = $this->validator->validate($item, $constraints);
104
105 3
        if (count($list) > 0) {
106 3
            $this->violations[$this->line] = $list;
107
108 3
            if ($this->throwExceptions) {
109 1
                throw new ValidationException($list, $this->line);
110
            }
111 2
        }
112
113 2
        $this->line++;
114
115 2
        return 0 === count($list);
116
    }
117
118
    /**
119
     * {@inheritdoc}
120
     */
121 1
    public function getPriority()
122
    {
123 1
        return 128;
124
    }
125
}
126