Skip to content

Commit 057ad8e

Browse files
authored
Merge pull request #88 from Braxilior/zrevrank
Implement zrevrank
2 parents 5f8821e + f3239cf commit 057ad8e

File tree

3 files changed

+35
-1
lines changed

3 files changed

+35
-1
lines changed

README.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,8 @@ Redis command | Description
8383
**ZCARD** *key* | Returns the sorted set cardinality (number of elements) of the sorted set stored at *key*
8484
**ZRANGE** *key* *start* *stop* *[withscores]* | Returns the specified range of members in a sorted set
8585
**ZRANGEBYSCORE** *key* *min* *max* *options* | Returns a range of members in a sorted set, by score
86-
**ZRANK** *key* *member* | Returns the rank of *member* in the sorted set stored at *key*
86+
**ZRANK** *key* *member* | Returns the rank of *member* in the sorted set stored at *key*, with the scores ordered from low to high
87+
**ZREVRANK** *key* *member* | Returns the rank of *member* in the sorted set stored at *key*, with the scores ordered from high to low
8788
**ZREM** *key* *member* | Removes one membner from a sorted set
8889
**ZREMRANGEBYSCORE** *key* *min* *max* | Removes all members in a sorted set within the given scores
8990
**ZREVRANGE** *key* *start* *stop* *[withscores]*| Returns the specified range of members in a sorted set, with scores ordered from high to low

src/M6Web/Component/RedisMock/RedisMock.php

+12
Original file line numberDiff line numberDiff line change
@@ -1048,6 +1048,18 @@ public function zrank($key, $member)
10481048
return $this->returnPipedInfo($rank);
10491049
}
10501050

1051+
public function zrevrank($key, $member)
1052+
{
1053+
$rank = $this->zrank($key, $member);
1054+
if ($rank === null) {
1055+
return $this->returnPipedInfo(null);
1056+
}
1057+
1058+
$revRank = count(self::$dataValues[$this->storage][$key]) - $rank - 1;
1059+
1060+
return $this->returnPipedInfo($revRank);
1061+
}
1062+
10511063
public function zremrangebyscore($key, $min, $max) {
10521064
if (!isset(self::$dataValues[$this->storage][$key]) || $this->deleteOnTtlExpired($key)) {
10531065
return $this->returnPipedInfo(0);

tests/units/RedisMock.php

+21
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,27 @@ public function testZRank()
812812
$this->assert->variable($redisMock->zrank('invalid', 'whatever'))->isEqualTo(null);
813813
}
814814

815+
public function testZRevRank()
816+
{
817+
$redisMock = new Redis();
818+
$redisMock->zadd('test', 4, 'test1');
819+
$redisMock->zadd('test', 15, 'test2');
820+
$redisMock->zadd('test', 2, 'test3');
821+
$redisMock->zadd('test', 1, 'test4');
822+
$redisMock->zadd('test', 30, 'test5');
823+
$redisMock->zadd('test', 0, 'test6');
824+
825+
$this->assert->variable($redisMock->zrevrank('test', 'test5'))->isEqualTo(0);
826+
$this->assert->variable($redisMock->zrevrank('test', 'test2'))->isEqualTo(1);
827+
$this->assert->variable($redisMock->zrevrank('test', 'test1'))->isEqualTo(2);
828+
$this->assert->variable($redisMock->zrevrank('test', 'test3'))->isEqualTo(3);
829+
$this->assert->variable($redisMock->zrevrank('test', 'test4'))->isEqualTo(4);
830+
$this->assert->variable($redisMock->zrevrank('test', 'test6'))->isEqualTo(5);
831+
832+
$this->assert->variable($redisMock->zrevrank('test', 'invalid'))->isEqualTo(null);
833+
$this->assert->variable($redisMock->zrevrank('invalid', 'whatever'))->isEqualTo(null);
834+
}
835+
815836
public function testZRange()
816837
{
817838
$redisMock = new Redis();

0 commit comments

Comments
 (0)