ZSetRange.php 2.3 KB

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