Sort.php 1.5 KB

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