1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace RedisProxy; |
4
|
|
|
|
5
|
|
|
trait SortedSetBehavior |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Add one or more members to a sorted set, or update its score if it already exists |
9
|
|
|
* @param string $key |
10
|
|
|
* @param array $dictionary (score1, member1[, score2, member2]) or associative array: [member1 => score1, member2 => score2] |
11
|
|
|
* @return int |
12
|
|
|
*/ |
13
|
20 |
|
public function zadd($key, ...$dictionary) |
14
|
|
|
{ |
15
|
20 |
|
$this->init(); |
|
|
|
|
16
|
20 |
|
if (is_array($dictionary[0])) { |
17
|
12 |
|
$return = 0; |
18
|
12 |
|
foreach ($dictionary[0] as $member => $score) { |
19
|
12 |
|
$res = $this->zadd($key, $score, $member); |
20
|
12 |
|
$return += $res; |
21
|
|
|
} |
22
|
12 |
|
return $return; |
23
|
|
|
} |
24
|
20 |
|
return $this->driver->zadd($key, ...$dictionary); |
|
|
|
|
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Return a range of members in a sorted set, by index |
29
|
|
|
* @param string $key |
30
|
|
|
* @param int $start |
31
|
|
|
* @param int $stop |
32
|
|
|
* @param boolean $withscores |
33
|
|
|
* @return array |
34
|
|
|
*/ |
35
|
4 |
View Code Duplication |
public function zrange($key, $start, $stop, $withscores = false) |
36
|
|
|
{ |
37
|
4 |
|
$this->init(); |
|
|
|
|
38
|
4 |
|
if ($this->actualDriver() === self::DRIVER_PREDIS) { |
|
|
|
|
39
|
2 |
|
return $this->driver->zrange($key, $start, $stop, ['WITHSCORES' => $withscores]); |
40
|
|
|
} |
41
|
2 |
|
return $this->driver->zrange($key, $start, $stop, $withscores); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Return a range of members in a sorted set, by index, with scores ordered from high to low |
46
|
|
|
* @param string $key |
47
|
|
|
* @param int $start |
48
|
|
|
* @param int $stop |
49
|
|
|
* @param boolean $withscores |
50
|
|
|
* @return array |
51
|
|
|
*/ |
52
|
4 |
View Code Duplication |
public function zrevrange($key, $start, $stop, $withscores = false) |
53
|
|
|
{ |
54
|
4 |
|
$this->init(); |
|
|
|
|
55
|
4 |
|
if ($this->actualDriver() === self::DRIVER_PREDIS) { |
|
|
|
|
56
|
2 |
|
return $this->driver->zrevrange($key, $start, $stop, ['WITHSCORES' => $withscores]); |
57
|
|
|
} |
58
|
2 |
|
return $this->driver->zrevrange($key, $start, $stop, $withscores); |
59
|
|
|
} |
60
|
|
|
} |
61
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.