ZSetRange.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  1. <?php
  2. namespace Predis\Commands;
  3. use Predis\Command;
  4. use Predis\Iterators\MultiBulkResponseTuple;
  5. class ZSetRange extends Command {
  6. private $_withScores = false;
  7. public function getCommandId() { return 'ZRANGE'; }
  8. public function filterArguments(Array $arguments) {
  9. if (count($arguments) === 4) {
  10. $lastType = gettype($arguments[3]);
  11. if ($lastType === 'string' && strtolower($arguments[3]) === 'withscores') {
  12. // Used for compatibility with older versions
  13. $arguments[3] = array('WITHSCORES' => true);
  14. $lastType = 'array';
  15. }
  16. if ($lastType === 'array') {
  17. $options = $this->prepareOptions(array_pop($arguments));
  18. return array_merge($arguments, $options);
  19. }
  20. }
  21. return $arguments;
  22. }
  23. protected function prepareOptions($options) {
  24. $opts = array_change_key_case($options, CASE_UPPER);
  25. $finalizedOpts = array();
  26. if (isset($opts['WITHSCORES'])) {
  27. $finalizedOpts[] = 'WITHSCORES';
  28. $this->_withScores = true;
  29. }
  30. return $finalizedOpts;
  31. }
  32. public function parseResponse($data) {
  33. if ($this->_withScores) {
  34. if ($data instanceof \Iterator) {
  35. return new MultiBulkResponseTuple($data);
  36. }
  37. $result = array();
  38. for ($i = 0; $i < count($data); $i++) {
  39. $result[] = array($data[$i], $data[++$i]);
  40. }
  41. return $result;
  42. }
  43. return $data;
  44. }
  45. }