PrefixHelpers.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. * Class that defines a few helpers method for prefixing keys.
  13. *
  14. * @author Daniele Alessandri <suppakilla@gmail.com>
  15. */
  16. class PrefixHelpers
  17. {
  18. /**
  19. * Applies the specified prefix only the first argument.
  20. *
  21. * @param ICommand $command Command instance.
  22. * @param string $prefix Prefix string.
  23. */
  24. public function first(ICommand $command, $prefix) {
  25. $arguments = $command->getArguments();
  26. $arguments[0] = "$prefix{$arguments[0]}";
  27. $command->setRawArguments($arguments);
  28. }
  29. /**
  30. * Applies the specified prefix to all the arguments.
  31. *
  32. * @param ICommand $command Command instance.
  33. * @param string $prefix Prefix string.
  34. */
  35. public static function all(ICommand $command, $prefix)
  36. {
  37. $arguments = $command->getArguments();
  38. foreach ($arguments as &$key) {
  39. $key = "$prefix$key";
  40. }
  41. $command->setRawArguments($arguments);
  42. }
  43. /**
  44. * Applies the specified prefix only to even arguments in the list.
  45. *
  46. * @param ICommand $command Command instance.
  47. * @param string $prefix Prefix string.
  48. */
  49. public static function interleaved(ICommand $command, $prefix)
  50. {
  51. $arguments = $command->getArguments();
  52. $length = count($arguments);
  53. for ($i = 0; $i < $length; $i += 2) {
  54. $arguments[$i] = "$prefix{$arguments[$i]}";
  55. }
  56. $command->setRawArguments($arguments);
  57. }
  58. /**
  59. * Applies the specified prefix to all the arguments but the last one.
  60. *
  61. * @param ICommand $command Command instance.
  62. * @param string $prefix Prefix string.
  63. */
  64. public static function skipLast(ICommand $command, $prefix)
  65. {
  66. $arguments = $command->getArguments();
  67. $length = count($arguments);
  68. for ($i = 0; $i < $length - 1; $i++) {
  69. $arguments[$i] = "$prefix{$arguments[$i]}";
  70. }
  71. $command->setRawArguments($arguments);
  72. }
  73. }