ZSetRange.php 1.7 KB

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