ZSetRange.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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. *
  14. * @author Daniele Alessandri <suppakilla@gmail.com>
  15. */
  16. class ZSetRange extends Command
  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' && strtoupper($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. *
  49. * @return array
  50. */
  51. protected function prepareOptions($options)
  52. {
  53. $opts = array_change_key_case($options, CASE_UPPER);
  54. $finalizedOpts = array();
  55. if (!empty($opts['WITHSCORES'])) {
  56. $finalizedOpts[] = 'WITHSCORES';
  57. }
  58. return $finalizedOpts;
  59. }
  60. /**
  61. * Checks for the presence of the WITHSCORES modifier.
  62. *
  63. * @return bool
  64. */
  65. protected function withScores()
  66. {
  67. $arguments = $this->getArguments();
  68. if (count($arguments) < 4) {
  69. return false;
  70. }
  71. return strtoupper($arguments[3]) === 'WITHSCORES';
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function parseResponse($data)
  77. {
  78. if ($this->withScores()) {
  79. $result = array();
  80. for ($i = 0; $i < count($data); ++$i) {
  81. $result[$data[$i]] = $data[++$i];
  82. }
  83. return $result;
  84. }
  85. return $data;
  86. }
  87. }