ZSetRange.php 1.5 KB

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