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
|
|
|
* Possible options to set for Constraints\Collection |
42
|
|
|
* @var array |
43
|
|
|
*/ |
44
|
|
|
private $possibleOptions = [ 'groups', 'allowExtraFields', 'allowMissingFields', 'extraFieldsMessage', 'missingFieldsMessage' ]; |
45
|
|
|
|
46
|
|
|
/** |
47
|
|
|
* @param ValidatorInterface $validator |
48
|
|
|
*/ |
49
|
4 |
|
public function __construct(ValidatorInterface $validator) |
50
|
|
|
{ |
51
|
4 |
|
$this->validator = $validator; |
52
|
4 |
|
} |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @param string $field |
56
|
|
|
* @param Constraint $constraint |
57
|
|
|
* |
58
|
|
|
* @return $this |
59
|
|
|
*/ |
60
|
3 |
|
public function add($field, Constraint $constraint) |
61
|
|
|
{ |
62
|
3 |
|
if (!isset($this->constraints[$field])) { |
63
|
3 |
|
$this->constraints['fields'][$field] = []; |
64
|
3 |
|
} |
65
|
|
|
|
66
|
3 |
|
$this->constraints['fields'][$field][] = $constraint; |
67
|
|
|
|
68
|
3 |
|
return $this; |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* @param boolean $flag |
73
|
|
|
*/ |
74
|
1 |
|
public function throwExceptions($flag = true) |
75
|
|
|
{ |
76
|
1 |
|
$this->throwExceptions = $flag; |
77
|
1 |
|
} |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @return array |
81
|
|
|
*/ |
82
|
2 |
|
public function getViolations() |
83
|
|
|
{ |
84
|
2 |
|
return $this->violations; |
85
|
|
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Add additional options for the constraints |
89
|
|
|
* @param string $option |
90
|
|
|
* @param $optionValue |
91
|
|
|
*/ |
92
|
1 |
|
public function addOption($option, $optionValue) |
93
|
|
|
{ |
94
|
1 |
|
if (!isset($this->possibleOptions[$option])) { |
95
|
1 |
|
return; |
96
|
|
|
} |
97
|
|
|
|
98
|
|
|
$this->constraints[$option] = $optionValue; |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
/** |
102
|
|
|
* {@inheritdoc} |
103
|
|
|
*/ |
104
|
3 |
|
public function process(&$item) |
105
|
|
|
{ |
106
|
3 |
|
$constraints = new Constraints\Collection($this->constraints); |
107
|
3 |
|
$list = $this->validator->validate($item, $constraints); |
108
|
|
|
|
109
|
3 |
|
if (count($list) > 0) { |
110
|
3 |
|
$this->violations[$this->line] = $list; |
111
|
|
|
|
112
|
3 |
|
if ($this->throwExceptions) { |
113
|
1 |
|
throw new ValidationException($list, $this->line); |
114
|
|
|
} |
115
|
2 |
|
} |
116
|
|
|
|
117
|
2 |
|
$this->line++; |
118
|
|
|
|
119
|
2 |
|
return 0 === count($list); |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* {@inheritdoc} |
124
|
|
|
*/ |
125
|
1 |
|
public function getPriority() |
126
|
|
|
{ |
127
|
1 |
|
return 128; |
128
|
|
|
} |
129
|
|
|
} |
130
|
|
|
|