ZSetUnionStore.php 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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\Commands;
  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. /**
  78. * {@inheritdoc}
  79. */
  80. protected function canBeHashed()
  81. {
  82. $args = $this->getArguments();
  83. return $this->checkSameHashForKeys(
  84. array_merge(array($args[0]), array_slice($args, 2, $args[1]))
  85. );
  86. }
  87. }