ZSetRange.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. class ZSetRange extends Command
  13. {
  14. public function getId()
  15. {
  16. return 'ZRANGE';
  17. }
  18. protected function filterArguments(Array $arguments)
  19. {
  20. if (count($arguments) === 4) {
  21. $lastType = gettype($arguments[3]);
  22. if ($lastType === 'string' && strtolower($arguments[3]) === 'withscores') {
  23. // Used for compatibility with older versions
  24. $arguments[3] = array('WITHSCORES' => true);
  25. $lastType = 'array';
  26. }
  27. if ($lastType === 'array') {
  28. $options = $this->prepareOptions(array_pop($arguments));
  29. return array_merge($arguments, $options);
  30. }
  31. }
  32. return $arguments;
  33. }
  34. protected function prepareOptions($options)
  35. {
  36. $opts = array_change_key_case($options, CASE_UPPER);
  37. $finalizedOpts = array();
  38. if (isset($opts['WITHSCORES'])) {
  39. $finalizedOpts[] = 'WITHSCORES';
  40. }
  41. return $finalizedOpts;
  42. }
  43. protected function withScores()
  44. {
  45. $arguments = $this->getArguments();
  46. if (count($arguments) < 4) {
  47. return false;
  48. }
  49. return strtoupper($arguments[3]) === 'WITHSCORES';
  50. }
  51. public function parseResponse($data)
  52. {
  53. if ($this->withScores()) {
  54. if ($data instanceof \Iterator) {
  55. return new MultiBulkResponseTuple($data);
  56. }
  57. $result = array();
  58. for ($i = 0; $i < count($data); $i++) {
  59. $result[] = array($data[$i], $data[++$i]);
  60. }
  61. return $result;
  62. }
  63. return $data;
  64. }
  65. }