ZSetRange.php 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace Predis\Commands;
  3. use Predis\Iterators\MultiBulkResponseTuple;
  4. class ZSetRange extends Command {
  5. public function getId() {
  6. return 'ZRANGE';
  7. }
  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. }
  29. return $finalizedOpts;
  30. }
  31. protected function withScores() {
  32. $arguments = $this->getArguments();
  33. if (count($arguments) < 4) {
  34. return false;
  35. }
  36. return strtoupper($arguments[3]) === 'WITHSCORES';
  37. }
  38. public function parseResponse($data) {
  39. if ($this->withScores()) {
  40. if ($data instanceof \Iterator) {
  41. return new MultiBulkResponseTuple($data);
  42. }
  43. $result = array();
  44. for ($i = 0; $i < count($data); $i++) {
  45. $result[] = array($data[$i], $data[++$i]);
  46. }
  47. return $result;
  48. }
  49. return $data;
  50. }
  51. }