| 1 |  |  | <?php | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  | namespace BestServedCold\LaravelZendSearch\Lucene\Store; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use BestServedCold\LaravelZendSearch\Lucene\Index; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use ZendSearch\Lucene\Document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use ZendSearch\Lucene\Document\Field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  * Class Insert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * @package BestServedCold\LaravelZendSearch\Lucene\Store | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  | class Insert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  | { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |      * @var Document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |     private $document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |      * @var float | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |     private $defaultBoost = 1.0; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  |      * Insert constructor. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |      * @param Document $document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 | 22 |  |     public function __construct(Document $document) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 | 22 |  |         $this->document = $document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 | 22 |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |      * Insert | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      * @param  integer $id | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 |  |  |      * @param  Index          $index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |      * @param  array          $fields      fields that are indexed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |      * @param  boolean|string $uid         unique identifier, if required | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |      * @param  array          $boostFields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 | 1 |  |     public function insert(Index $index, $id, array $fields, $uid = false, array $boostFields = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 | 1 |  |         $this->document->addField($this->field('xref_id', $id)); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 | 1 |  |         $this->document = $this->addUid($this->document, $uid); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 | 1 |  |         $this->document = $this->addFields($this->document, $fields, $boostFields); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 | 1 |  |         return $index->get()->addDocument($this->document); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 |  |  |      * @param  string|integer $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      * @param  string         $value | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |      * @param  string         $type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * @return Field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 | 1 |  |     private function field($field, $value, $type = 'keyword', $boost = null) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |         $field = Field::$type($field, strtolower(strip_tags($value))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 | 1 |  |         $field->boost = $boost ?: $this->defaultBoost; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |         return $field; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * @param  Document       $document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * @param  boolean|string $uid | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return Document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 1 |  |     private function addUid(Document $document, $uid = false) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 1 |  |         if ($uid) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 1 |  |             $document->addField($this->field('uid', $uid)); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         return $document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * @param  Document $document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * @param  array    $fields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @param  array    $boostFields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      * @return Document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 | 1 |  |     private function addFields(Document $document, array $fields, array $boostFields = []) | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 1 |  |         foreach ($fields as $field => $text) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 | 1 |  |             $document->addField($this->field($field, $text, 'text', $this->boost($field, $boostFields))); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 | 1 |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 | 1 |  |         return $document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |     } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * @param  $field | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @param  array      $boostFields | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      * @return mixed|null | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 100 |  |  |      */ | 
            
                                                        
            
                                    
            
            
                | 101 | 2 |  |     private function boost($field, array $boostFields = []) | 
            
                                                        
            
                                    
            
            
                | 102 |  |  |     { | 
            
                                                        
            
                                    
            
            
                | 103 | 2 |  |         if (empty($boostFields)) { | 
            
                                                        
            
                                    
            
            
                | 104 | 1 |  |             return null; | 
            
                                                        
            
                                    
            
            
                | 105 |  |  |         } | 
            
                                                        
            
                                    
            
            
                | 106 | 1 |  |         return array_key_exists($field, $boostFields) ? $boostFields[$field] : null; | 
            
                                                        
            
                                    
            
            
                | 107 |  |  |     } | 
            
                                                        
            
                                    
            
            
                | 108 |  |  | } | 
            
                                                        
            
                                    
            
            
                | 109 |  |  |  | 
            
                        
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.