Passed
Push — master ( f8f76e...e5015c )
by Bas
12:29
created

HasArangoSearchFunctions::ngramMatch()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 20
Code Lines 10

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 12
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 10
c 1
b 0
f 0
nc 1
nop 4
dl 0
loc 20
ccs 12
cts 12
cp 1
crap 1
rs 9.9332
1
<?php
2
3
namespace LaravelFreelancerNL\FluentAQL\AQL;
4
5
use LaravelFreelancerNL\FluentAQL\Expressions\Expression;
6
use LaravelFreelancerNL\FluentAQL\Expressions\FunctionExpression;
7
use LaravelFreelancerNL\FluentAQL\QueryBuilder;
8
9
/**
10
 * Geo AQL functions.
11
 *
12
 * @see https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html
13
 */
14
trait HasArangoSearchFunctions
15
{
16
    /**
17
     * set the analyzer for the given search expression
18
     *
19
     * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#analyzer
20
     */
21 8
    public function analyzer(): FunctionExpression
22
    {
23 8
        $arguments = func_get_args();
24
25 8
        $analyzer = array_pop($arguments);
26
27 8
        $predicates = $arguments;
28 8
        if (! is_array($predicates[0])) {
29 7
            $predicates = [[
30 7
                ...$predicates
31
            ]];
32
        }
33 8
        $preppedArguments = [
34 8
            'predicates' => $predicates,
35 8
            'analyzer' => $analyzer
36
        ];
37 8
        return new FunctionExpression('ANALYZER', $preppedArguments);
38
    }
39
40
    /**
41
     * Override the boost value of the search expression
42
     *
43
     * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#boost
44
     */
45 2
    public function boost(): FunctionExpression
46
    {
47 2
        $arguments = func_get_args();
48
49 2
        $boost = array_pop($arguments);
50
51 2
        $predicates = $arguments;
52 2
        if (! is_array($predicates[0])) {
53 1
            $predicates = [[
54 1
                ...$predicates
55
            ]];
56
        }
57 2
        $preppedArguments = [
58 2
            'predicates' => $predicates,
59 2
            'boost' => $boost
60
        ];
61 2
        return new FunctionExpression('BOOST', $preppedArguments);
62
    }
63
64
    /**
65
     * Sorts documents using the Best Matching 25 algorithm (Okapi BM25).
66
     *
67
     * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#bm25
68
     */
69 3
    public function bm25(mixed $doc, mixed $k = null, mixed $b = null): FunctionExpression
70
    {
71 3
        $arguments = [
72 3
            $doc,
73 3
            $k,
74 3
            $b
75
        ];
76 3
        $arguments = array_filter($arguments);
77
78 3
        return new FunctionExpression('BM25', $arguments);
79
    }
80
81
    /**
82
     * Sorts documents using the term frequency–inverse document frequency algorithm (TF-IDF).
83
     *
84
     * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#tfidf
85
     */
86 2
    public function tfidf(mixed $doc, mixed $normalize = null): FunctionExpression
87
    {
88 2
        $arguments = [
89 2
            $doc,
90 2
            $normalize,
91
        ];
92 2
        $arguments = array_filter($arguments);
93
94 2
        return new FunctionExpression('TFIDF', $arguments);
95
    }
96
97
    /**
98
     * Match documents where the attribute at path is present and is of the specified data type.
99
     *
100
     * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#exists
101
     *
102
     * @param string|QueryBuilder|Expression $path
103
     * @param QueryBuilder|Expression|string|null $type
104
     * @return FunctionExpression
105
     */
106 2
    public function exists(
107
        Expression|QueryBuilder|string $path,
108
        Expression|null|string|QueryBuilder $type = null
0 ignored issues
show
Bug introduced by
The type LaravelFreelancerNL\FluentAQL\AQL\null was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
109
    ): FunctionExpression {
110 2
        $arguments = [
111 2
            $path,
112 2
            $type,
113
        ];
114 2
        $arguments = array_filter($arguments);
115
116 2
        return new FunctionExpression('EXISTS', $arguments);
117
    }
118
119
    /**
120
     * Match documents where the attribute at path is present and is of the specified data type.
121
     *
122
     * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#in_range
123
     *
124
     * @param string|QueryBuilder|Expression $path
125
     * @param mixed $low
126
     * @param mixed $high
127
     * @param mixed|null $includeLow
128
     * @param mixed|null $includeHigh
129
     * @return FunctionExpression
130
     */
131 2
    public function inRange(
132
        mixed $path,
133
        mixed $low,
134
        mixed $high,
135
        mixed $includeLow = null,
136
        mixed $includeHigh = null
137
    ): FunctionExpression {
138 2
        $arguments = [
139 2
            "path" => $path,
140 2
            "low" => $low,
141 2
            "high" => $high,
142 2
            "includeLow" => $includeLow,
143 2
            "includeHigh" => $includeHigh
144
        ];
145 2
        $arguments = array_filter(
146 2
            $arguments,
147 2
            function ($value) {
148 2
                return ! is_null($value);
149 2
            }
150
        );
151
152 2
        return new FunctionExpression('IN_RANGE', $arguments);
153
    }
154
155
    /**
156
     * Match documents with a Damerau-Levenshtein distance lower than or equal to
157
     * the distance between the stored attribute value and target.
158
     *
159
     * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#levenshtein_match
160
     *
161
     * @param string|QueryBuilder|Expression $path
162
     * @param string|QueryBuilder|Expression $target
163
     * @param int|QueryBuilder|Expression $distance
164
     * @param null|bool|QueryBuilder|Expression $transpositions
165
     * @param null|int|QueryBuilder|Expression $maxTerms
166
     * @param null|string|QueryBuilder|Expression $prefix
167
     * @return FunctionExpression
168
     */
169 2
    public function levenshteinMatch(
170
        mixed $path,
171
        mixed $target,
172
        mixed $distance,
173
        mixed $transpositions = null,
174
        mixed $maxTerms = null,
175
        mixed $prefix = null
176
    ): FunctionExpression {
177 2
        $arguments = [
178 2
            "path" => $path,
179 2
            "target" => $target,
180 2
            "distance" => $distance,
181 2
            "transpositions" => $transpositions,
182 2
            "maxTerms" => $maxTerms,
183 2
            "prefix" => $prefix,
184
        ];
185 2
        $arguments = array_filter(
186 2
            $arguments,
187 2
            function ($value) {
188 2
                return ! is_null($value);
189 2
            }
190
        );
191
192 2
        return new FunctionExpression('LEVENSHTEIN_MATCH', $arguments);
193
    }
194
195
    /**
196
     * Check whether the pattern search is contained in the attribute denoted by path, using wildcard matching.
197
     *
198
     * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#like
199
     *
200
     * @param string|QueryBuilder|Expression $path
201
     * @param string|QueryBuilder|Expression $target
202
     * @return FunctionExpression
203
     */
204 1
    public function like(
205
        mixed $path,
206
        mixed $search
207
    ): FunctionExpression {
208 1
        $arguments = [
209 1
            "path" => $path,
210 1
            "search" => $search,
211
        ];
212
213 1
        return new FunctionExpression('LIKE', $arguments);
214
    }
215
216
    /**
217
     * Match documents with a Damerau-Levenshtein distance lower than or equal to
218
     * the distance between the stored attribute value and target.
219
     *
220
     * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#levenshtein_match
221
     *
222
     * @param string|QueryBuilder|Expression $path
223
     * @param string|QueryBuilder|Expression $target
224
     * @param null|int|float|QueryBuilder|Expression $threshold
225
     * @param null|string|QueryBuilder|Expression $analyzer
226
     * @return FunctionExpression
227
     */
228 2
    public function ngramMatch(
229
        mixed $path,
230
        mixed $target,
231
        mixed $threshold = null,
232
        mixed $analyzer = null
233
    ): FunctionExpression {
234 2
        $arguments = [
235 2
            "path" => $path,
236 2
            "target" => $target,
237 2
            "threshold" => $threshold,
238 2
            "analyzer" => $analyzer
239
        ];
240 2
        $arguments = array_filter(
241 2
            $arguments,
242 2
            function ($value) {
243 2
                return ! is_null($value);
244 2
            }
245
        );
246
247 2
        return new FunctionExpression('NGRAM_MATCH', $arguments);
248
    }
249
250
    /**
251
     * Search for a phrase in the referenced attribute.
252
     *
253
     * https://www.arangodb.com/docs/stable/aql/functions-arangosearch.html#phrase
254
     */
255 3
    public function phrase(): FunctionExpression
256
    {
257 3
        $arguments = func_get_args();
258
259 3
        return new FunctionExpression('PHRASE', $arguments);
260
    }
261
}
262