1 | <?php |
||
10 | class TNTSearchEngine extends Engine |
||
11 | { |
||
12 | /** |
||
13 | * Create a new engine instance. |
||
14 | * |
||
15 | * @param TNTSearch $tnt |
||
16 | */ |
||
17 | public function __construct(TNTSearch $tnt) |
||
18 | { |
||
19 | $this->tnt = $tnt; |
||
|
|||
20 | } |
||
21 | |||
22 | /** |
||
23 | * Update the given model in the index. |
||
24 | * |
||
25 | * @param Collection $models |
||
26 | * |
||
27 | * @return void |
||
28 | */ |
||
29 | public function update($models) |
||
30 | { |
||
31 | $this->initIndex($models->first()); |
||
32 | |||
33 | $models->each(function ($model) { |
||
34 | $searchableFields = $model->toSearchableArray(); |
||
35 | |||
36 | $this->tnt->selectIndex("{$this->getIndexName($model)}.index"); |
||
37 | $index = $this->tnt->getIndex(); |
||
38 | $index->setPrimaryKey($model->getKeyName()); |
||
39 | |||
40 | if ($model->getKey()) { |
||
41 | $index->update($model->getKey(), $searchableFields); |
||
42 | } else { |
||
43 | $index->insert($searchableFields); |
||
44 | } |
||
45 | }); |
||
46 | } |
||
47 | |||
48 | /** |
||
49 | * Remove the given model from the index. |
||
50 | * |
||
51 | * @param Collection $models |
||
52 | * |
||
53 | * @return void |
||
54 | */ |
||
55 | public function delete($models) |
||
65 | |||
66 | /** |
||
67 | * Perform the given search on the engine. |
||
68 | * |
||
69 | * @param Builder $builder |
||
70 | * |
||
71 | * @return mixed |
||
72 | */ |
||
73 | public function search(Builder $builder) |
||
77 | |||
78 | /** |
||
79 | * Perform the given search on the engine. |
||
80 | * |
||
81 | * @param Builder $builder |
||
82 | * @param int $perPage |
||
83 | * @param int $page |
||
84 | * |
||
85 | * @return mixed |
||
86 | */ |
||
87 | public function paginate(Builder $builder, $perPage, $page) |
||
100 | |||
101 | /** |
||
102 | * Perform the given search on the engine. |
||
103 | * |
||
104 | * @param Builder $builder |
||
105 | * @param array $options |
||
106 | * |
||
107 | * @return mixed |
||
108 | */ |
||
109 | protected function performSearch(Builder $builder, array $options = []) |
||
118 | |||
119 | /** |
||
120 | * Get the filter array for the query. |
||
121 | * |
||
122 | * @param Builder $builder |
||
123 | * |
||
124 | * @return array |
||
125 | */ |
||
126 | protected function filters(Builder $builder) |
||
132 | |||
133 | /** |
||
134 | * Map the given results to instances of the given model. |
||
135 | * |
||
136 | * @param mixed $results |
||
137 | * @param \Illuminate\Database\Eloquent\Model $model |
||
138 | * |
||
139 | * @return Collection |
||
140 | */ |
||
141 | public function map($results, $model) |
||
155 | |||
156 | public function initIndex($model) |
||
170 | |||
171 | /** |
||
172 | * @param $model |
||
173 | * @return mixed |
||
174 | */ |
||
175 | private function getIndexName($model) |
||
183 | } |
||
184 |
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: