ZSetRange.php 1.5 KB

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