algolia /
scout-extended
| 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 func_num_args; |
||
| 17 | use function is_callable; |
||
| 18 | use Laravel\Scout\Builder as BaseBuilder; |
||
| 19 | |||
| 20 | final class Builder extends BaseBuilder |
||
| 21 | { |
||
| 22 | /** |
||
| 23 | * {@inheritdoc} |
||
| 24 | * |
||
| 25 | * @see https://github.com/algolia/scout-extended/issues/98 |
||
| 26 | */ |
||
| 27 | 20 | public function __construct($model, $query, $callback = null, $softDelete = false) |
|
| 28 | { |
||
| 29 | 20 | parent::__construct($model, (string) $query, $callback, $softDelete); |
|
| 30 | 20 | } |
|
| 31 | |||
| 32 | /** |
||
| 33 | * Customize the search to be around a given location. |
||
| 34 | * |
||
| 35 | * @link https://www.algolia.com/doc/guides/geo-search/geo-search-overview |
||
| 36 | * |
||
| 37 | * @param float $lat Latitude of the center |
||
| 38 | * @param float $lng Longitude of the center |
||
| 39 | * |
||
| 40 | * @return $this |
||
| 41 | */ |
||
| 42 | 1 | public function aroundLatLng(float $lat, float $lng): self |
|
| 43 | { |
||
| 44 | 1 | return $this->with([ |
|
| 45 | 1 | 'aroundLatLng' => $lat.','.$lng, |
|
| 46 | ]); |
||
| 47 | } |
||
| 48 | |||
| 49 | /** |
||
| 50 | * Count the number of items in the search results. |
||
| 51 | * |
||
| 52 | * @return int |
||
| 53 | */ |
||
| 54 | 2 | public function count(): int |
|
| 55 | { |
||
| 56 | 2 | $raw = $this->raw(); |
|
| 57 | |||
| 58 | 2 | return array_key_exists('nbHits', $raw) ? (int) $raw['nbHits'] : 0; |
|
| 59 | } |
||
| 60 | |||
| 61 | /** |
||
| 62 | * Customize the search adding a where clause. |
||
| 63 | * |
||
| 64 | * @param string $field |
||
| 65 | * @param mixed $operator |
||
| 66 | * @param mixed $value |
||
| 67 | * |
||
| 68 | * @return $this |
||
| 69 | */ |
||
| 70 | 7 | public function where($field, $operator, $value = null): self |
|
| 71 | { |
||
| 72 | // Here we will make some assumptions about the operator. If only 2 values are |
||
| 73 | // passed to the method, we will assume that the operator is an equals sign |
||
| 74 | // and keep going. Otherwise, we'll require the operator to be passed in. |
||
| 75 | 7 | if (func_num_args() === 2) { |
|
| 76 | 5 | return parent::where($field, $this->transform($operator)); |
|
|
0 ignored issues
–
show
Bug
Best Practice
introduced
by
Loading history...
|
|||
| 77 | } |
||
| 78 | |||
| 79 | 2 | return parent::where($field, "$operator {$this->transform($value)}"); |
|
|
0 ignored issues
–
show
|
|||
| 80 | } |
||
| 81 | |||
| 82 | /** |
||
| 83 | * Customize the search adding a where between clause. |
||
| 84 | * |
||
| 85 | * @param string $field |
||
| 86 | * @param array $values |
||
| 87 | * |
||
| 88 | * @return $this |
||
| 89 | */ |
||
| 90 | 2 | public function whereBetween($field, array $values): self |
|
| 91 | { |
||
| 92 | 2 | return $this->where("$field:", "{$this->transform($values[0])} TO {$this->transform($values[1])}"); |
|
| 93 | } |
||
| 94 | |||
| 95 | /** |
||
| 96 | * Customize the search adding a where in clause. |
||
| 97 | * |
||
| 98 | * @param string $field |
||
| 99 | * @param array $values |
||
| 100 | * |
||
| 101 | * @return $this |
||
| 102 | */ |
||
| 103 | 2 | public function whereIn($field, array $values): self |
|
| 104 | { |
||
| 105 | $wheres = array_map(function ($value) use ($field) { |
||
| 106 | 2 | return "$field={$this->transform($value)}"; |
|
| 107 | 2 | }, array_values($values)); |
|
| 108 | |||
| 109 | 2 | $this->wheres[] = $wheres; |
|
| 110 | |||
| 111 | 2 | return $this; |
|
| 112 | } |
||
| 113 | |||
| 114 | /** |
||
| 115 | * Customize the search with the provided search parameters. |
||
| 116 | * |
||
| 117 | * @link https://www.algolia.com/doc/api-reference/search-api-parameters |
||
| 118 | * |
||
| 119 | * @param array $parameters The search parameters. |
||
| 120 | * |
||
| 121 | * @return $this |
||
| 122 | */ |
||
| 123 | 2 | public function with(array $parameters): self |
|
| 124 | { |
||
| 125 | 2 | $callback = $this->callback; |
|
| 126 | |||
| 127 | $this->callback = function ($algolia, $query, $baseParameters) use ($parameters, $callback) { |
||
| 128 | 2 | $parameters = array_merge($parameters, $baseParameters); |
|
| 129 | |||
| 130 | 2 | if (is_callable($callback)) { |
|
| 131 | return $callback($algolia, $query, $parameters); |
||
| 132 | } |
||
| 133 | |||
| 134 | 2 | return $algolia->search($query, $parameters); |
|
| 135 | }; |
||
| 136 | |||
| 137 | 2 | return $this; |
|
| 138 | } |
||
| 139 | |||
| 140 | /** |
||
| 141 | * Transform the given where value. |
||
| 142 | * |
||
| 143 | * @param mixed $value |
||
| 144 | * |
||
| 145 | * @return mixed |
||
| 146 | */ |
||
| 147 | 8 | private function transform($value) |
|
| 148 | { |
||
| 149 | /* |
||
| 150 | * Casts carbon instances to timestamp. |
||
| 151 | */ |
||
| 152 | 8 | if ($value instanceof \Illuminate\Support\Carbon) { |
|
| 153 | 2 | $value = $value->getTimestamp(); |
|
| 154 | } |
||
| 155 | |||
| 156 | 8 | return $value; |
|
| 157 | } |
||
| 158 | } |
||
| 159 |