Completed
Pull Request — master (#96)
by Roberto
13:50
created

SortedSets::zInterStore()   A

Complexity

Conditions 5
Paths 4

Size

Total Lines 25

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 25
c 0
b 0
f 0
rs 9.2088
cc 5
nc 4
nop 4
1
<?php
2
3
namespace Webdcg\Redis\Traits;
4
5
use Webdcg\Redis\Exceptions\SetOperationException;
6
use Webdcg\Redis\Exceptions\UnsupportedOptionException;
7
8
trait SortedSets
9
{
10
    /*
11
     * Available Set Operations
12
     */
13
    protected $SET_OPERATIONS = ['SUM', 'MIN', 'MAX'];
14
15
    public function bzPop(): bool
16
    {
17
        return false;
18
    }
19
20
    /**
21
     * Add one or more members to a sorted set or update its score if it
22
     * already exists.
23
     *
24
     * See: https://redis.io/commands/zadd.
25
     *
26
     * @param  string $key
27
     * @param  float  $score
28
     * @param  mixed] $member
0 ignored issues
show
Documentation introduced by
The doc-type mixed] could not be parsed: Expected "|" or "end of type", but got "]" at position 5. (view supported doc-types)

This check marks PHPDoc comments that could not be parsed by our parser. To see which comment annotations we can parse, please refer to our documentation on supported doc-types.

Loading history...
29
     *
30
     * @return int              1 if the element is added. 0 otherwise.
31
     */
32
    public function zAdd(string $key, float $score, $member): int
33
    {
34
        return $this->redis->zAdd($key, $score, $member);
0 ignored issues
show
Bug introduced by
The property redis does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
35
    }
36
37
    /**
38
     * Get the number of members in a sorted set.
39
     * See: https://redis.io/commands/zcard.
40
     *
41
     * @param  string $key
42
     *
43
     * @return int          The Set's cardinality
44
     */
45
    public function zCard(string $key): int
46
    {
47
        return $this->redis->zCard($key);
48
    }
49
50
    /**
51
     * Get the number of members in a sorted set.
52
     *
53
     * Note: zSize is an alias for zCard and will be removed in future
54
     * versions of phpredis.
55
     *
56
     * See: https://redis.io/commands/zcard.
57
     *
58
     * @param  string $key
59
     *
60
     * @return int          The Set's cardinality
61
     */
62
    public function zSize(string $key): int
63
    {
64
        return $this->redis->zCard($key);
65
    }
66
67
    /**
68
     * Returns the number of elements of the sorted set stored at the specified
69
     * key which have scores in the range [start, end]. Adding a parenthesis
70
     * before start or end excludes it from the range. +inf and -inf are also
71
     * valid limits.
72
     *
73
     * See: https://redis.io/commands/zcount.
74
     *
75
     * @param  string $key
76
     * @param  mixed|int|string $start
77
     * @param  mixed|int|string $end
78
     *
79
     * @return int                      the size of a corresponding zRangeByScore.
80
     */
81
    public function zCount(string $key, $start, $end): int
82
    {
83
        return $this->redis->zCount($key, $start, $end);
84
    }
85
86
    /**
87
     * Increments the score of a member from a sorted set by a given amount.
88
     *
89
     * See: https://redis.io/commands/zincrby.
90
     *
91
     * @param  string $key
92
     * @param  float  $value    (double) value that will be added to the
93
     *                          member's score).
94
     * @param  string $member
95
     *
96
     * @return float            the new value
97
     */
98
    public function zIncrBy(string $key, float $value, $member): float
99
    {
100
        return $this->redis->zIncrBy($key, $value, $member);
101
    }
102
103
    /**
104
     * Creates an intersection of sorted sets given in second argument.
105
     * The result of the union will be stored in the sorted set defined by the
106
     * first argument.
107
     *
108
     * The third optional argument defines weights to apply to the sorted sets
109
     * in input. In this case, the weights will be multiplied by the score of
110
     * each element in the sorted set before applying the aggregation. The
111
     * forth argument defines the AGGREGATE option which specify how the
112
     * results of the union are aggregated.
113
     *
114
     * See: https://redis.io/commands/zinterstore.
115
     *
116
     * @param  string $keyOutput
117
     * @param  array  $arrayZSetKeys
118
     * @param  array  $arrayWeights
119
     * @param  string $aggregateFunction    Either "SUM", "MIN", or "MAX":
120
     *                                      defines the behaviour to use on
121
     *                                      duplicate entries during the
122
     *                                      zInterStore.
123
     *
124
     * @return int                          The number of values in the new
125
     *                                      sorted set.
126
     */
127
    public function zInterStore(
128
        string $keyOutput,
129
        array $arrayZSetKeys,
130
        ?array $arrayWeights = null,
131
        ?string $aggregateFunction = null
132
    ): int {
133
        // Validate Aggregate Function
134
        if (!is_null($aggregateFunction) && !is_null($arrayWeights)) {
135
            $operation = strtoupper($aggregateFunction);
136
137
            if (!in_array($operation, $this->SET_OPERATIONS)) {
138
                throw new SetOperationException('Operation not supported', 1);
139
            }
140
141
            return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights, $operation);
142
        }
143
144
        // Call using Weights
145
        if (!is_null($arrayWeights)) {
146
            return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights);
147
        }
148
149
        // Make simplest call just with the required params
150
        return $this->redis->zInterStore($keyOutput, $arrayZSetKeys);
151
    }
152
153
    /**
154
     * Creates an intersection of sorted sets given in second argument.
155
     * The result of the union will be stored in the sorted set defined by the
156
     * first argument.
157
     *
158
     * The third optional argument defines weights to apply to the sorted sets
159
     * in input. In this case, the weights will be multiplied by the score of
160
     * each element in the sorted set before applying the aggregation. The
161
     * forth argument defines the AGGREGATE option which specify how the
162
     * results of the union are aggregated.
163
     *
164
     * Note: zInter is an alias for zinterstore and will be removed in future
165
     * versions of phpredis.
166
     *
167
     * See: https://redis.io/commands/zinterstore.
168
     *
169
     * @param  string $keyOutput
170
     * @param  array  $arrayZSetKeys
171
     * @param  array  $arrayWeights
172
     * @param  string $aggregateFunction    Either "SUM", "MIN", or "MAX":
173
     *                                      defines the behaviour to use on
174
     *                                      duplicate entries during the
175
     *                                      zInterStore.
176
     *
177
     * @return int                          The number of values in the new
178
     *                                      sorted set.
179
     */
180
    public function zInter(
181
        string $keyOutput,
182
        array $arrayZSetKeys,
183
        ?array $arrayWeights = null,
184
        ?string $aggregateFunction = null
185
    ): int {
186
        // Validate Aggregate Function
187
        if (!is_null($aggregateFunction) && !is_null($arrayWeights)) {
188
            $operation = strtoupper($aggregateFunction);
189
190
            if (!in_array($operation, $this->SET_OPERATIONS)) {
191
                throw new SetOperationException('Operation not supported', 1);
192
            }
193
194
            return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights, $operation);
195
        }
196
197
        // Call using Weights
198
        if (!is_null($arrayWeights)) {
199
            return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights);
200
        }
201
202
        // Make simplest call just with the required params
203
        return $this->redis->zInterStore($keyOutput, $arrayZSetKeys);
204
    }
205
206
    /**
207
     * Can pop the highest or lowest scoring members from one ZSETs.
208
     * There are two commands (ZPOPMIN and ZPOPMAX for popping the lowest and
209
     * highest scoring elements respectively.).
210
     *
211
     * See: https://redis.io/commands/zpopmin.
212
     * See: https://redis.io/commands/zpopmax.
213
     *
214
     * @param  string       $key
215
     * @param  int|integer  $count
216
     * @param  bool|boolean $max
217
     *
218
     * @return array                Either an array with the key member and
219
     *                              score of the highest or lowest element
220
     *                              or an empty array if there is no element
221
     *                              available.
222
     */
223
    public function zPop(string $key, int $count = 1, bool $max = true): array
224
    {
225
        return $max ? $this->redis->zPopMax($key, $count) : $this->redis->zPopMin($key, $count);
226
    }
227
228
    /**
229
     * Can pop the lowest scoring members from one ZSETs.
230
     *
231
     * See: https://redis.io/commands/zpopmin.
232
     *
233
     * @param  string      $key
234
     * @param  int|integer $count
235
     *
236
     * @return array                Either an array with the key member and
237
     *                              score of the highest or lowest element
238
     *                              or an empty array if there is no element
239
     *                              available.
240
     */
241
    public function zPopMin(string $key, int $count = 1): array
242
    {
243
        return $this->redis->zPopMin($key, $count);
244
    }
245
246
    /**
247
     * Can pop the highest scoring members from one ZSETs.
248
     *
249
     * See: https://redis.io/commands/zpopmax.
250
     *
251
     * @param  string      $key
252
     * @param  int|integer $count
253
     *
254
     * @return array                Either an array with the key member and
255
     *                              score of the highest or lowest element
256
     *                              or an empty array if there is no element
257
     *                              available.
258
     */
259
    public function zPopMax(string $key, int $count = 1): array
260
    {
261
        return $this->redis->zPopMax($key, $count);
262
    }
263
264
    /**
265
     * Returns a range of elements from the ordered set stored at the specified
266
     * key, with values in the range [start, end].
267
     *
268
     * Start and stop are interpreted as zero-based indices:
269
     *     0 the first element, 1 the second ...
270
     *     -1 the last element, -2 the penultimate ...
271
     *
272
     * See: https://redis.io/commands/zrange.
273
     *
274
     * @param  string       $key
275
     * @param  int|integer  $start
276
     * @param  int|integer  $end
277
     * @param  bool|boolean $withScores
278
     *
279
     * @return array                    Array containing the values in specified range.
280
     */
281
    public function zRange(string $key, int $start = 0, int $end = -1, bool $withScores = false): array
282
    {
283
        return $this->redis->zRange($key, $start, $end, $withScores);
284
    }
285
286
    /**
287
     * Returns the elements of the sorted set stored at the specified key which
288
     * have scores in the range [start,end]. Adding a parenthesis before start
289
     * or end excludes it from the range. +inf and -inf are also valid limits.
290
     *
291
     * See: https://redis.io/commands/zrangebyscore.
292
     *
293
     * @param  string     $key
294
     * @param  mixed|int|string     $start
295
     * @param  mixed|int|string     $end
296
     * @param  array|null           $options  Two options are available:
297
     *                                        - withscores => TRUE,
298
     *                                        and limit => [$offset, $count]
299
     *
300
     * @return array                        Array containing the values in
301
     *                                      specified range.
302
     */
303
    public function zRangeByScore(string $key, $start, $end, ?array $options = null): array
304
    {
305
        if (is_null($options)) {
306
            return $this->redis->zRangeByScore($key, $start, $end);
307
        }
308
309
        $rangeOptions = ['withscores', 'limit'];
310
311
        if (count(array_intersect(array_keys($options), $rangeOptions)) != count($options)) {
312
            throw new UnsupportedOptionException("Option Not Supported", 1);
313
        }
314
315
        return $this->redis->zRangeByScore($key, $start, $end, $options);
316
    }
317
318
    /**
319
     * Returns the elements of the sorted set stored at the specified key which
320
     * have scores in the range [start,end]. Adding a parenthesis before start
321
     * or end excludes it from the range. +inf and -inf are also valid limits.
322
     *
323
     * zRevRangeByScore returns the same items in reverse order, when the start
324
     * and end parameters are swapped.
325
     *
326
     * See: https://redis.io/commands/zrevrangebyscore.
327
     *
328
     * @param  string     $key
329
     * @param  mixed|int|string     $start
330
     * @param  mixed|int|string     $end
331
     * @param  array|null           $options  Two options are available:
332
     *                                        - withscores => TRUE,
333
     *                                        and limit => [$offset, $count]
334
     *
335
     * @return array                        Array containing the values in
336
     *                                      specified range.
337
     */
338
    public function zRevRangeByScore(string $key, $start, $end, ?array $options = null): array
339
    {
340
        if (is_null($options)) {
341
            return $this->redis->zRevRangeByScore($key, $start, $end);
342
        }
343
344
        $rangeOptions = ['withscores', 'limit'];
345
346
        if (count(array_intersect(array_keys($options), $rangeOptions)) != count($options)) {
347
            throw new UnsupportedOptionException("Option Not Supported", 1);
348
        }
349
350
        return $this->redis->zRevRangeByScore($key, $start, $end, $options);
351
    }
352
353
    public function zRangeByLex(): bool
354
    {
355
        return false;
356
    }
357
358
    public function zRank(): bool
359
    {
360
        return false;
361
    }
362
363
    public function zRevRank(): bool
364
    {
365
        return false;
366
    }
367
368
    public function zRem(): bool
369
    {
370
        return false;
371
    }
372
373
    public function zDelete(): bool
374
    {
375
        return false;
376
    }
377
378
    public function zRemove(): bool
379
    {
380
        return false;
381
    }
382
383
    public function zRemRangeByRank(): bool
384
    {
385
        return false;
386
    }
387
388
    public function zDeleteRangeByRank(): bool
389
    {
390
        return false;
391
    }
392
393
    public function zRemRangeByScore(): bool
394
    {
395
        return false;
396
    }
397
398
    public function zDeleteRangeByScore(): bool
399
    {
400
        return false;
401
    }
402
403
    public function zRemoveRangeByScore(): bool
404
    {
405
        return false;
406
    }
407
408
    public function zRevRange(): bool
409
    {
410
        return false;
411
    }
412
413
    public function zScore(): bool
414
    {
415
        return false;
416
    }
417
418
    public function zunionstore(): bool
419
    {
420
        return false;
421
    }
422
423
    public function zUnion(): bool
424
    {
425
        return false;
426
    }
427
428
    public function zScan(): bool
429
    {
430
        return false;
431
    }
432
}
433