ZSetRange.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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\Command;
  11. /**
  12. * @link http://redis.io/commands/zrange
  13. * @author Daniele Alessandri <suppakilla@gmail.com>
  14. */
  15. class ZSetRange extends PrefixableCommand
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function getId()
  21. {
  22. return 'ZRANGE';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function filterArguments(Array $arguments)
  28. {
  29. if (count($arguments) === 4) {
  30. $lastType = gettype($arguments[3]);
  31. if ($lastType === 'string' && strtolower($arguments[3]) === 'withscores') {
  32. // Used for compatibility with older versions
  33. $arguments[3] = array('WITHSCORES' => true);
  34. $lastType = 'array';
  35. }
  36. if ($lastType === 'array') {
  37. $options = $this->prepareOptions(array_pop($arguments));
  38. return array_merge($arguments, $options);
  39. }
  40. }
  41. return $arguments;
  42. }
  43. /**
  44. * Returns a list of options and modifiers compatible with Redis.
  45. *
  46. * @param array $options List of options.
  47. * @return array
  48. */
  49. protected function prepareOptions($options)
  50. {
  51. $opts = array_change_key_case($options, CASE_UPPER);
  52. $finalizedOpts = array();
  53. if (isset($opts['WITHSCORES'])) {
  54. $finalizedOpts[] = 'WITHSCORES';
  55. }
  56. return $finalizedOpts;
  57. }
  58. /**
  59. * Checks for the presence of the WITHSCORES modifier.
  60. *
  61. * @return Boolean
  62. */
  63. protected function withScores()
  64. {
  65. $arguments = $this->getArguments();
  66. if (count($arguments) < 4) {
  67. return false;
  68. }
  69. return strtoupper($arguments[3]) === 'WITHSCORES';
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function parseResponse($data)
  75. {
  76. if ($this->withScores()) {
  77. $result = array();
  78. for ($i = 0; $i < count($data); $i++) {
  79. $result[] = array($data[$i], $data[++$i]);
  80. }
  81. return $result;
  82. }
  83. return $data;
  84. }
  85. }