Sort.php 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. <?php
  2. namespace Predis\Commands;
  3. use Predis\Command;
  4. class Sort extends Command {
  5. public function getCommandId() { return 'SORT'; }
  6. public function filterArguments(Array $arguments) {
  7. if (count($arguments) === 1) {
  8. return $arguments;
  9. }
  10. $query = array($arguments[0]);
  11. $sortParams = array_change_key_case($arguments[1], CASE_UPPER);
  12. if (isset($sortParams['BY'])) {
  13. $query[] = 'BY';
  14. $query[] = $sortParams['BY'];
  15. }
  16. if (isset($sortParams['GET'])) {
  17. $getargs = $sortParams['GET'];
  18. if (is_array($getargs)) {
  19. foreach ($getargs as $getarg) {
  20. $query[] = 'GET';
  21. $query[] = $getarg;
  22. }
  23. }
  24. else {
  25. $query[] = 'GET';
  26. $query[] = $getargs;
  27. }
  28. }
  29. if (isset($sortParams['LIMIT']) && is_array($sortParams['LIMIT'])
  30. && count($sortParams['LIMIT']) == 2) {
  31. $query[] = 'LIMIT';
  32. $query[] = $sortParams['LIMIT'][0];
  33. $query[] = $sortParams['LIMIT'][1];
  34. }
  35. if (isset($sortParams['SORT'])) {
  36. $query[] = strtoupper($sortParams['SORT']);
  37. }
  38. if (isset($sortParams['ALPHA']) && $sortParams['ALPHA'] == true) {
  39. $query[] = 'ALPHA';
  40. }
  41. if (isset($sortParams['STORE'])) {
  42. $query[] = 'STORE';
  43. $query[] = $sortParams['STORE'];
  44. }
  45. return $query;
  46. }
  47. }