|
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
|
4 |
|
public function __construct(ValidatorInterface $validator) |
|
44
|
|
|
{ |
|
45
|
4 |
|
$this->validator = $validator; |
|
46
|
4 |
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param string $field |
|
50
|
|
|
* @param Constraint $constraint |
|
51
|
|
|
* |
|
52
|
|
|
* @return $this |
|
53
|
|
|
*/ |
|
54
|
3 |
|
public function add($field, Constraint $constraint) |
|
55
|
|
|
{ |
|
56
|
3 |
|
if (!isset($this->constraints[$field])) { |
|
57
|
3 |
|
$this->constraints['fields'][$field] = []; |
|
58
|
3 |
|
} |
|
59
|
|
|
|
|
60
|
3 |
|
$this->constraints['fields'][$field][] = $constraint; |
|
61
|
|
|
|
|
62
|
3 |
|
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
|
2 |
|
public function getViolations() |
|
77
|
|
|
{ |
|
78
|
2 |
|
return $this->violations; |
|
79
|
|
|
} |
|
80
|
|
|
|
|
81
|
|
|
/** |
|
82
|
|
|
* Use this option to allow extra fields without specific validation constraint set. |
|
83
|
|
|
*/ |
|
84
|
1 |
|
public function allowExtraFields() |
|
85
|
|
|
{ |
|
86
|
1 |
|
$this->constraints['allowExtraFields'] = true; |
|
87
|
1 |
|
} |
|
88
|
|
|
|
|
89
|
|
|
/** |
|
90
|
|
|
* {@inheritdoc} |
|
91
|
|
|
*/ |
|
92
|
3 |
|
public function process(&$item) |
|
93
|
|
|
{ |
|
94
|
3 |
|
$constraints = new Constraints\Collection($this->constraints); |
|
95
|
3 |
|
$list = $this->validator->validate($item, $constraints); |
|
96
|
|
|
|
|
97
|
3 |
|
if (count($list) > 0) { |
|
98
|
3 |
|
$this->violations[$this->line] = $list; |
|
99
|
|
|
|
|
100
|
3 |
|
if ($this->throwExceptions) { |
|
101
|
1 |
|
throw new ValidationException($list, $this->line); |
|
102
|
|
|
} |
|
103
|
2 |
|
} |
|
104
|
|
|
|
|
105
|
2 |
|
$this->line++; |
|
106
|
|
|
|
|
107
|
2 |
|
return 0 === count($list); |
|
108
|
|
|
} |
|
109
|
|
|
|
|
110
|
|
|
/** |
|
111
|
|
|
* {@inheritdoc} |
|
112
|
|
|
*/ |
|
113
|
1 |
|
public function getPriority() |
|
114
|
|
|
{ |
|
115
|
1 |
|
return 128; |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|