ZSetRange.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. <?php
  2. /*
  3. * This file is part of the Predis package.
  4. *
  5. * (c) Daniele Alessandri <suppakilla@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Predis\Commands;
  11. use Predis\Iterators\MultiBulkResponseTuple;
  12. /**
  13. * @link http://redis.io/commands/zrange
  14. * @author Daniele Alessandri <suppakilla@gmail.com>
  15. */
  16. class ZSetRange extends PrefixableCommand
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getId()
  22. {
  23. return 'ZRANGE';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function filterArguments(Array $arguments)
  29. {
  30. if (count($arguments) === 4) {
  31. $lastType = gettype($arguments[3]);
  32. if ($lastType === 'string' && strtolower($arguments[3]) === 'withscores') {
  33. // Used for compatibility with older versions
  34. $arguments[3] = array('WITHSCORES' => true);
  35. $lastType = 'array';
  36. }
  37. if ($lastType === 'array') {
  38. $options = $this->prepareOptions(array_pop($arguments));
  39. return array_merge($arguments, $options);
  40. }
  41. }
  42. return $arguments;
  43. }
  44. /**
  45. * Returns a list of options and modifiers compatible with Redis.
  46. *
  47. * @param array $options List of options.
  48. * @return array
  49. */
  50. protected function prepareOptions($options)
  51. {
  52. $opts = array_change_key_case($options, CASE_UPPER);
  53. $finalizedOpts = array();
  54. if (isset($opts['WITHSCORES'])) {
  55. $finalizedOpts[] = 'WITHSCORES';
  56. }
  57. return $finalizedOpts;
  58. }
  59. /**
  60. * Checks for the presence of the WITHSCORES modifier.
  61. *
  62. * @return Boolean
  63. */
  64. protected function withScores()
  65. {
  66. $arguments = $this->getArguments();
  67. if (count($arguments) < 4) {
  68. return false;
  69. }
  70. return strtoupper($arguments[3]) === 'WITHSCORES';
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function parseResponse($data)
  76. {
  77. if ($this->withScores()) {
  78. if ($data instanceof \Iterator) {
  79. return new MultiBulkResponseTuple($data);
  80. }
  81. $result = array();
  82. for ($i = 0; $i < count($data); $i++) {
  83. $result[] = array($data[$i], $data[++$i]);
  84. }
  85. return $result;
  86. }
  87. return $data;
  88. }
  89. }