KeySort.php 2.6 KB

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