ZSetUnionStore.php 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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 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. * Return 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. protected function onPrefixKeys(Array $arguments, $prefix)
  68. {
  69. $arguments[0] = "$prefix{$arguments[0]}";
  70. $length = ((int) $arguments[1]) + 2;
  71. for ($i = 2; $i < $length; $i++) {
  72. $arguments[$i] = "$prefix{$arguments[$i]}";
  73. }
  74. return $arguments;
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. protected function canBeHashed()
  80. {
  81. $args = $this->getArguments();
  82. return $this->checkSameHashForKeys(
  83. array_merge(array($args[0]), array_slice($args, 2, $args[1]))
  84. );
  85. }
  86. }