ZSetUnionStore.php 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. class ZSetUnionStore extends Command
  12. {
  13. public function getId()
  14. {
  15. return 'ZUNIONSTORE';
  16. }
  17. protected function filterArguments(Array $arguments)
  18. {
  19. $options = array();
  20. $argc = count($arguments);
  21. if ($argc > 2 && is_array($arguments[$argc - 1])) {
  22. $options = $this->prepareOptions(array_pop($arguments));
  23. }
  24. if (is_array($arguments[1])) {
  25. $arguments = array_merge(
  26. array($arguments[0], count($arguments[1])),
  27. $arguments[1]
  28. );
  29. }
  30. return array_merge($arguments, $options);
  31. }
  32. private function prepareOptions($options)
  33. {
  34. $opts = array_change_key_case($options, CASE_UPPER);
  35. $finalizedOpts = array();
  36. if (isset($opts['WEIGHTS']) && is_array($opts['WEIGHTS'])) {
  37. $finalizedOpts[] = 'WEIGHTS';
  38. foreach ($opts['WEIGHTS'] as $weight) {
  39. $finalizedOpts[] = $weight;
  40. }
  41. }
  42. if (isset($opts['AGGREGATE'])) {
  43. $finalizedOpts[] = 'AGGREGATE';
  44. $finalizedOpts[] = $opts['AGGREGATE'];
  45. }
  46. return $finalizedOpts;
  47. }
  48. protected function onPrefixKeys(Array $arguments, $prefix)
  49. {
  50. $arguments[0] = "$prefix{$arguments[0]}";
  51. $length = ((int) $arguments[1]) + 2;
  52. for ($i = 2; $i < $length; $i++) {
  53. $arguments[$i] = "$prefix{$arguments[$i]}";
  54. }
  55. return $arguments;
  56. }
  57. protected function canBeHashed()
  58. {
  59. $args = $this->getArguments();
  60. return $this->checkSameHashForKeys(
  61. array_merge(array($args[0]), array_slice($args, 2, $args[1]))
  62. );
  63. }
  64. }