Skip to content
This repository was archived by the owner on Dec 12, 2021. It is now read-only.

Commit 317464f

Browse files
Merge pull request #20 from devloopsnet/analysis-J2joev
Apply fixes from StyleCI
2 parents 2beb173 + 2e06b6a commit 317464f

File tree

5 files changed

+105
-67
lines changed

5 files changed

+105
-67
lines changed

src/Classes/TypesenseDocumentIndexResponse.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
/**
66
* Class TypesenseDocumentIndexResponse.
77
*
8-
* @package Devloops\LaravelTypesense\Classes
98
* @date 02/10/2021
9+
*
1010
* @author Abdullah Al-Faqeir <[email protected]>
1111
*/
1212
class TypesenseDocumentIndexResponse

src/Engines/TypesenseSearchEngine.php

Lines changed: 64 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@
44

55
use Devloops\LaravelTypesense\Typesense;
66
use Exception;
7-
use Illuminate\Support\LazyCollection;
8-
use Illuminate\Support\Collection;
9-
use Illuminate\Support\Str;
107
use Illuminate\Database\Eloquent\Model;
118
use Illuminate\Database\Eloquent\SoftDeletes;
9+
use Illuminate\Support\Collection;
10+
use Illuminate\Support\LazyCollection;
11+
use Illuminate\Support\Str;
1212
use Laravel\Scout\Builder;
1313
use Laravel\Scout\Engines\Engine;
1414

@@ -82,7 +82,7 @@ public function update($models): void
8282
$models->each->pushSoftDeleteMetadata();
8383
}
8484

85-
$this->typesense->importDocuments($collection, $models->map(fn($m) => $m->toSearchableArray())
85+
$this->typesense->importDocuments($collection, $models->map(fn ($m) => $m->toSearchableArray())
8686
->toArray());
8787
}
8888

@@ -104,9 +104,10 @@ public function delete($models): void
104104
/**
105105
* @param \Laravel\Scout\Builder $builder
106106
*
107-
* @return mixed
108107
* @throws \Http\Client\Exception
109108
* @throws \Typesense\Exceptions\TypesenseClientError
109+
*
110+
* @return mixed
110111
*/
111112
public function search(Builder $builder): mixed
112113
{
@@ -115,12 +116,13 @@ public function search(Builder $builder): mixed
115116

116117
/**
117118
* @param \Laravel\Scout\Builder $builder
118-
* @param int $perPage
119-
* @param int $page
119+
* @param int $perPage
120+
* @param int $page
120121
*
121-
* @return mixed
122122
* @throws \Http\Client\Exception
123123
* @throws \Typesense\Exceptions\TypesenseClientError
124+
*
125+
* @return mixed
124126
*/
125127
public function paginate(Builder $builder, $perPage, $page): mixed
126128
{
@@ -129,21 +131,21 @@ public function paginate(Builder $builder, $perPage, $page): mixed
129131

130132
/**
131133
* @param \Laravel\Scout\Builder $builder
132-
* @param int $page
133-
* @param int $perPage
134+
* @param int $page
135+
* @param int $perPage
134136
*
135137
* @return array
136138
*/
137139
private function buildSearchParams(Builder $builder, int $page, int $perPage): array
138140
{
139141
$params = [
140-
'q' => $builder->query,
141-
'query_by' => implode(',', $builder->model->typesenseQueryBy()),
142-
'filter_by' => $this->filters($builder),
143-
'per_page' => $perPage,
144-
'page' => $page,
145-
'highlight_start_tag' => $this->startTag,
146-
'highlight_end_tag' => $this->endTag,
142+
'q' => $builder->query,
143+
'query_by' => implode(',', $builder->model->typesenseQueryBy()),
144+
'filter_by' => $this->filters($builder),
145+
'per_page' => $perPage,
146+
'page' => $page,
147+
'highlight_start_tag' => $this->startTag,
148+
'highlight_end_tag' => $this->endTag,
147149
];
148150

149151
if ($this->limitHits > 0) {
@@ -175,8 +177,8 @@ private function buildSearchParams(Builder $builder, int $page, int $perPage): a
175177
* Parse location order by for sort_by.
176178
*
177179
* @param string $column
178-
* @param float $lat
179-
* @param float $lng
180+
* @param float $lat
181+
* @param float $lng
180182
* @param string $direction
181183
*
182184
* @return string
@@ -186,11 +188,12 @@ private function parseOrderByLocation(string $column, float $lat, float $lng, st
186188
{
187189
$direction = Str::lower($direction) === 'asc' ? 'asc' : 'desc';
188190
$str = $column.'('.$lat.', '.$lng.')';
191+
189192
return $str.':'.$direction;
190193
}
191194

192195
/**
193-
* Parse sort_by fields
196+
* Parse sort_by fields.
194197
*
195198
* @param array $orders
196199
*
@@ -202,16 +205,18 @@ private function parseOrderBy(array $orders): string
202205
foreach ($orders as $order) {
203206
$sortByArr[] = $order['column'].':'.$order['direction'];
204207
}
208+
205209
return implode(',', $sortByArr);
206210
}
207211

208212
/**
209213
* @param \Laravel\Scout\Builder $builder
210-
* @param array $options
214+
* @param array $options
211215
*
212-
* @return mixed
213216
* @throws \Http\Client\Exception
214217
* @throws \Typesense\Exceptions\TypesenseClientError
218+
*
219+
* @return mixed
215220
*/
216221
protected function performSearch(Builder $builder, array $options = []): mixed
217222
{
@@ -220,11 +225,12 @@ protected function performSearch(Builder $builder, array $options = []): mixed
220225
if ($builder->callback) {
221226
return call_user_func($builder->callback, $documents, $builder->query, $options);
222227
}
228+
223229
return $documents->search($options);
224230
}
225231

226232
/**
227-
* Prepare filters
233+
* Prepare filters.
228234
*
229235
* @param Builder $builder
230236
*
@@ -234,18 +240,18 @@ protected function filters(Builder $builder): string
234240
{
235241
return collect($builder->wheres)
236242
->map([
237-
$this,
238-
'parseFilters',
243+
$this,
244+
'parseFilters',
239245
])
240246
->values()
241247
->implode(' && ');
242248
}
243249

244250
/**
245-
* Parse typesense filters
251+
* Parse typesense filters.
246252
*
247253
* @param array|string $value
248-
* @param string $key
254+
* @param string $key
249255
*
250256
* @return string
251257
*/
@@ -254,6 +260,7 @@ public function parseFilters(array|string $value, string $key): string
254260
if (is_array($value)) {
255261
return sprintf('%s:%s', $key, implode('', $value));
256262
}
263+
257264
return sprintf('%s:=%s', $key, $value);
258265
}
259266

@@ -270,15 +277,15 @@ public function mapIds($results): Collection
270277
}
271278

272279
/**
273-
* @param \Laravel\Scout\Builder $builder
274-
* @param mixed $results
280+
* @param \Laravel\Scout\Builder $builder
281+
* @param mixed $results
275282
* @param \Illuminate\Database\Eloquent\Model $model
276283
*
277284
* @return \Illuminate\Database\Eloquent\Collection
278285
*/
279286
public function map(Builder $builder, $results, $model): \Illuminate\Database\Eloquent\Collection
280287
{
281-
if ((int)($results['found'] ?? 0) === 0) {
288+
if ((int) ($results['found'] ?? 0) === 0) {
282289
return $model->newCollection();
283290
}
284291

@@ -288,6 +295,7 @@ public function map(Builder $builder, $results, $model): \Illuminate\Database\El
288295
->all();
289296

290297
$objectIdPositions = array_flip($objectIds);
298+
291299
return $model->getScoutModelsByIds($builder, $objectIds)
292300
->filter(static function ($model) use ($objectIds) {
293301
return in_array($model->getScoutKey(), $objectIds, false);
@@ -303,7 +311,7 @@ public function map(Builder $builder, $results, $model): \Illuminate\Database\El
303311
*/
304312
public function getTotalCount($results): int
305313
{
306-
return (int)($results['found'] ?? 0);
314+
return (int) ($results['found'] ?? 0);
307315
}
308316

309317
/**
@@ -329,15 +337,15 @@ protected function usesSoftDelete($model): bool
329337
}
330338

331339
/**
332-
* @param \Laravel\Scout\Builder $builder
333-
* @param mixed $results
340+
* @param \Laravel\Scout\Builder $builder
341+
* @param mixed $results
334342
* @param \Illuminate\Database\Eloquent\Model $model
335343
*
336344
* @return \Illuminate\Support\LazyCollection
337345
*/
338346
public function lazyMap(Builder $builder, $results, $model): LazyCollection
339347
{
340-
if ((int)($results['found'] ?? 0) === 0) {
348+
if ((int) ($results['found'] ?? 0) === 0) {
341349
return LazyCollection::make($model->newCollection());
342350
}
343351

@@ -361,10 +369,11 @@ public function lazyMap(Builder $builder, $results, $model): LazyCollection
361369

362370
/**
363371
* @param string $name
364-
* @param array $options
372+
* @param array $options
365373
*
366-
* @return void
367374
* @throws \Exception
375+
*
376+
* @return void
368377
*/
369378
public function createIndex($name, array $options = []): void
370379
{
@@ -381,11 +390,12 @@ public function createIndex($name, array $options = []): void
381390
public function groupBy(array $groupBy): static
382391
{
383392
$this->groupBy = $groupBy;
393+
384394
return $this;
385395
}
386396

387397
/**
388-
* Maximum number of hits to be returned for every group. (default: 3)
398+
* Maximum number of hits to be returned for every group. (default: 3).
389399
*
390400
* @param int $groupByLimit
391401
*
@@ -394,11 +404,12 @@ public function groupBy(array $groupBy): static
394404
public function groupByLimit(int $groupByLimit): static
395405
{
396406
$this->groupByLimit = $groupByLimit;
407+
397408
return $this;
398409
}
399410

400411
/**
401-
* The start tag used for the highlighted snippets. (default: <mark>)
412+
* The start tag used for the highlighted snippets. (default: <mark>).
402413
*
403414
* @param string $startTag
404415
*
@@ -407,11 +418,12 @@ public function groupByLimit(int $groupByLimit): static
407418
public function setHighlightStartTag(string $startTag): static
408419
{
409420
$this->startTag = $startTag;
421+
410422
return $this;
411423
}
412424

413425
/**
414-
* The end tag used for the highlighted snippets. (default: </mark>)
426+
* The end tag used for the highlighted snippets. (default: </mark>).
415427
*
416428
* @param string $endTag
417429
*
@@ -420,11 +432,12 @@ public function setHighlightStartTag(string $startTag): static
420432
public function setHighlightEndTag(string $endTag): static
421433
{
422434
$this->endTag = $endTag;
435+
423436
return $this;
424437
}
425438

426439
/**
427-
* Maximum number of hits that can be fetched from the collection (default: no limit)
440+
* Maximum number of hits that can be fetched from the collection (default: no limit).
428441
*
429442
* (page * per_page) should be less than this number for the search request to return results.
430443
*
@@ -435,37 +448,40 @@ public function setHighlightEndTag(string $endTag): static
435448
public function limitHits(int $limitHits): static
436449
{
437450
$this->limitHits = $limitHits;
451+
438452
return $this;
439453
}
440454

441455
/**
442-
* Add location to order by clause
456+
* Add location to order by clause.
443457
*
444458
* @param string $column
445-
* @param float $lat
446-
* @param float $lng
459+
* @param float $lat
460+
* @param float $lng
447461
* @param string $direction
448462
*
449463
* @return $this
450464
*/
451465
public function orderByLocation(string $column, float $lat, float $lng, string $direction): static
452466
{
453467
$this->locationOrderBy = [
454-
'column' => $column,
455-
'lat' => $lat,
456-
'lng' => $lng,
457-
'direction' => $direction,
468+
'column' => $column,
469+
'lat' => $lat,
470+
'lng' => $lng,
471+
'direction' => $direction,
458472
];
473+
459474
return $this;
460475
}
461476

462477
/**
463478
* @param string $name
464479
*
465-
* @return array
466480
* @throws \Http\Client\Exception
467481
* @throws \Typesense\Exceptions\ObjectNotFound
468482
* @throws \Typesense\Exceptions\TypesenseClientError
483+
*
484+
* @return array
469485
*/
470486
public function deleteIndex($name): array
471487
{

0 commit comments

Comments
 (0)