1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\LaravelZendSearch\Lucene\Filter; |
4
|
|
|
|
5
|
|
|
use Doctrine\Instantiator\Exception\InvalidArgumentException; |
6
|
|
|
use ZendSearch\Lucene\Analysis\TokenFilter\LowerCase; |
7
|
|
|
use ZendSearch\Lucene\Analysis\TokenFilter\LowerCaseUtf8; |
8
|
|
|
use ZendSearch\Lucene\Analysis\TokenFilter\ShortWords; |
9
|
|
|
use ZendSearch\Lucene\Analysis\TokenFilter\StopWords; |
10
|
|
|
use ZendSearch\Lucene\Analysis\TokenFilter\TokenFilterInterface; |
11
|
|
|
|
12
|
|
|
/** |
13
|
|
|
* Class ShortWord |
14
|
|
|
* @package BestServedCold\LaravelZendSearch\Lucene\Filter |
15
|
|
|
*/ |
16
|
|
|
class Factory |
17
|
|
|
{ |
18
|
|
|
/** |
19
|
|
|
* @param string $filter |
20
|
|
|
* @param mixed $option |
21
|
|
|
* @return TokenFilterInterface |
22
|
|
|
* @throws \InvalidArgumentException |
23
|
|
|
*/ |
24
|
2 |
|
public static function getFilter($filter, $option) |
25
|
|
|
{ |
26
|
2 |
|
$supportedFilters = self::getSupportedFilters(); |
27
|
2 |
|
self::supportedException($filter, $supportedFilters); |
28
|
1 |
|
return new $supportedFilters[$filter]($option); |
29
|
|
|
} |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param string $filter |
33
|
|
|
* @param array $supportedFilters |
34
|
|
|
* @throws \InvalidArgumentException |
35
|
|
|
*/ |
36
|
2 |
|
private static function supportedException($filter, array $supportedFilters) |
37
|
|
|
{ |
38
|
2 |
|
if (!array_key_exists($filter, $supportedFilters)) { |
39
|
1 |
|
throw new \InvalidArgumentException( |
40
|
1 |
|
'Filter ['.$filter.'] is not supported by ['.self::class.']' |
41
|
1 |
|
); |
42
|
|
|
} |
43
|
1 |
|
} |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* @return array |
47
|
|
|
*/ |
48
|
2 |
|
private static function getSupportedFilters() |
49
|
|
|
{ |
50
|
|
|
return [ |
51
|
2 |
|
'LowerCase' => LowerCase::class, |
52
|
2 |
|
'LowerCaseUtf8' => LowerCaseUtf8::class, |
53
|
2 |
|
'ShortWords' => ShortWords::class, |
54
|
|
|
'StopWords' => StopWords::class |
55
|
2 |
|
]; |
56
|
|
|
} |
57
|
|
|
} |
58
|
|
|
|