| 1 |  |  | <?php | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 2 |  |  | namespace Triadev\Leopard; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 3 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 4 |  |  | use Carbon\Carbon; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 5 |  |  | use Illuminate\Database\Eloquent\Model; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 6 |  |  | use Illuminate\Support\Collection; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 7 |  |  | use Triadev\Leopard\Contract\ElasticsearchManagerContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 8 |  |  | use Triadev\Leopard\Contract\Repository\ElasticsearchRepositoryContract; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 9 |  |  | use Triadev\Leopard\Facade\Leopard; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 10 |  |  |  | 
            
                                                                                                            
                            
            
                                    
            
            
                | 11 |  |  | /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 12 |  |  |  * Trait Searchable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 13 |  |  |  * @package Triadev\Leopard | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 14 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 15 |  |  |  * @property string $documentIndex | 
            
                                                                                                            
                            
            
                                    
            
            
                | 16 |  |  |  * @property string $documentType | 
            
                                                                                                            
                            
            
                                    
            
            
                | 17 |  |  |  * @property bool $syncDocument | 
            
                                                                                                            
                            
            
                                    
            
            
                | 18 |  |  |  * @property array $searchable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 19 |  |  |  * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 20 |  |  |  * @method static ElasticsearchManagerContract search() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 21 |  |  |  * @methdo static ElasticsearchManagerContract suggest() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 22 |  |  |  * @method array buildDocument() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 23 |  |  |  * @method array toArray() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 24 |  |  |  * @method string getTable() | 
            
                                                                                                            
                            
            
                                    
            
            
                | 25 |  |  |  */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 26 |  |  | trait Searchable | 
            
                                                                                                            
                            
            
                                    
            
            
                | 27 |  |  | { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 28 |  |  |     /** @var bool */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 29 |  |  |     public $isDocument = false; | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 30 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 31 |  |  |     /** @var float|null */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 32 |  |  |     public $documentScore; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 33 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 34 |  |  |     /** @var int|null */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 35 |  |  |     public $documentVersion; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 36 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 37 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 38 |  |  |      * Searchable boot model. | 
            
                                                                                                            
                            
            
                                    
            
            
                | 39 |  |  |      */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 40 | 21 |  |     public static function bootSearchable() | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 41 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 42 |  |  |         static::saved(function (Model $model) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 43 |  |  |             /** @var Model|Searchable $model */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 44 | 1 |  |             if ($model->shouldSyncDocument()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 45 | 1 |  |                 $model->repository()->save(/** @scrutinizer ignore-type */$model); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 46 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 47 | 21 |  |         }); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 48 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 49 |  |  |         static::deleted(function (Model $model) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 50 |  |  |             /** @var Model|Searchable $model */ | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 51 |  |  |             if ($model->shouldSyncDocument()) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 52 |  |  |                 $model->repository()->delete(/** @scrutinizer ignore-type */$model); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 53 |  |  |             } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 54 | 21 |  |         }); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 55 | 21 |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 56 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 57 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 58 |  |  |      * Repository | 
            
                                                                                                            
                            
            
                                    
            
            
                | 59 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 60 |  |  |      * @return ElasticsearchRepositoryContract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 61 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 62 | 1 |  |     public function repository() : ElasticsearchRepositoryContract | 
            
                                                                                                            
                            
            
                                    
            
            
                | 63 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 64 | 1 |  |         return app(ElasticsearchRepositoryContract::class); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 65 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 66 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 67 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 68 |  |  |      * Get document index | 
            
                                                                                                            
                            
            
                                    
            
            
                | 69 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 70 |  |  |      * @return string|null | 
            
                                                                                                            
                            
            
                                    
            
            
                | 71 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 72 | 15 |  |     public function getDocumentIndex(): ?string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 73 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 74 | 15 |  |         if (property_exists($this, 'documentIndex')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 75 | 14 |  |             return $this->documentIndex; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 76 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 77 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 78 | 1 |  |         return null; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 79 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 80 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 81 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 82 |  |  |      * Get document type | 
            
                                                                                                            
                            
            
                                    
            
            
                | 83 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 84 |  |  |      * @return string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 85 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 86 | 13 |  |     public function getDocumentType(): string | 
            
                                                                                                            
                            
            
                                    
            
            
                | 87 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 88 | 13 |  |         if (property_exists($this, 'documentType')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 89 | 12 |  |             return (string)$this->documentType; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 90 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 91 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 92 | 1 |  |         return (string)$this->getTable(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 93 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 94 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 95 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 96 |  |  |      * Should sync document | 
            
                                                                                                            
                            
            
                                    
            
            
                | 97 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 98 |  |  |      * @return bool | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 99 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 100 | 2 |  |     public function shouldSyncDocument() : bool | 
            
                                                                                                            
                            
            
                                    
            
            
                | 101 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 102 | 2 |  |         if (property_exists($this, 'syncDocument')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 103 | 2 |  |             return (bool)$this->syncDocument; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 104 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 105 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 106 | 1 |  |         return true; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 107 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 108 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 109 |  |  |     /** | 
            
                                                                                                            
                            
            
                                    
            
            
                | 110 |  |  |      * Get document data | 
            
                                                                                                            
                            
            
                                    
            
            
                | 111 |  |  |      * | 
            
                                                                                                            
                            
            
                                    
            
            
                | 112 |  |  |      * @return array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 113 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 114 | 9 |  |     public function getDocumentData() : array | 
            
                                                                                                            
                            
            
                                    
            
            
                | 115 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 116 | 9 |  |         if (method_exists($this, 'buildDocument')) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 117 | 7 |  |             return $this->buildDocument(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 118 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 119 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 120 | 2 |  |         if (property_exists($this, 'searchable') && is_array($this->searchable)) { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 121 | 1 |  |             return $this->buildDocumentFromArray($this->searchable); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 122 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 123 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 124 | 1 |  |         return $this->toArray(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 125 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 126 |  |  |      | 
            
                                                                        
                            
            
                                    
            
            
                | 127 | 1 |  |     private function buildDocumentFromArray(array $searchable) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 128 |  |  |     { | 
            
                                                                        
                            
            
                                    
            
            
                | 129 | 1 |  |         $document = []; | 
            
                                                                        
                            
            
                                    
            
            
                | 130 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 131 | 1 |  |         foreach ($searchable as $value) { | 
            
                                                                        
                            
            
                                    
            
            
                | 132 | 1 |  |             $result = $this->$value; | 
            
                                                                        
                            
            
                                    
            
            
                | 133 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 134 | 1 |  |             if ($result instanceof Collection) { | 
            
                                                                        
                            
            
                                    
            
            
                | 135 |  |  |                 $result = $result->toArray(); | 
            
                                                                        
                            
            
                                    
            
            
                | 136 | 1 |  |             } elseif ($result instanceof Carbon) { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                        
                            
            
                                    
            
            
                | 137 |  |  |                 $result = $result->toDateTimeString(); | 
            
                                                                        
                            
            
                                    
            
            
                | 138 |  |  |             } else { | 
            
                                                                        
                            
            
                                    
            
            
                | 139 | 1 |  |                 $result = $this->$value; | 
            
                                                                        
                            
            
                                    
            
            
                | 140 |  |  |             } | 
            
                                                                        
                            
            
                                    
            
            
                | 141 |  |  |              | 
            
                                                                        
                            
            
                                    
            
            
                | 142 | 1 |  |             $document[$value] = $result; | 
            
                                                                        
                            
            
                                    
            
            
                | 143 |  |  |         } | 
            
                                                                        
                            
            
                                    
            
            
                | 144 |  |  |          | 
            
                                                                        
                            
            
                                    
            
            
                | 145 | 1 |  |         return $document; | 
            
                                                                                                            
                            
            
                                    
            
            
                | 146 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 147 |  |  |      | 
            
                                                                                                            
                            
            
                                    
            
            
                | 148 |  |  |     /** | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 149 |  |  |      * @param $name | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 150 |  |  |      * @param $arguments | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 151 |  |  |      * @return mixed | 
            
                                                                                                            
                            
            
                                    
            
            
                | 152 |  |  |      */ | 
            
                                                                                                            
                            
            
                                    
            
            
                | 153 | 1 |  |     public function __call($name, $arguments) | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 154 |  |  |     { | 
            
                                                                                                            
                            
            
                                    
            
            
                | 155 | 1 |  |         if ($name == 'search') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 156 |  |  |             return Leopard::search()->model($this); | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 157 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 158 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 159 | 1 |  |         if ($name == 'suggest') { | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                                                                            
                            
            
                                    
            
            
                | 160 |  |  |             return Leopard::suggest(); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 161 |  |  |         } | 
            
                                                                                                            
                            
            
                                    
            
            
                | 162 |  |  |          | 
            
                                                                                                            
                            
            
                                    
            
            
                | 163 | 1 |  |         return parent::__call($name, $arguments); | 
            
                                                                                                            
                            
            
                                    
            
            
                | 164 |  |  |     } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                            
                                                                                            
                                                                                     | 
            
                                                                                                            
                                                                
            
                                    
            
            
                | 165 |  |  | } | 
                            
                    |  |  |  | 
                                                                                        
                                                                                     | 
            
                                                        
            
                                    
            
            
                | 166 |  |  |  |