1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace BestServedCold\LaravelZendSearch\Laravel; |
4
|
|
|
|
5
|
|
|
use BestServedCold\LaravelZendSearch\Lucene\Index as LuceneIndex; |
6
|
|
|
use Illuminate\Config\Repository; |
7
|
|
|
use Illuminate\Filesystem\Filesystem; |
8
|
|
|
|
9
|
|
|
/** |
10
|
|
|
* Class Index |
11
|
|
|
* @package BestServedCold\LaravelZendSearch\Laravel |
12
|
|
|
*/ |
13
|
|
|
class Index extends LuceneIndex |
14
|
|
|
{ |
15
|
|
|
/** |
16
|
|
|
* Index constructor. |
17
|
|
|
* @param Filter $filter |
18
|
|
|
* @param Repository $config |
19
|
|
|
* @param Filesystem $filesystem |
20
|
|
|
*/ |
21
|
21 |
|
public function __construct(Filter $filter, Repository $config, Filesystem $filesystem) |
22
|
|
|
{ |
23
|
21 |
|
$this->setPath($config->get('search.index.path')); |
24
|
|
|
|
25
|
21 |
|
if ($config->get('search.filters')) { |
26
|
21 |
|
$this->addFilters($config->get('search.filters'), $filesystem); |
27
|
21 |
|
} |
28
|
|
|
|
29
|
21 |
|
parent::__construct($filter); |
30
|
21 |
|
} |
31
|
|
|
|
32
|
|
|
/** |
33
|
|
|
* @param array $filters |
34
|
|
|
* @param Filesystem $filesystem |
35
|
|
|
*/ |
36
|
21 |
|
private function addFilters(array $filters = [], Filesystem $filesystem) |
37
|
|
|
{ |
38
|
21 |
|
foreach ($filters as $filter => $switch) { |
39
|
21 |
|
$switch === false ? null : $this->handleFilter($switch, $filter, $filesystem); |
40
|
21 |
|
} |
41
|
21 |
|
} |
42
|
|
|
|
43
|
|
|
/** |
44
|
|
|
* @param $switch |
45
|
|
|
* @param $filter |
46
|
|
|
* @param Filesystem $filesystem |
47
|
|
|
*/ |
48
|
21 |
|
private function handleFilter($switch, $filter, Filesystem $filesystem) |
49
|
|
|
{ |
50
|
21 |
|
$filter === 'StopWords' && !is_array($switch) |
51
|
21 |
|
? $this->stopWordFilter($switch, $filesystem) |
52
|
21 |
|
: $this->filter($filter, $switch); |
53
|
21 |
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* @param $filter |
57
|
|
|
* @param $switch |
58
|
|
|
*/ |
59
|
21 |
|
private function filter($filter, $switch) |
60
|
|
|
{ |
61
|
21 |
|
Filter::addFilter($filter, $switch === true ? null : [$switch]); |
62
|
21 |
|
} |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @param $switch |
66
|
|
|
* @param Filesystem $filesystem |
67
|
|
|
*/ |
68
|
21 |
|
private function stopWordFilter($switch, Filesystem $filesystem) |
69
|
|
|
{ |
70
|
21 |
|
Filter::addStopWordFilter($switch === true ? 'en' : $switch, $filesystem); |
71
|
21 |
|
} |
72
|
|
|
} |
73
|
|
|
|