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. * @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. } else {
  46. $query[] = 'GET';
  47. $query[] = $getargs;
  48. }
  49. }
  50. if (isset($sortParams['LIMIT']) &&
  51. 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. }