PageRenderTime 42ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/library/Rediska/Command/GetFromSortedSetByScore.php

https://bitbucket.org/ilyabazhenov/speakplace
PHP | 83 lines | 39 code | 11 blank | 33 comment | 6 complexity | 4c0b93ae8d81e07a8a616c26c687458f MD5 | raw file
  1. <?php
  2. /**
  3. * Get members from sorted set by min and max score
  4. *
  5. * @author Ivan Shumkov
  6. * @package Rediska
  7. * @subpackage Commands
  8. * @version 0.5.7
  9. * @link http://rediska.geometria-lab.net
  10. * @license http://www.opensource.org/licenses/bsd-license.php
  11. */
  12. class Rediska_Command_GetFromSortedSetByScore extends Rediska_Command_Abstract
  13. {
  14. /**
  15. * Supported version
  16. *
  17. * @var string
  18. */
  19. protected $_version = '1.1';
  20. /**
  21. * Create command
  22. *
  23. * @param string $key Key name
  24. * @param number $min Min score
  25. * @param number $max Max score
  26. * @param boolean[optional] $withScores Get with scores. For default is false
  27. * @param integer[optional] $limit Limit. For default is no limit
  28. * @param integer[optional] $offset Offset. For default is no offset
  29. * @param boolean[optional] $revert Revert. For default is false
  30. * @return Rediska_Connection_Exec
  31. */
  32. public function create($key, $min, $max, $withScores = false, $limit = null, $offset = null, $revert = false)
  33. {
  34. if ($revert) {
  35. $this->_throwExceptionIfNotSupported('2.1.6');
  36. }
  37. $connection = $this->_rediska->getConnectionByKeyName($key);
  38. $command = array($revert ? 'ZREVRANGEBYSCORE' : 'ZRANGEBYSCORE',
  39. $this->_rediska->getOption('namespace') . $key,
  40. $revert ? $max : $min,
  41. $revert ? $min : $max);
  42. if (!is_null($limit)) {
  43. if (is_null($offset)) {
  44. $offset = 0;
  45. }
  46. $command[] = 'LIMIT';
  47. $command[] = $offset;
  48. $command[] = $limit;
  49. }
  50. if ($withScores) {
  51. $this->_throwExceptionIfNotSupported('1.3.4');
  52. $command[] = 'WITHSCORES';
  53. }
  54. return new Rediska_Connection_Exec($connection, $command);
  55. }
  56. /**
  57. * Parse response
  58. *
  59. * @param array $response
  60. * @return array
  61. */
  62. public function parseResponse($response)
  63. {
  64. $values = $response;
  65. if ($this->withScores) {
  66. $values = Rediska_Command_Response_ValueAndScore::combine($this->_rediska, $values);
  67. } else {
  68. $values = array_map(array($this->_rediska->getSerializer(), 'unserialize'), $values);
  69. }
  70. return $values;
  71. }
  72. }