1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
declare(strict_types=1); |
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* This file is part of Scout Extended. |
7
|
|
|
* |
8
|
|
|
* (c) Algolia Team <[email protected]> |
9
|
|
|
* |
10
|
|
|
* For the full copyright and license information, please view the LICENSE |
11
|
|
|
* file that was distributed with this source code. |
12
|
|
|
*/ |
13
|
|
|
|
14
|
|
|
namespace Algolia\ScoutExtended; |
15
|
|
|
|
16
|
|
|
use function is_callable; |
17
|
|
|
use Laravel\Scout\Builder as BaseBuilder; |
18
|
|
|
|
19
|
|
|
final class Builder extends BaseBuilder |
20
|
|
|
{ |
21
|
|
|
/* |
22
|
|
|
* Count the number of items in the search results. |
23
|
|
|
* |
24
|
|
|
* @return int |
25
|
|
|
*/ |
26
|
2 |
|
public function count(): int |
27
|
|
|
{ |
28
|
2 |
|
$raw = $this->raw(); |
29
|
|
|
|
30
|
2 |
|
return array_key_exists('nbHits', $raw) ? (int) $raw['nbHits'] : 0; |
|
|
|
|
31
|
|
|
} |
32
|
|
|
|
33
|
|
|
/* |
34
|
|
|
* Customize the search with the provided search parameters. |
35
|
|
|
* |
36
|
|
|
* @link https://www.algolia.com/doc/api-reference/search-api-parameters |
37
|
|
|
* |
38
|
|
|
* @param array $parameters The search parameters. |
39
|
|
|
* |
40
|
|
|
* @return \Algolia\ScoutExtended\Builder |
41
|
|
|
*/ |
42
|
2 |
|
public function with(array $parameters): Builder |
43
|
|
|
{ |
44
|
2 |
|
$callback = $this->callback; |
45
|
|
|
|
46
|
|
|
$this->callback = function ($algolia, $query, $baseParameters) use ($parameters, $callback) { |
|
|
|
|
47
|
2 |
|
$parameters = array_merge($parameters, $baseParameters); |
48
|
|
|
|
49
|
2 |
|
if (is_callable($callback)) { |
50
|
|
|
return $callback($algolia, $query, $parameters); |
51
|
|
|
} |
52
|
|
|
|
53
|
2 |
|
return $algolia->search($query, $parameters); |
54
|
|
|
}; |
55
|
|
|
|
56
|
2 |
|
return $this; |
57
|
|
|
} |
58
|
|
|
|
59
|
|
|
/* |
60
|
|
|
* Customize the search to be around a given location. |
61
|
|
|
* |
62
|
|
|
* @link https://www.algolia.com/doc/guides/geo-search/geo-search-overview |
63
|
|
|
* |
64
|
|
|
* @param float $lat Latitude of the center |
65
|
|
|
* @param float $lng Longitude of the center |
66
|
|
|
* |
67
|
|
|
* @return \Algolia\ScoutExtended\Builder |
68
|
|
|
*/ |
69
|
1 |
|
public function aroundLatLng(float $lat, float $lng): Builder |
70
|
|
|
{ |
71
|
1 |
|
return $this->with([ |
72
|
1 |
|
'aroundLatLng' => $lat.','.$lng, |
73
|
|
|
]); |
74
|
|
|
} |
75
|
|
|
} |
76
|
|
|
|