|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Kaliop\eZMigrationBundle\Core\Matcher; |
|
4
|
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion\Operator; |
|
6
|
|
|
use Symfony\Component\Validator\Validator\ValidatorInterface; |
|
7
|
|
|
use Kaliop\eZMigrationBundle\API\ReferenceResolverInterface; |
|
8
|
|
|
|
|
9
|
|
|
/** |
|
10
|
|
|
* We abuse a bit the 'matcher' framework to set up a 'constraint' matcher which is used to tell whether a reference |
|
11
|
|
|
* matches a given condition |
|
12
|
|
|
*/ |
|
13
|
|
|
class ReferenceMatcher extends AbstractMatcher |
|
14
|
|
|
{ |
|
15
|
|
|
//const MATCH_REFERENCE = 'reference'; |
|
|
|
|
|
|
16
|
|
|
|
|
17
|
|
|
protected $allowedConditions = array( |
|
18
|
|
|
self::MATCH_AND, self::MATCH_OR, self::MATCH_NOT, |
|
19
|
|
|
//self::MATCH_REFERENCE |
|
20
|
|
|
); |
|
21
|
|
|
|
|
22
|
|
|
// since this is used for recursive calls as well, we have to unlock it for now |
|
23
|
|
|
/// @todo allow this to be set in a more flexible way |
|
24
|
|
|
protected $maxConditions = 0; |
|
25
|
|
|
|
|
26
|
|
|
protected $validator; |
|
27
|
|
|
/** @var ReferenceResolverInterface $referenceResolver */ |
|
28
|
|
|
protected $referenceResolver; |
|
29
|
|
|
|
|
30
|
|
|
/// @todo add more operators |
|
31
|
|
|
static protected $operatorsMap = array( |
|
32
|
|
|
'eq' => '\Symfony\Component\Validator\Constraints\EqualTo', |
|
33
|
|
|
'gt' => '\Symfony\Component\Validator\Constraints\GreaterThan', |
|
34
|
|
|
'gte' => '\Symfony\Component\Validator\Constraints\GreaterThanOrEqual', |
|
35
|
|
|
'lt' => '\Symfony\Component\Validator\Constraints\LessThan', |
|
36
|
|
|
'lte' => '\Symfony\Component\Validator\Constraints\LessThanOrEqual', |
|
37
|
|
|
'ne' => '\Symfony\Component\Validator\Constraints\NotEqualTo', |
|
38
|
|
|
|
|
39
|
|
|
'count' => '\Symfony\Component\Validator\Constraints\Count', |
|
40
|
|
|
'length' => '\Symfony\Component\Validator\Constraints\Length', |
|
41
|
|
|
'regex' => '\Symfony\Component\Validator\Constraints\Regex', |
|
42
|
|
|
//'in' => Operator::IN, |
|
|
|
|
|
|
43
|
|
|
//'between' => Operator::BETWEEN, => use count/length with min & max sub-members |
|
44
|
|
|
//'like' => Operator::LIKE, => use regex |
|
|
|
|
|
|
45
|
|
|
//'contains' => Operator::CONTAINS, |
|
|
|
|
|
|
46
|
|
|
Operator::EQ => '\Symfony\Component\Validator\Constraints\EqualTo', |
|
47
|
|
|
Operator::GT => '\Symfony\Component\Validator\Constraints\GreaterThan', |
|
48
|
|
|
Operator::GTE => '\Symfony\Component\Validator\Constraints\GreaterThanOrEqual', |
|
49
|
|
|
Operator::LT => '\Symfony\Component\Validator\Constraints\LessThan', |
|
50
|
|
|
Operator::LTE => '\Symfony\Component\Validator\Constraints\LessThanOrEqual', |
|
51
|
|
|
'!=' => '\Symfony\Component\Validator\Constraints\NotEqualTo', |
|
52
|
|
|
'<>' => '\Symfony\Component\Validator\Constraints\NotEqualTo', |
|
53
|
|
|
); |
|
54
|
|
|
|
|
55
|
|
|
public function __construct(ReferenceResolverInterface $referenceResolver, ValidatorInterface $validator) |
|
56
|
|
|
{ |
|
57
|
|
|
$this->referenceResolver = $referenceResolver; |
|
58
|
|
|
$this->validator = $validator; |
|
59
|
|
|
} |
|
60
|
|
|
|
|
61
|
|
|
protected function validateConditions(array $conditions) |
|
62
|
|
|
{ |
|
63
|
|
|
foreach ($conditions as $key => $val) { |
|
64
|
|
|
if ($this->referenceResolver->isReference($key)) { |
|
65
|
|
|
return true; |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return parent::validateConditions($conditions); |
|
70
|
|
|
} |
|
71
|
|
|
|
|
72
|
|
|
/** |
|
73
|
|
|
* @param array $conditions key: condition, value: int / string / int[] / string[] |
|
74
|
|
|
* @return array 1 element with the value true/false |
|
75
|
|
|
*/ |
|
76
|
|
|
public function match(array $conditions) |
|
77
|
|
|
{ |
|
78
|
|
|
$this->validateConditions($conditions); |
|
79
|
|
|
|
|
80
|
|
|
foreach ($conditions as $key => $values) { |
|
81
|
|
|
|
|
82
|
|
|
switch ($key) { |
|
83
|
|
View Code Duplication |
case self::MATCH_AND: |
|
|
|
|
|
|
84
|
|
|
foreach($values as $subCriterion) { |
|
85
|
|
|
$value = $this->match($subCriterion); |
|
86
|
|
|
if (!reset($value)) { |
|
87
|
|
|
return $value; |
|
88
|
|
|
} |
|
89
|
|
|
} |
|
90
|
|
|
return array(true); |
|
91
|
|
|
|
|
92
|
|
View Code Duplication |
case self::MATCH_OR: |
|
|
|
|
|
|
93
|
|
|
foreach($values as $subCriterion) { |
|
94
|
|
|
$value = $this->match($subCriterion); |
|
95
|
|
|
if (reset($value)) { |
|
96
|
|
|
return $value; |
|
97
|
|
|
} |
|
98
|
|
|
} |
|
99
|
|
|
return array(false); |
|
100
|
|
|
|
|
101
|
|
|
case self::MATCH_NOT: |
|
102
|
|
|
return array(!reset($this->match($values))); |
|
|
|
|
|
|
103
|
|
|
|
|
104
|
|
|
default: |
|
105
|
|
|
// we assume that all are refs because of the call to validate() |
|
106
|
|
|
$currentValue = $this->referenceResolver->resolveReference($key); |
|
107
|
|
|
$targetValue = reset($values); |
|
108
|
|
|
$constraint = key($values); |
|
109
|
|
|
$errorList = $this->validator->validate($currentValue, $this->getConstraint($constraint, $targetValue)); |
|
110
|
|
|
if (0 === count($errorList)) { |
|
111
|
|
|
return array(true); |
|
112
|
|
|
} |
|
113
|
|
|
return array(false); |
|
114
|
|
|
} |
|
115
|
|
|
} |
|
116
|
|
|
} |
|
117
|
|
|
|
|
118
|
|
|
/** |
|
119
|
|
|
* @param string $constraint |
|
120
|
|
|
* @param $targetValue |
|
121
|
|
|
* @return mixed |
|
122
|
|
|
* @throws \Exception for unsupported keys |
|
123
|
|
|
*/ |
|
124
|
|
|
protected function getConstraint($constraint, $targetValue) |
|
125
|
|
|
{ |
|
126
|
|
|
/*if (!is_array($values)) { |
|
|
|
|
|
|
127
|
|
|
$values = array($values); |
|
128
|
|
|
}*/ |
|
129
|
|
|
|
|
130
|
|
|
if (!isset(self::$operatorsMap[$constraint])) { |
|
131
|
|
|
throw new \Exception("Matching condition '$constraint' is not supported. Supported conditions are: " . |
|
132
|
|
|
implode(', ', array_keys(self::$operatorsMap)) |
|
133
|
|
|
); |
|
134
|
|
|
} |
|
135
|
|
|
|
|
136
|
|
|
$class = self::$operatorsMap[$constraint]; |
|
137
|
|
|
return new $class($targetValue); |
|
138
|
|
|
} |
|
139
|
|
|
} |
|
140
|
|
|
|
Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.
The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.
This check looks for comments that seem to be mostly valid code and reports them.