GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.
Completed
Push — master ( caf19e...cd31e6 )
by Freek
03:19
created

SearchQuery::setPage()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 1
1
<?php
2
3
namespace Spatie\SearchIndex\Query\Algolia;
4
5
class SearchQuery
6
{
7
    const LOGICAL_OPERATOR_AND = 'and';
8
    const LOGICAL_OPERATOR_OR = 'or';
9
10
    protected $query = '';
11
12
    /**
13
     * @var bool
14
     */
15
    protected $useLocationAwareSearch = false;
16
17
    /**
18
     * @var int
19
     */
20
    protected $lat;
21
22
    /**
23
     * @var int
24
     */
25
    protected $lng;
26
27
    /**
28
     * @var int
29
     */
30
    protected $aroundRadius;
31
32
    /**
33
     * @var array
34
     */
35
    protected $dateRestrictions = [];
36
37
    /**
38
     * @var array
39
     */
40
    protected $numericFilters = [];
41
42
    /**
43
     * @var array
44
     */
45
    protected $facets = [];
46
47
    /**
48
     * @var int
49
     */
50
    protected $hitsPerPage = 10000;
51
52
    /**
53
     * @var int
54
     */
55
    protected $page = 0;
56
57
    /**
58
     * Set the query to search for.
59
     *
60
     * @param mixed $query
61
     *
62
     * @return SearchQuery
63
     */
64
    public function searchFor($query)
65
    {
66
        $this->query = $query;
67
68
        return $this;
69
    }
70
71
    /**
72
     * Create a location aware search.
73
     *
74
     * @param int $lat
75
     * @param int $lng
76
     * @param int $aroundRadius
77
     *
78
     * @return $this
79
     */
80
    public function aroundLocation($lat, $lng, $aroundRadius = 30000)
81
    {
82
        $this->lat = $lat;
83
        $this->lng = $lng;
84
        $this->aroundRadius = $aroundRadius;
85
        $this->useLocationAwareSearch = true;
86
87
        return $this;
88
    }
89
90
    /**
91
     * Set a date restriction.
92
     *
93
     * @param string $dateFieldName
94
     * @param string $operation
95
     * @param \DateTime $date
96
     * @return $this
97
     */
98
    public function withDateRestriction($dateFieldName, $operation, \DateTime $date)
99
    {
100
        $this->dateRestrictions[] = compact('dateFieldName', 'operation', 'date');
101
102
        return $this;
103
    }
104
105
    /**
106
     * Set a facet.
107
     *
108
     * @param string $name
109
     * @param string $value
110
     * @return $this
111
     */
112
    public function withFacet($name, $value)
113
    {
114
        $this->facets[] = $name.':'.$value;
115
116
        return $this;
117
    }
118
119
    /**
120
     * @param int $hitsPerPage
121
     *
122
     * @return SearchQuery
123
     */
124
    public function setHitsPerPage($hitsPerPage)
125
    {
126
        $this->hitsPerPage = $hitsPerPage;
127
128
        return $this;
129
    }
130
131
    /**
132
     * @param int $page
133
     *
134
     * @return SearchQuery
135
     */
136
    public function setPage($page)
137
    {
138
        $this->page = $page;
139
140
        return $this;
141
    }
142
143
    /**
144
     * Set a numeric filter.
145
     *
146
     * @param string $name
147
     * @param string|array $values
148
     * @param string $logicalOperator
149
     * @return $this
150
     */
151
    public function withNumericFilter($name, $values, $logicalOperator = 'and')
152
    {
153
        if (!is_array($values)) {
154
            $values = [$values];
155
        }
156
157
        $numericalFilterArray = array_map(function ($value) use ($name) {
158
            return "{$name}={$value}";
159
        }, $values);
160
161
        $numericalFilter = implode(',', $numericalFilterArray);
162
163
        if ($logicalOperator == self::LOGICAL_OPERATOR_OR) {
164
            $numericalFilter = "({$numericalFilter})";
165
        }
166
167
        $this->numericFilters[] = $numericalFilter;
168
169
        return $this;
170
    }
171
172
    /**
173
     * Get the query as an array.
174
     *
175
     * @return array
176
     */
177
    public function toArray()
178
    {
179
        $query = [
180
            'numericFilters' => '',
181
            'facetFilters' => '',
182
        ];
183
184
        if ($this->query != '') {
185
            $query['query'] = $this->query;
186
        }
187
188
        if ($this->useLocationAwareSearch) {
189
            $query['aroundLatLng'] = $this->lat.','.$this->lng;
190
            $query['aroundRadius'] = $this->aroundRadius;
191
        }
192
193
        foreach ($this->dateRestrictions as $dateRestriction) {
194
            $query['numericFilters'] .= ','.
195
                $dateRestriction['dateFieldName'].
196
                $dateRestriction['operation'].
197
                $dateRestriction['date']->getTimeStamp();
198
        }
199
200
        foreach ($this->numericFilters as $filter) {
201
            $query['numericFilters'] .= ','.$filter;
202
        }
203
204
        foreach ($this->facets as $facet) {
205
            $query['facetFilters'] .= ','.$facet;
206
        }
207
208
        $query['page'] = $this->page;
209
        $query['hitsPerPage'] = $this->hitsPerPage;
210
211
        return $query;
212
    }
213
}
214