-
Notifications
You must be signed in to change notification settings - Fork 10
/
macros.php
122 lines (101 loc) · 3.3 KB
/
macros.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
<?php
use Laravel\Scout\Builder;
use Illuminate\Database\Eloquent\Collection;
if (! Builder::hasMacro('count')) {
/**
* Return the total amount of results for the current query.
*
* @return int Number of results
*/
Builder::macro('count', function () {
$raw = $this->engine()->search($this);
return (int) $raw['nbHits'];
});
}
if (! Builder::hasMacro('aroundLatLng')) {
/**
* Search for entries around a given location.
*
* @see https://www.algolia.com/doc/guides/geo-search/geo-search-overview/
*
* @param float $lat Latitude of the center
* @param float $lng Longitude of the center
*
* @return Laravel\Scout\Builder
*/
Builder::macro('aroundLatLng', function ($lat, $lng) {
$callback = $this->callback;
$this->callback = function ($algolia, $query, $options) use ($lat, $lng, $callback) {
$options['aroundLatLng'] = (float) $lat . ',' . (float) $lng;
if ($callback) {
return call_user_func(
$callback,
$algolia,
$query,
$options
);
}
return $algolia->search($query, $options);
};
return $this;
});
}
if (! Builder::hasMacro('with')) {
/**
* Override the algolia search options to give you full control over the request,
* similar to official algolia-laravel package.
*
* Adds the final missing piece to scout to make the library useful.
*
* @param array $opts Latitude of the center
*
* @return Laravel\Scout\Builder
*/
Builder::macro('with', function ($opts) {
$callback = $this->callback;
$this->callback = function ($algolia, $query, $options) use ($opts, $callback) {
$options = array_merge($options, $opts);
if ($callback) {
return call_user_func(
$callback,
$algolia,
$query,
$options
);
}
return $algolia->search($query, $options);
};
return $this;
});
}
if (! Builder::hasMacro('hydrate')) {
/**
* get() hydrates records by looking up the Ids in the corresponding database
* This macro uses the data returned from the search results to hydrate
* the models and return a collection
*
* @return Collection
*/
Builder::macro('hydrate', function () {
$results = $this->engine()->search($this);
if (count($results['hits']) === 0) {
return Collection::make();
}
$hits = collect($results['hits']);
$className = get_class($this->model);
$models = new Collection();
/* If the model is fully guarded, we unguard it.
Fully garded is the default configuration and it will
only result in error.
If the `$guarded` attribute is set to a list of attribute
we take it into account. */
if (in_array('*', $this->model->getGuarded())) {
Eloquent::unguard();
}
$hits->each(function($item, $key) use ($className, $models) {
$models->push(new $className($item));
});
Eloquent::reguard();
return $models;
});
}