ZSetUnionStore.php 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 PrefixableCommand
  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. /**
  65. * {@inheritdoc}
  66. */
  67. public function prefixKeys($prefix)
  68. {
  69. $arguments = $this->getArguments();
  70. $arguments[0] = "$prefix{$arguments[0]}";
  71. $length = ((int) $arguments[1]) + 2;
  72. for ($i = 2; $i < $length; $i++) {
  73. $arguments[$i] = "$prefix{$arguments[$i]}";
  74. }
  75. $this->setRawArguments($arguments);
  76. }
  77. }