Sort.php 1.5 KB

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