KeySort.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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\Command;
  11. /**
  12. * @link http://redis.io/commands/sort
  13. *
  14. * @author Daniele Alessandri <suppakilla@gmail.com>
  15. */
  16. class KeySort extends Command
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getId()
  22. {
  23. return 'SORT';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function filterArguments(array $arguments)
  29. {
  30. if (count($arguments) === 1) {
  31. return $arguments;
  32. }
  33. $query = array($arguments[0]);
  34. $sortParams = array_change_key_case($arguments[1], CASE_UPPER);
  35. if (isset($sortParams['BY'])) {
  36. $query[] = 'BY';
  37. $query[] = $sortParams['BY'];
  38. }
  39. if (isset($sortParams['GET'])) {
  40. $getargs = $sortParams['GET'];
  41. if (is_array($getargs)) {
  42. foreach ($getargs as $getarg) {
  43. $query[] = 'GET';
  44. $query[] = $getarg;
  45. }
  46. } else {
  47. $query[] = 'GET';
  48. $query[] = $getargs;
  49. }
  50. }
  51. if (isset($sortParams['LIMIT']) &&
  52. is_array($sortParams['LIMIT']) &&
  53. count($sortParams['LIMIT']) == 2) {
  54. $query[] = 'LIMIT';
  55. $query[] = $sortParams['LIMIT'][0];
  56. $query[] = $sortParams['LIMIT'][1];
  57. }
  58. if (isset($sortParams['SORT'])) {
  59. $query[] = strtoupper($sortParams['SORT']);
  60. }
  61. if (isset($sortParams['ALPHA']) && $sortParams['ALPHA'] == true) {
  62. $query[] = 'ALPHA';
  63. }
  64. if (isset($sortParams['STORE'])) {
  65. $query[] = 'STORE';
  66. $query[] = $sortParams['STORE'];
  67. }
  68. return $query;
  69. }
  70. }