use Kaliop\eZMigrationBundle\API\MatcherInterface;
6
7
abstract class AbstractMatcher implements MatcherInterface
8
{
9
/** @var string[] $allowedConditions the keywords we allow to be used for matching on*/
10
protected $allowedConditions = array();
11
/** @var string $returns user-readable name of the type of object returned */
12
protected $returns;
13
/** @var int $maxConditions the maximum number of conditions we allow to match on for a single match request */
14
protected $maxConditions = 1;
15
16
protected function validateConditions(array $conditions)
17
{
18
if (count($conditions) == 0) {
19
throw new \Exception($this->returns . ' can not be matched because the matching conditions are empty');
20
}
21
22
if (count($conditions) > $this->maxConditions) {
23
20
throw new \Exception($this->returns . " can not be matched because multiple matching conditions are specified. Only {this->maxConditions} condition(s) are supported");
24
}
25
20
26
20
foreach ($conditions as $key => $value) {
27
if (!in_array((string)$key, $this->allowedConditions)) {
28
3
throw new \Exception($this->returns . " can not be matched because matching condition '$key' is not supported. Supported conditions are: " .
29
implode(', ', $this->allowedConditions));
30
3
}
31
}
32
}
33
34
3
public function matchOne(array $conditions)
35
{
36
$results = $this->match($conditions);
37
$count = count($results);
38
3
if ($count !== 1) {
39
3
throw new \Exception("Found $count " . $this->returns . " when expected exactly only one to match the conditions");
40
}
41
return reset($results);
42
}
43
3
44
3
abstract public function match(array $conditions);