ZSetUnionStore.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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/zunionstore
  13. *
  14. * @author Daniele Alessandri <suppakilla@gmail.com>
  15. */
  16. class ZSetUnionStore extends Command
  17. {
  18. /**
  19. * {@inheritdoc}
  20. */
  21. public function getId()
  22. {
  23. return 'ZUNIONSTORE';
  24. }
  25. /**
  26. * {@inheritdoc}
  27. */
  28. protected function filterArguments(array $arguments)
  29. {
  30. $options = array();
  31. $argc = count($arguments);
  32. if ($argc > 2 && is_array($arguments[$argc - 1])) {
  33. $options = $this->prepareOptions(array_pop($arguments));
  34. }
  35. if (is_array($arguments[1])) {
  36. $arguments = array_merge(
  37. array($arguments[0], count($arguments[1])),
  38. $arguments[1]
  39. );
  40. }
  41. return array_merge($arguments, $options);
  42. }
  43. /**
  44. * Returns a list of options and modifiers compatible with Redis.
  45. *
  46. * @param array $options List of options.
  47. *
  48. * @return array
  49. */
  50. private function prepareOptions($options)
  51. {
  52. $opts = array_change_key_case($options, CASE_UPPER);
  53. $finalizedOpts = array();
  54. if (isset($opts['WEIGHTS']) && is_array($opts['WEIGHTS'])) {
  55. $finalizedOpts[] = 'WEIGHTS';
  56. foreach ($opts['WEIGHTS'] as $weight) {
  57. $finalizedOpts[] = $weight;
  58. }
  59. }
  60. if (isset($opts['AGGREGATE'])) {
  61. $finalizedOpts[] = 'AGGREGATE';
  62. $finalizedOpts[] = $opts['AGGREGATE'];
  63. }
  64. return $finalizedOpts;
  65. }
  66. }