1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Mathielen\ImportEngine\Validation; |
4
|
|
|
|
5
|
|
|
use Ddeboer\DataImport\Workflow; |
6
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
7
|
|
|
use Symfony\Component\Validator\Constraint; |
8
|
|
|
use Mathielen\DataImport\Filter\ValidatorFilter; |
9
|
|
|
|
10
|
|
|
class ValidatorValidation implements ValidationInterface |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @var ValidatorInterface |
14
|
|
|
*/ |
15
|
|
|
private $validator; |
16
|
|
|
|
17
|
|
|
/** |
18
|
|
|
* @var ValidatorFilter |
19
|
|
|
*/ |
20
|
|
|
private $sourceValidatorFilter; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var ValidatorFilter |
24
|
|
|
*/ |
25
|
|
|
private $targetValidatorFilter; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* @return ValidatorValidation |
29
|
|
|
*/ |
30
|
4 |
|
public static function build(ValidatorInterface $validator) |
31
|
|
|
{ |
32
|
4 |
|
return new self($validator); |
33
|
|
|
} |
34
|
|
|
|
35
|
12 |
|
public function __construct(ValidatorInterface $validator) |
36
|
|
|
{ |
37
|
12 |
|
$this->validator = $validator; |
38
|
12 |
|
} |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @return ValidatorValidation |
42
|
|
|
*/ |
43
|
3 |
View Code Duplication |
public function addSourceConstraint($field, Constraint $constraint) |
|
|
|
|
44
|
|
|
{ |
45
|
3 |
|
if (!$this->sourceValidatorFilter) { |
46
|
3 |
|
$this->setSourceValidatorFilter(new ValidatorFilter($this->validator)); |
47
|
3 |
|
$this->sourceValidatorFilter->setAllowExtraFields(true); |
48
|
|
|
} |
49
|
|
|
|
50
|
3 |
|
$this->sourceValidatorFilter->add($field, $constraint); |
51
|
|
|
|
52
|
3 |
|
return $this; |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @return ValidatorValidation |
57
|
|
|
*/ |
58
|
1 |
View Code Duplication |
public function addTargetConstraint($field, Constraint $constraint) |
|
|
|
|
59
|
|
|
{ |
60
|
1 |
|
if (!$this->targetValidatorFilter) { |
61
|
1 |
|
$this->setTargetValidatorFilter(new ValidatorFilter($this->validator)); |
62
|
1 |
|
$this->targetValidatorFilter->setAllowExtraFields(true); |
63
|
|
|
} |
64
|
|
|
|
65
|
1 |
|
$this->targetValidatorFilter->add($field, $constraint); |
66
|
|
|
|
67
|
1 |
|
return $this; |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return ValidatorValidation |
72
|
|
|
*/ |
73
|
4 |
|
public function setSourceValidatorFilter(ValidatorFilter $validatorFilter) |
74
|
|
|
{ |
75
|
4 |
|
$this->sourceValidatorFilter = $validatorFilter; |
76
|
|
|
|
77
|
4 |
|
return $this; |
78
|
|
|
} |
79
|
|
|
|
80
|
|
|
/** |
81
|
|
|
* @return ValidatorValidation |
82
|
|
|
*/ |
83
|
3 |
|
public function setTargetValidatorFilter(ValidatorFilter $validatorFilter) |
84
|
|
|
{ |
85
|
3 |
|
$this->targetValidatorFilter = $validatorFilter; |
86
|
|
|
|
87
|
3 |
|
return $this; |
88
|
|
|
} |
89
|
|
|
|
90
|
|
|
/** |
91
|
|
|
* @return ValidatorValidation |
92
|
|
|
*/ |
93
|
5 |
|
public function apply(Workflow $workflow) |
94
|
|
|
{ |
95
|
5 |
|
if ($this->sourceValidatorFilter) { |
96
|
2 |
|
$this->sourceValidatorFilter->reset(); |
97
|
2 |
|
$workflow->addFilter($this->sourceValidatorFilter); |
98
|
|
|
} |
99
|
5 |
|
if ($this->targetValidatorFilter) { |
100
|
1 |
|
$this->targetValidatorFilter->reset(); |
101
|
1 |
|
$workflow->addFilterAfterConversion($this->targetValidatorFilter); |
102
|
|
|
} |
103
|
|
|
|
104
|
5 |
|
return $this; |
105
|
|
|
} |
106
|
|
|
|
107
|
|
|
/** |
108
|
|
|
* @return array |
109
|
|
|
*/ |
110
|
3 |
|
public function getViolations() |
111
|
|
|
{ |
112
|
3 |
|
$violations = array('source' => array(), 'target' => array()); |
113
|
3 |
|
if ($this->sourceValidatorFilter) { |
114
|
2 |
|
$violations['source'] = $this->sourceValidatorFilter->getViolations(); |
115
|
|
|
} |
116
|
3 |
|
if ($this->targetValidatorFilter) { |
117
|
1 |
|
$violations['target'] = $this->targetValidatorFilter->getViolations(); |
118
|
|
|
} |
119
|
|
|
|
120
|
3 |
|
return $violations; |
121
|
|
|
} |
122
|
|
|
} |
123
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.