1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Kaliop\EzFindSearchEngineBundle\Core\Persistence\eZFind\Content\Search\Common\Gateway\CriterionHandler; |
4
|
|
|
|
5
|
|
|
use eZ\Publish\API\Repository\Values\Content\Query\Criterion; |
6
|
|
|
use Kaliop\EzFindSearchEngineBundle\Core\Persistence\eZFind\Content\Search\Common\Gateway\CriteriaConverter; |
7
|
|
|
use Kaliop\EzFindSearchEngineBundle\Core\Persistence\eZFind\Content\Search\Common\Gateway\CriterionHandler; |
8
|
|
|
|
9
|
|
|
class Field extends FieldBase |
10
|
|
|
{ |
11
|
|
|
public function accept( Criterion $criterion ) |
12
|
|
|
{ |
13
|
|
|
return $criterion instanceof Criterion\Field; |
14
|
|
|
} |
15
|
|
|
|
16
|
|
|
/// @todo should we apply specific conversions based on field type? |
17
|
|
|
public function handle( CriteriaConverter $converter, Criterion $criterion ) |
18
|
|
|
{ |
19
|
|
|
$field = $this->determineFieldName($criterion->target); |
20
|
|
|
|
21
|
|
|
switch ( $criterion->operator ) |
22
|
|
|
{ |
23
|
|
View Code Duplication |
case Criterion\Operator::IN: |
|
|
|
|
24
|
|
|
$values = array_map(array($this, 'escapeValue'), $criterion->value); |
25
|
|
|
return $field . ':(' . implode(' ', $values) . ')'; |
26
|
|
|
|
27
|
|
|
case Criterion\Operator::GT: |
28
|
|
|
return $field . ':{' . $this->escapeValue($criterion->value) . ' TO *]'; |
29
|
|
|
|
30
|
|
|
case Criterion\Operator::GTE: |
31
|
|
|
return $field . ':[' . $this->escapeValue($criterion->value) . ' TO *]'; |
32
|
|
|
|
33
|
|
|
case Criterion\Operator::LT: |
34
|
|
|
return $field . ':[* TO ' . $this->escapeValue($criterion->value) . '}'; |
35
|
|
|
|
36
|
|
|
case Criterion\Operator::LTE: |
37
|
|
|
return $field . ':[* TO ' . $this->escapeValue($criterion->value) . ']'; |
38
|
|
|
|
39
|
|
|
case Criterion\Operator::LIKE: |
40
|
|
|
return $field . ':' . $this->like2regexp($this->escapeValue($criterion->value)); |
41
|
|
|
|
42
|
|
View Code Duplication |
case Criterion\Operator::BETWEEN: |
|
|
|
|
43
|
|
|
return $field . ':[' . $this->escapeValue($criterion->value[0]) . ' TO ' . $this->escapeValue($criterion->value[1]) . ']'; |
44
|
|
|
|
45
|
|
|
case Criterion\Operator::CONTAINS: |
46
|
|
|
return $field . ':*' . $this->escapeValue($criterion->value) . '*'; |
47
|
|
|
} |
48
|
|
|
} |
49
|
|
|
|
50
|
|
|
/** |
51
|
|
|
* Transform db-style LIKE in regexp |
52
|
|
|
* - replace %, _ chars |
53
|
|
|
* - escape regexp chars |
54
|
|
|
*/ |
55
|
|
|
protected function like2regexp($string, $delimiter='/') |
56
|
|
|
{ |
57
|
|
|
$string = preg_quote($string, $delimiter); |
58
|
|
|
$string = str_replace(array('%', '_'), array('.*', '.'), $string); |
59
|
|
|
return $delimiter . $string . $delimiter; |
60
|
|
|
} |
61
|
|
|
} |
62
|
|
|
|
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.