1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\LaravelZendSearch\Lucene; |
4
|
|
|
|
5
|
|
|
use BestServedCold\LaravelZendSearch\Lucene\Filter\Factory; |
6
|
|
|
use ZendSearch\Lucene\Analysis\Analyzer\Analyzer; |
7
|
|
|
use ZendSearch\Lucene\Analysis\Analyzer\Common\AbstractCommon; |
8
|
|
|
use ZendSearch\Lucene\Analysis\Analyzer\Common\TextNum\CaseInsensitive; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Class Filter |
12
|
|
|
* |
13
|
|
|
* @package BestServedCold\LaravelZendSearch\Lucene |
14
|
|
|
*/ |
15
|
|
|
class Filter |
16
|
|
|
{ |
17
|
|
|
/** |
18
|
|
|
* @var null|AbstractCommon |
19
|
|
|
*/ |
20
|
|
|
private static $analyzer = null; |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @var array |
24
|
|
|
*/ |
25
|
|
|
private static $filters = []; |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Filter constructor. |
29
|
|
|
* |
30
|
|
|
* @param CaseInsensitive $caseInsensitive |
31
|
|
|
*/ |
32
|
29 |
|
public function __construct(CaseInsensitive $caseInsensitive) |
33
|
|
|
{ |
34
|
29 |
|
self::$analyzer = self::$analyzer ?: $caseInsensitive; |
35
|
29 |
|
} |
36
|
|
|
|
37
|
|
|
/** |
38
|
|
|
* @param AbstractCommon $analyzer |
39
|
|
|
*/ |
40
|
1 |
|
public static function setAnalyzer(AbstractCommon $analyzer) |
41
|
|
|
{ |
42
|
1 |
|
self::$analyzer = $analyzer; |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return null|AbstractCommon|CaseInsensitive |
47
|
|
|
*/ |
48
|
2 |
|
public static function getAnalyzer() |
49
|
|
|
{ |
50
|
2 |
|
return self::$analyzer; |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @param string $filter |
55
|
|
|
* @param mixed $option |
56
|
|
|
*/ |
57
|
22 |
|
public static function addFilter($filter, $option) |
58
|
|
|
{ |
59
|
22 |
|
if (self::$analyzer === null) { |
60
|
1 |
|
self::$analyzer = new CaseInsensitive; |
61
|
1 |
|
} |
62
|
|
|
|
63
|
22 |
|
if (!in_array($filter, self::$filters)) { |
64
|
1 |
|
self::$analyzer->addFilter(Factory::getFilter($filter, $option)); |
65
|
1 |
|
self::$filters[] = $filter; |
66
|
1 |
|
} |
67
|
|
|
|
68
|
22 |
|
} |
69
|
|
|
|
70
|
|
|
/** |
71
|
|
|
* @return void |
72
|
|
|
*/ |
73
|
7 |
|
public function setFilters() |
74
|
|
|
{ |
75
|
7 |
|
if (self::$analyzer instanceof AbstractCommon) { |
76
|
7 |
|
Analyzer::setDefault(self::$analyzer); |
77
|
7 |
|
} |
78
|
|
|
|
79
|
7 |
|
} |
80
|
|
|
} |
81
|
|
|
|