ZSetUnionStore.php 1.7 KB

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