ZSetUnionStore.php 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. namespace Predis\Commands;
  3. class ZSetUnionStore extends Command {
  4. public function getId() {
  5. return 'ZUNIONSTORE';
  6. }
  7. protected function filterArguments(Array $arguments) {
  8. $options = array();
  9. $argc = count($arguments);
  10. if ($argc > 2 && is_array($arguments[$argc - 1])) {
  11. $options = $this->prepareOptions(array_pop($arguments));
  12. }
  13. if (is_array($arguments[1])) {
  14. $arguments = array_merge(
  15. array($arguments[0], count($arguments[1])),
  16. $arguments[1]
  17. );
  18. }
  19. return array_merge($arguments, $options);
  20. }
  21. private function prepareOptions($options) {
  22. $opts = array_change_key_case($options, CASE_UPPER);
  23. $finalizedOpts = array();
  24. if (isset($opts['WEIGHTS']) && is_array($opts['WEIGHTS'])) {
  25. $finalizedOpts[] = 'WEIGHTS';
  26. foreach ($opts['WEIGHTS'] as $weight) {
  27. $finalizedOpts[] = $weight;
  28. }
  29. }
  30. if (isset($opts['AGGREGATE'])) {
  31. $finalizedOpts[] = 'AGGREGATE';
  32. $finalizedOpts[] = $opts['AGGREGATE'];
  33. }
  34. return $finalizedOpts;
  35. }
  36. protected function onPrefixKeys(Array $arguments, $prefix) {
  37. $arguments[0] = "$prefix{$arguments[0]}";
  38. $length = ((int) $arguments[1]) + 2;
  39. for ($i = 2; $i < $length; $i++) {
  40. $arguments[$i] = "$prefix{$arguments[$i]}";
  41. }
  42. return $arguments;
  43. }
  44. protected function canBeHashed() {
  45. $args = $this->getArguments();
  46. return $this->checkSameHashForKeys(
  47. array_merge(array($args[0]), array_slice($args, 2, $args[1]))
  48. );
  49. }
  50. }