Completed
Push — master ( 74f7c1...8b2cc0 )
by Nuno
59:07 queued 52:46
created

Builder::whereIn()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 5
nc 1
nop 2
dl 0
loc 9
ccs 5
cts 5
cp 1
crap 1
rs 10
c 0
b 0
f 0
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 function func_num_args;
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 18
    public function __construct($model, $query, $callback = null, $softDelete = false)
28
    {
29 18
        parent::__construct($model, (string) $query, $callback, $softDelete);
30 18
    }
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;
0 ignored issues
show
Bug introduced by
It seems like $raw can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $search of array_key_exists() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

58
        return array_key_exists('nbHits', /** @scrutinizer ignore-type */ $raw) ? (int) $raw['nbHits'] : 0;
Loading history...
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
The expression return parent::where($fi...->transform($operator)) returns the type Laravel\Scout\Builder which includes types incompatible with the type-hinted return Algolia\ScoutExtended\Builder.
Loading history...
77
        }
78
79 2
        return parent::where($field, "$operator {$this->transform($value)}");
0 ignored issues
show
Bug Best Practice introduced by
The expression return parent::where($fi...his->transform($value)) returns the type Laravel\Scout\Builder which includes types incompatible with the type-hinted return Algolia\ScoutExtended\Builder.
Loading history...
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) {
0 ignored issues
show
Documentation Bug introduced by
It seems like function(...) { /* ... */ } of type callable is incompatible with the declared type string of property $callback.

Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.

Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..

Loading history...
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