Completed
Push — master ( 554b31...9f86af )
by Nuno
31:31 queued 27:37
created

Builder::transform()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 10
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 2

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 1
dl 0
loc 10
ccs 4
cts 4
cp 1
crap 2
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
     * Customize the search to be around a given location.
24
     *
25
     * @link https://www.algolia.com/doc/guides/geo-search/geo-search-overview
26
     *
27
     * @param float $lat Latitude of the center
28
     * @param float $lng Longitude of the center
29
     *
30
     * @return $this
31
     */
32 1
    public function aroundLatLng(float $lat, float $lng): self
33
    {
34 1
        return $this->with([
35 1
            'aroundLatLng' => $lat.','.$lng,
36
        ]);
37
    }
38
39
    /**
40
     * Count the number of items in the search results.
41
     *
42
     * @return int
43
     */
44 2
    public function count(): int
45
    {
46 2
        $raw = $this->raw();
47
48 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

48
        return array_key_exists('nbHits', /** @scrutinizer ignore-type */ $raw) ? (int) $raw['nbHits'] : 0;
Loading history...
49
    }
50
51
    /**
52
     * Customize the search adding a where clause.
53
     *
54
     * @param  string $field
55
     * @param  mixed $operator
56
     * @param  mixed $value
57
     *
58
     * @return $this
59
     */
60 6
    public function where($field, $operator, $value = null): self
61
    {
62
        // Here we will make some assumptions about the operator. If only 2 values are
63
        // passed to the method, we will assume that the operator is an equals sign
64
        // and keep going. Otherwise, we'll require the operator to be passed in.
65 6
        if (func_num_args() === 2) {
66 4
            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...
67
        }
68
69 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...
70
    }
71
72
    /**
73
     * Customize the search adding a where between clause.
74
     *
75
     * @param  string $field
76
     * @param  array $values
77
     *
78
     * @return $this
79
     */
80 2
    public function whereBetween($field, array $values): self
81
    {
82 2
        return $this->where("$field:", "{$this->transform($values[0])} TO {$this->transform($values[1])}");
83
    }
84
85
    /**
86
     * Customize the search with the provided search parameters.
87
     *
88
     * @link https://www.algolia.com/doc/api-reference/search-api-parameters
89
     *
90
     * @param array $parameters The search parameters.
91
     *
92
     * @return $this
93
     */
94 2
    public function with(array $parameters): self
95
    {
96 2
        $callback = $this->callback;
97
98
        $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...
99 2
            $parameters = array_merge($parameters, $baseParameters);
100
101 2
            if (is_callable($callback)) {
102
                return $callback($algolia, $query, $parameters);
103
            }
104
105 2
            return $algolia->search($query, $parameters);
106
        };
107
108 2
        return $this;
109
    }
110
111
    /**
112
     * Transform the given where value.
113
     *
114
     * @param  mixed $value
115
     *
116
     * @return mixed
117
     */
118 6
    private function transform($value)
119
    {
120
        /*
121
         * Casts carbon instances to timestamp.
122
         */
123 6
        if ($value instanceof \Illuminate\Support\Carbon) {
124 2
            $value = $value->getTimestamp();
125
        }
126
127 6
        return $value;
128
    }
129
}
130