1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\EzFindSearchEngineBundle\Core\Persistence\eZFind\Content\Search\Common\Gateway; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; |
6
|
|
|
use Kaliop\EzFindSearchEngineBundle\Core\Base\Exceptions\NotImplementedException; |
7
|
|
|
|
8
|
|
|
class CriteriaConverter extends Converter |
9
|
|
|
{ |
10
|
|
|
protected $queryBuilderClass = '\ezfeZPSolrQueryBuilder'; |
11
|
|
|
|
12
|
|
|
public function __construct($handlers = array(), $queryBuilderClass = null) |
13
|
|
|
{ |
14
|
|
|
parent::__construct($handlers); |
15
|
|
|
if ($queryBuilderClass) { |
16
|
|
|
$this->queryBuilderClass = $queryBuilderClass; |
17
|
|
|
} |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* @param Criterion $criterion |
22
|
|
|
* @return bool |
23
|
|
|
*/ |
24
|
|
|
public function canHandle(Criterion $criterion) |
25
|
|
|
{ |
26
|
|
|
/** @var CriterionHandler $handler */ |
27
|
|
|
foreach ($this->handlers as $handler) { |
28
|
|
|
if ($handler->accept($criterion)) { |
29
|
|
|
return true; |
30
|
|
|
} |
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
return false; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @param Criterion $criterion |
38
|
|
|
* @return mixed |
39
|
|
|
* @throws NotImplementedException |
40
|
|
|
*/ |
41
|
|
View Code Duplication |
public function handle(Criterion $criterion) |
|
|
|
|
42
|
|
|
{ |
43
|
|
|
/** @var CriterionHandler $handler */ |
44
|
|
|
foreach ($this->handlers as $handler) { |
45
|
|
|
if ($handler->accept($criterion)) { |
46
|
|
|
return $handler->handle($this, $criterion); |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
throw new NotImplementedException( |
51
|
|
|
'No handler available for: ' . get_class($criterion) . ' with operator ' . $criterion->operator |
52
|
|
|
); |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Given a Solr filter in array form (as passed to ezfeZPSolrQueryBuilder), return its string representtaion |
57
|
|
|
* @param array $filter |
58
|
|
|
* @return string |
59
|
|
|
* @throws NotImplementedException |
60
|
|
|
*/ |
61
|
|
|
public function generateQueryString($filter) |
62
|
|
|
{ |
63
|
|
|
// build the Solr query string via ezfind - we have to resort to |
64
|
|
|
// a hackish way to access a protected method in ezfeZPSolrQueryBuilder |
65
|
|
|
$queryBuilderClass = $this->queryBuilderClass; |
66
|
|
|
/** @var \ezfeZPSolrQueryBuilder $queryBuilder */ |
67
|
|
|
$queryBuilder = new $queryBuilderClass(null); |
68
|
|
|
$filterCreator = \Closure::bind(function($filter){return $this->getParamFilterQuery(array('Filter' => $filter));}, $queryBuilder, $queryBuilder); |
|
|
|
|
69
|
|
|
return $filterCreator($filter); |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
} |
73
|
|
|
|
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.