KeySort.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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 implements IPrefixable
  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. public function prefixKeys($prefix)
  73. {
  74. $arguments = $this->getArguments();
  75. $arguments[0] = "$prefix{$arguments[0]}";
  76. if (($count = count($arguments)) > 1) {
  77. for ($i = 1; $i < $count; $i++) {
  78. switch ($arguments[$i]) {
  79. case 'BY':
  80. case 'STORE':
  81. $arguments[$i] = "$prefix{$arguments[++$i]}";
  82. break;
  83. case 'GET':
  84. $value = $arguments[++$i];
  85. if ($value !== '#') {
  86. $arguments[$i] = "$prefix$value";
  87. }
  88. break;
  89. case 'LIMIT';
  90. $i += 2;
  91. break;
  92. }
  93. }
  94. }
  95. $this->setRawArguments($arguments);
  96. }
  97. }