Parcourir la source

Dynamically check for the presence of the WITHSCORE modifier with the ZRANGE family of commands.

Daniele Alessandri il y a 14 ans
Parent
commit
9b4a915a52
2 fichiers modifiés avec 23 ajouts et 4 suppressions
  1. 9 4
      lib/Predis/Commands/ZSetRange.php
  2. 14 0
      lib/Predis/Commands/ZSetRangeByScore.php

+ 9 - 4
lib/Predis/Commands/ZSetRange.php

@@ -5,8 +5,6 @@ namespace Predis\Commands;
 use Predis\Iterators\MultiBulkResponseTuple;
 
 class ZSetRange extends Command {
-    private $_withScores = false;
-
     public function getId() {
         return 'ZRANGE';
     }
@@ -32,13 +30,20 @@ class ZSetRange extends Command {
         $finalizedOpts = array();
         if (isset($opts['WITHSCORES'])) {
             $finalizedOpts[] = 'WITHSCORES';
-            $this->_withScores = true;
         }
         return $finalizedOpts;
     }
 
+    protected function withScores() {
+        $arguments = $this->getArguments();
+        if (count($arguments) < 4) {
+            return false;
+        }
+        return strtoupper($arguments[3]) === 'WITHSCORES';
+    }
+
     public function parseResponse($data) {
-        if ($this->_withScores) {
+        if ($this->withScores()) {
             if ($data instanceof \Iterator) {
                 return new MultiBulkResponseTuple($data);
             }

+ 14 - 0
lib/Predis/Commands/ZSetRangeByScore.php

@@ -18,4 +18,18 @@ class ZSetRangeByScore extends ZSetRange {
         }
         return array_merge($finalizedOpts, parent::prepareOptions($options));
     }
+
+    protected function withScores() {
+        $arguments = $this->getArguments();
+        for ($i = 3; $i < count($arguments); $i++) {
+            switch (strtoupper($arguments[$i])) {
+                case 'WITHSCORES':
+                    return true;
+                case 'LIMIT':
+                    $i += 2;
+                    break;
+            }
+        }
+        return false;
+    }
 }