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
|
|
|
use Kaliop\EzFindSearchEngineBundle\API\Repository\Values\Content\Query\Criterion\EzFindText as EzFindTextCriterion; |
9
|
|
|
|
10
|
|
|
class EzFindText extends CriterionHandler |
11
|
|
|
{ |
12
|
|
|
/** |
13
|
|
|
* @inheritdoc |
14
|
|
|
*/ |
15
|
|
|
public function accept(Criterion $criterion) |
16
|
|
|
{ |
17
|
|
|
return $criterion instanceof EzFindTextCriterion; |
18
|
|
|
} |
19
|
|
|
|
20
|
|
|
/** |
21
|
|
|
* If the full-text search contain wildcard search, build correct wildcard query |
22
|
|
|
* with non-truncated words boosted. |
23
|
|
|
* |
24
|
|
|
* @inheritdoc |
25
|
|
|
*/ |
26
|
|
|
public function handle(CriteriaConverter $converter, Criterion $criterion) |
27
|
|
|
{ |
28
|
|
|
$value = trim($criterion->value); |
29
|
|
|
|
30
|
|
View Code Duplication |
if (preg_match('/^".+"$/', $value)) { |
|
|
|
|
31
|
|
|
// Quoted-string query: escape everything but the outher quotes |
32
|
|
|
$value = '"' . $this->escapeValue(substr($value, 1, -1)) . '"'; |
33
|
|
|
|
34
|
|
|
} else if (preg_match('/(^\*|\*$)/', $value)) { |
35
|
|
|
// Wildcard query: make the exact match stronger than the wildcard |
36
|
|
|
|
37
|
|
|
// @bug we do not support wildcard chars in the middle of phrases |
38
|
|
|
|
39
|
|
|
$value = $this->escapeValue($value); |
40
|
|
|
|
41
|
|
|
// Escape spaces |
42
|
|
|
$value = str_replace(' ', '\\ ', $value); |
43
|
|
|
|
44
|
|
|
// wildcard match: un-escape wildcard char |
45
|
|
|
$wildcard = str_replace('\\*', '*', $value); |
46
|
|
|
|
47
|
|
|
// Non-wildcard match |
48
|
|
|
$value = trim($value, '*'); |
49
|
|
|
$value = rtrim($value, '\\'); |
50
|
|
|
|
51
|
|
|
$value = $value . '^2 OR ' . $wildcard; |
52
|
|
|
|
53
|
|
|
} else { |
54
|
|
|
// plain query |
55
|
|
|
$value = $this->escapeValue($value); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
return $value; |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
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.