Completed
Pull Request — master (#311)
by
unknown
11:14 queued 03:41
created

ValidatorStep::allowExtraFields()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
ccs 3
cts 3
cp 1
rs 10
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
    /**
41
     * @param ValidatorInterface $validator
42
     */
43 3
    public function __construct(ValidatorInterface $validator)
44
    {
45 3
        $this->validator = $validator;
46 3
    }
47
48
    /**
49
     * @param string     $field
50
     * @param Constraint $constraint
51
     *
52
     * @return $this
53
     */
54 2
    public function add($field, Constraint $constraint)
55
    {
56 2
        if (!isset($this->constraints[$field])) {
57 2
            $this->constraints['fields'][$field] = [];
58 2
        }
59
60 2
        $this->constraints['fields'][$field][] = $constraint;
61
62 2
        return $this;
63
    }
64
65
    /**
66
     * @param boolean $flag
67
     */
68 1
    public function throwExceptions($flag = true)
69
    {
70 1
        $this->throwExceptions = $flag;
71 1
    }
72
73
    /**
74
     * @return array
75
     */
76 1
    public function getViolations()
77
    {
78 1
        return $this->violations;
79
    }
80
81
    /**
82
     * Use this option to allow extra fields without specific validation constraint set.
83
     */
84 2
    public function allowExtraFields()
85
    {
86 2
        $this->constraints['allowExtraFields'] = true;
87 2
    }
88
89 2
    /**
90 2
     * {@inheritdoc}
91
     */
92 2
    public function process(&$item)
93 1
    {
94
        $constraints = new Constraints\Collection($this->constraints);
95 1
        $list = $this->validator->validate($item, $constraints);
96
97 1
        if (count($list) > 0) {
98
            $this->violations[$this->line] = $list;
99 1
100
            if ($this->throwExceptions) {
101
                throw new ValidationException($list, $this->line);
102
            }
103
        }
104
105 1
        $this->line++;
106
107 1
        return 0 === count($list);
108
    }
109
110
    /**
111
     * {@inheritdoc}
112
     */
113
    public function getPriority()
114
    {
115
        return 128;
116
    }
117
}
118