KeySort.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  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. /**
  12. * @link http://redis.io/commands/sort
  13. * @author Daniele Alessandri <suppakilla@gmail.com>
  14. */
  15. class KeySort extends Command
  16. {
  17. /**
  18. * {@inheritdoc}
  19. */
  20. public function getId()
  21. {
  22. return 'SORT';
  23. }
  24. /**
  25. * {@inheritdoc}
  26. */
  27. protected function filterArguments(Array $arguments)
  28. {
  29. if (count($arguments) === 1) {
  30. return $arguments;
  31. }
  32. $query = array($arguments[0]);
  33. $sortParams = array_change_key_case($arguments[1], CASE_UPPER);
  34. if (isset($sortParams['BY'])) {
  35. $query[] = 'BY';
  36. $query[] = $sortParams['BY'];
  37. }
  38. if (isset($sortParams['GET'])) {
  39. $getargs = $sortParams['GET'];
  40. if (is_array($getargs)) {
  41. foreach ($getargs as $getarg) {
  42. $query[] = 'GET';
  43. $query[] = $getarg;
  44. }
  45. }
  46. else {
  47. $query[] = 'GET';
  48. $query[] = $getargs;
  49. }
  50. }
  51. if (isset($sortParams['LIMIT']) && is_array($sortParams['LIMIT'])
  52. && count($sortParams['LIMIT']) == 2) {
  53. $query[] = 'LIMIT';
  54. $query[] = $sortParams['LIMIT'][0];
  55. $query[] = $sortParams['LIMIT'][1];
  56. }
  57. if (isset($sortParams['SORT'])) {
  58. $query[] = strtoupper($sortParams['SORT']);
  59. }
  60. if (isset($sortParams['ALPHA']) && $sortParams['ALPHA'] == true) {
  61. $query[] = 'ALPHA';
  62. }
  63. if (isset($sortParams['STORE'])) {
  64. $query[] = 'STORE';
  65. $query[] = $sortParams['STORE'];
  66. }
  67. return $query;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. protected function onPrefixKeys(Array $arguments, $prefix)
  73. {
  74. $arguments[0] = "$prefix{$arguments[0]}";
  75. if (($count = count($arguments)) > 1) {
  76. for ($i = 1; $i < $count; $i++) {
  77. switch ($arguments[$i]) {
  78. case 'BY':
  79. case 'STORE':
  80. $arguments[$i] = "$prefix{$arguments[++$i]}";
  81. break;
  82. case 'GET':
  83. $value = $arguments[++$i];
  84. if ($value !== '#') {
  85. $arguments[$i] = "$prefix$value";
  86. }
  87. break;
  88. case 'LIMIT';
  89. $i += 2;
  90. break;
  91. }
  92. }
  93. }
  94. return $arguments;
  95. }
  96. }