ZSetRange.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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 Command
  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' && strtoupper($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. *
  48. * @return array
  49. */
  50. protected function prepareOptions($options)
  51. {
  52. $opts = array_change_key_case($options, CASE_UPPER);
  53. $finalizedOpts = array();
  54. if (!empty($opts['WITHSCORES'])) {
  55. $finalizedOpts[] = 'WITHSCORES';
  56. }
  57. return $finalizedOpts;
  58. }
  59. /**
  60. * Checks for the presence of the WITHSCORES modifier.
  61. *
  62. * @return bool
  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. $result = array();
  79. for ($i = 0; $i < count($data); $i++) {
  80. $result[$data[$i]] = $data[++$i];
  81. }
  82. return $result;
  83. }
  84. return $data;
  85. }
  86. }