| 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 Repository $config | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 | 21 |  |     public function __construct(Filter $filter, Repository $config, Filesystem $filesystem) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 | 21 |  |         $this->setPath($config->get('search.index.path')); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 | 21 |  |         $this->addFilters($config->get('search.filters'), $filesystem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 | 21 |  |         parent::__construct($filter); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 | 21 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * @param array $filters | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 | 21 |  |     private function addFilters(array $filters = [], Filesystem $filesystem) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 | 21 |  |         foreach ($filters as $filter => $switch) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 21 |  |             $switch === false ? null : $this->handleFilter($switch, $filter, $filesystem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 21 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 | 21 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * @param $switch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param $filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param Filesystem $filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 | 21 |  |     private function handleFilter($switch, $filter, Filesystem $filesystem) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 21 |  |         $filter === 'StopWords' && !is_array($switch) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 21 |  |             ? $this->stopWordFilter($switch, $filesystem) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 21 |  |             : $this->filter($filter, $switch); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 21 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |      * @param $filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @param $switch | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 53 | 21 |  |     private function filter($filter, $switch) | 
            
                                                                        
                            
            
                                    
            
            
                | 54 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 55 | 21 |  |         Filter::addFilter($filter, $switch === true ? [] : [$switch]); | 
            
                                                                        
                            
            
                                    
            
            
                | 56 | 21 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * @param $switch | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @param Filesystem $filesystem | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 21 |  |     private function stopWordFilter($switch, Filesystem $filesystem) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 21 |  |         Filter::addStopWordFilter($switch === true ? 'en' : $switch, $filesystem); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 | 21 |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 66 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 67 |  |  |  |