Completed
Pull Request — master (#96)
by Roberto
17:40 queued 03:06
created

SortedSets::zPopMax()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Webdcg\Redis\Traits;
4
5
use Webdcg\Redis\Exceptions\SetOperationException;
6
7
trait SortedSets
8
{
9
    /*
10
     * Available Set Operations
11
     */
12
    protected $SET_OPERATIONS = ['SUM', 'MIN', 'MAX'];
13
14
    public function bzPop(): bool
15
    {
16
        return false;
17
    }
18
19
    /**
20
     * Add one or more members to a sorted set or update its score if it
21
     * already exists.
22
     * See: https://redis.io/commands/zadd.
23
     *
24
     * @param  string $key
25
     * @param  float  $score
26
     * @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...
27
     *
28
     * @return int              1 if the element is added. 0 otherwise.
29
     */
30
    public function zAdd(string $key, float $score, $member): int
31
    {
32
        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...
33
    }
34
35
    /**
36
     * Get the number of members in a sorted set.
37
     * See: https://redis.io/commands/zcard.
38
     *
39
     * @param  string $key
40
     *
41
     * @return int          The Set's cardinality
42
     */
43
    public function zCard(string $key): int
44
    {
45
        return $this->redis->zCard($key);
46
    }
47
48
    /**
49
     * Get the number of members in a sorted set.
50
     * Note: zSize is an alias for zCard and will be removed in future
51
     * versions of phpredis.
52
     * See: https://redis.io/commands/zcard.
53
     *
54
     * @param  string $key
55
     *
56
     * @return int          The Set's cardinality
57
     */
58
    public function zSize(string $key): int
59
    {
60
        return $this->redis->zCard($key);
61
    }
62
63
    /**
64
     * Returns the number of elements of the sorted set stored at the specified
65
     * key which have scores in the range [start, end]. Adding a parenthesis
66
     * before start or end excludes it from the range. +inf and -inf are also
67
     * valid limits.
68
     * See: https://redis.io/commands/zcount.
69
     *
70
     * @param  string $key
71
     * @param  mixed|int|string $start
72
     * @param  mixed|int|string $end
73
     *
74
     * @return int                      the size of a corresponding zRangeByScore.
75
     */
76
    public function zCount(string $key, $start, $end): int
77
    {
78
        return $this->redis->zCount($key, $start, $end);
79
    }
80
81
    /**
82
     * Increments the score of a member from a sorted set by a given amount.
83
     * See: https://redis.io/commands/zincrby.
84
     *
85
     * @param  string $key
86
     * @param  float  $value    (double) value that will be added to the
87
     *                          member's score).
88
     * @param  string $member
89
     *
90
     * @return float            the new value
91
     */
92
    public function zIncrBy(string $key, float $value, $member): float
93
    {
94
        return $this->redis->zIncrBy($key, $value, $member);
95
    }
96
97
    /**
98
     * Creates an intersection of sorted sets given in second argument.
99
     * The result of the union will be stored in the sorted set defined by the
100
     * first argument.
101
     * The third optional argument defines weights to apply to the sorted sets
102
     * in input. In this case, the weights will be multiplied by the score of
103
     * each element in the sorted set before applying the aggregation. The
104
     * forth argument defines the AGGREGATE option which specify how the
105
     * results of the union are aggregated.
106
     * See: https://redis.io/commands/zinterstore.
107
     *
108
     * @param  string $keyOutput
109
     * @param  array  $arrayZSetKeys
110
     * @param  array  $arrayWeights
111
     * @param  string $aggregateFunction    Either "SUM", "MIN", or "MAX":
112
     *                                      defines the behaviour to use on
113
     *                                      duplicate entries during the
114
     *                                      zInterStore.
115
     *
116
     * @return int                          The number of values in the new
117
     *                                      sorted set.
118
     */
119
    public function zInterStore(
120
        string $keyOutput,
121
        array $arrayZSetKeys,
122
        ?array $arrayWeights = null,
123
        ?string $aggregateFunction = null
124
    ): int {
125
        // Validate Aggregate Function
126
        if (!is_null($aggregateFunction) && !is_null($arrayWeights)) {
127
            $operation = strtoupper($aggregateFunction);
128
129
            if (!in_array($operation, $this->SET_OPERATIONS)) {
130
                throw new SetOperationException('Operation not supported', 1);
131
            }
132
133
            return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights, $operation);
134
        }
135
136
        // Call using Weights
137
        if (!is_null($arrayWeights)) {
138
            return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights);
139
        }
140
141
        // Make simplest call just with the required params
142
        return $this->redis->zInterStore($keyOutput, $arrayZSetKeys);
143
    }
144
145
    /**
146
     * Creates an intersection of sorted sets given in second argument.
147
     * The result of the union will be stored in the sorted set defined by the
148
     * first argument.
149
     * The third optional argument defines weights to apply to the sorted sets
150
     * in input. In this case, the weights will be multiplied by the score of
151
     * each element in the sorted set before applying the aggregation. The
152
     * forth argument defines the AGGREGATE option which specify how the
153
     * results of the union are aggregated.
154
     * Note: zInter is an alias for zinterstore and will be removed in future
155
     * versions of phpredis.
156
     * See: https://redis.io/commands/zinterstore.
157
     *
158
     * @param  string $keyOutput
159
     * @param  array  $arrayZSetKeys
160
     * @param  array  $arrayWeights
161
     * @param  string $aggregateFunction    Either "SUM", "MIN", or "MAX":
162
     *                                      defines the behaviour to use on
163
     *                                      duplicate entries during the
164
     *                                      zInterStore.
165
     *
166
     * @return int                          The number of values in the new
167
     *                                      sorted set.
168
     */
169
    public function zInter(
170
        string $keyOutput,
171
        array $arrayZSetKeys,
172
        ?array $arrayWeights = null,
173
        ?string $aggregateFunction = null
174
    ): int {
175
        // Validate Aggregate Function
176
        if (!is_null($aggregateFunction) && !is_null($arrayWeights)) {
177
            $operation = strtoupper($aggregateFunction);
178
179
            if (!in_array($operation, $this->SET_OPERATIONS)) {
180
                throw new SetOperationException('Operation not supported', 1);
181
            }
182
183
            return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights, $operation);
184
        }
185
186
        // Call using Weights
187
        if (!is_null($arrayWeights)) {
188
            return $this->redis->zInterStore($keyOutput, $arrayZSetKeys, $arrayWeights);
189
        }
190
191
        // Make simplest call just with the required params
192
        return $this->redis->zInterStore($keyOutput, $arrayZSetKeys);
193
    }
194
195
    /**
196
     * Can pop the highest or lowest scoring members from one ZSETs.
197
     * There are two commands (ZPOPMIN and ZPOPMAX for popping the lowest and
198
     * highest scoring elements respectively.).
199
     * See: https://redis.io/commands/zpopmin.
200
     * See: https://redis.io/commands/zpopmax.
201
     *
202
     * @param  string       $key
203
     * @param  int|integer  $count
204
     * @param  bool|boolean $max
205
     *
206
     * @return array                Either an array with the key member and
207
     *                              score of the highest or lowest element
208
     *                              or an empty array if there is no element
209
     *                              available.
210
     */
211
    public function zPop(string $key, int $count = 1, bool $max = true): array
212
    {
213
        return $max ? $this->redis->zPopMax($key, $count) : $this->redis->zPopMin($key, $count);
214
    }
215
216
    /**
217
     * Can pop the lowest scoring members from one ZSETs.
218
     * See: https://redis.io/commands/zpopmin.
219
     *
220
     * @param  string      $key
221
     * @param  int|integer $count
222
     *
223
     * @return array                Either an array with the key member and
224
     *                              score of the highest or lowest element
225
     *                              or an empty array if there is no element
226
     *                              available.
227
     */
228
    public function zPopMin(string $key, int $count = 1): array
229
    {
230
        return $this->redis->zPopMin($key, $count);
231
    }
232
233
    /**
234
     * Can pop the highest scoring members from one ZSETs.
235
     * See: https://redis.io/commands/zpopmax.
236
     *
237
     * @param  string      $key
238
     * @param  int|integer $count
239
     *
240
     * @return array                Either an array with the key member and
241
     *                              score of the highest or lowest element
242
     *                              or an empty array if there is no element
243
     *                              available.
244
     */
245
    public function zPopMax(string $key, int $count = 1): array
246
    {
247
        return $this->redis->zPopMax($key, $count);
248
    }
249
250
    public function zRange(): bool
251
    {
252
        return false;
253
    }
254
255
    public function zRangeByScore(): bool
256
    {
257
        return false;
258
    }
259
260
    public function zRevRangeByScore(): bool
261
    {
262
        return false;
263
    }
264
265
    public function zRangeByLex(): bool
266
    {
267
        return false;
268
    }
269
270
    public function zRank(): bool
271
    {
272
        return false;
273
    }
274
275
    public function zRevRank(): bool
276
    {
277
        return false;
278
    }
279
280
    public function zRem(): bool
281
    {
282
        return false;
283
    }
284
285
    public function zDelete(): bool
286
    {
287
        return false;
288
    }
289
290
    public function zRemove(): bool
291
    {
292
        return false;
293
    }
294
295
    public function zRemRangeByRank(): bool
296
    {
297
        return false;
298
    }
299
300
    public function zDeleteRangeByRank(): bool
301
    {
302
        return false;
303
    }
304
305
    public function zRemRangeByScore(): bool
306
    {
307
        return false;
308
    }
309
310
    public function zDeleteRangeByScore(): bool
311
    {
312
        return false;
313
    }
314
315
    public function zRemoveRangeByScore(): bool
316
    {
317
        return false;
318
    }
319
320
    public function zRevRange(): bool
321
    {
322
        return false;
323
    }
324
325
    public function zScore(): bool
326
    {
327
        return false;
328
    }
329
330
    public function zunionstore(): bool
331
    {
332
        return false;
333
    }
334
335
    public function zUnion(): bool
336
    {
337
        return false;
338
    }
339
340
    public function zScan(): bool
341
    {
342
        return false;
343
    }
344
}
345