KeySort.php 2.0 KB

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