ZSetUnionStore.php 1.8 KB

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