| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  * Trait BaseQueryTrait|Firesphere\SolrSearch\Traits\BaseQueryTrait Trait to clean up the | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  * {@link \Firesphere\SolrSearch\Queries\BaseQuery} | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  |  * @package Firesphere\SolrSearch\Traits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  |  * @author Simon `Firesphere` Erkelens; Marco `Sheepy` Hermo | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  * @copyright Copyright (c) 2018 - now() Firesphere & Sheepy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | namespace Firesphere\SolrSearch\Traits; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  | use ReflectionException; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | use SilverStripe\Core\ClassInfo; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * Trait BaseQueryTrait Extraction from the BaseQuery class to keep things readable. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * This trait adds the support for adding the basic field/filter/term/facet options | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @package Firesphere\SolrSearch\Traits | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  | trait BaseQueryTrait | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |      * @var array Terms to search | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     protected $terms = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |      * @var array Fields to filter | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |     protected $filter = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      * @var array Fields to search | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |     protected $fields = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * Key => value pairs of facets to apply | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      *     'FacetTitle' => [1, 2, 3] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * ] | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |      * @var array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |     protected $facetFilter = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |      * @var array Fields to exclude | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |     protected $exclude = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @var array Classes to exclude through hierarchy | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     protected $excludedSubClasses = []; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * Each boosted query needs a separate addition! | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      * e.g. $this->addTerm('test', ['MyField', 'MyOtherField'], 3) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 |  |  |      * followed by | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |      * $this->addTerm('otherTest', ['Title'], 5); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |      * If you want a generic boost on all terms, use addTerm only once, but boost on each field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |      * The fields parameter is used to boost on | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * For generic boosting, use @addBoostedField($field, $boost), this will add the boost at Index time | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      * @param string $term Term to search for | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 |  |  |      * @param array $fields fields to boost on | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |      * @param int $boost Boost value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 |  |  |      * @param bool|float $fuzzy True or a value to the maximum amount of iterations | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 | 7 |  |     public function addTerm($term, $fields = [], $boost = 0, $fuzzy = null): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 | 7 |  |         $this->terms[] = [ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 | 7 |  |             'text'   => $term, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 | 7 |  |             'fields' => $fields, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 | 7 |  |             'boost'  => $boost, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 | 7 |  |             'fuzzy'  => $fuzzy, | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |         ]; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 7 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |      * Adds filters to filter on by value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |      * @param string $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |      * @param string|array $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 | 2 |  |     public function addFilter($field, $value): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 | 2 |  |         $field = str_replace('.', '_', $field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 | 2 |  |         $this->filter[$field] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |      * Add a field to be returned | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |      * @param string $field fieldname | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 | 2 |  |     public function addField($field): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 | 2 |  |         $field = str_replace('.', '_', $field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 | 2 |  |         $this->fields[] = $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |      * Exclude fields from the search action | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 |  |  |      * @param string $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |      * @param string|array $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 | 2 |  |     public function addExclude($field, $value): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 126 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 127 | 2 |  |         $field = str_replace('.', '_', $field); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 128 | 2 |  |         $this->exclude[$field] = $value; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 129 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 130 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 131 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 132 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 133 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 134 |  |  |      * Add the subclasses of the given class to exclude. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 135 |  |  |      * It's all merged in to a single array, for easier generation of the filter. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 136 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 137 |  |  |      * @param string $class | 
            
                                                                                                            
                            
            
                                    
            
            
                | 138 |  |  |      * @return $this | 
            
                                                                                                            
                            
            
                                    
            
            
                | 139 |  |  |      * @throws ReflectionException | 
            
                                                                                                            
                            
            
                                    
            
            
                | 140 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 141 |  |  |     public function addExcludedSubclasses($class): self | 
            
                                                                                                            
                            
            
                                    
            
            
                | 142 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 143 |  |  |         $subClasses = ClassInfo::subclassesFor($class, false); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 144 |  |  |         $excluded = $this->getExcludedSubClasses(); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 145 |  |  |         $this->excludedSubClasses = array_merge($excluded, $subClasses); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * Add faceting | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 |  |  |      * @param string $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |      * @param string|array $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 |  |  |      * @return $this | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 156 |  |  |      */ | 
            
                                                                        
                            
            
                                    
            
            
                | 157 | 2 |  |     public function addFacetFilter($field, $value): self | 
            
                                                                        
                            
            
                                    
            
            
                | 158 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 159 | 2 |  |         $this->facetFilter[$field][] = $value; | 
            
                                                                        
                            
            
                                    
            
            
                | 160 |  |  |  | 
            
                                                                        
                            
            
                                    
            
            
                | 161 | 2 |  |         return $this; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |     } | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 163 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 164 |  |  |  |