PrefixHelpers.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 static function first(ICommand $command, $prefix)
  25. {
  26. if ($arguments = $command->getArguments()) {
  27. $arguments[0] = "$prefix{$arguments[0]}";
  28. $command->setRawArguments($arguments);
  29. }
  30. }
  31. /**
  32. * Applies the specified prefix to all the arguments.
  33. *
  34. * @param ICommand $command Command instance.
  35. * @param string $prefix Prefix string.
  36. */
  37. public static function all(ICommand $command, $prefix)
  38. {
  39. $arguments = $command->getArguments();
  40. foreach ($arguments as &$key) {
  41. $key = "$prefix$key";
  42. }
  43. $command->setRawArguments($arguments);
  44. }
  45. /**
  46. * Applies the specified prefix only to even arguments in the list.
  47. *
  48. * @param ICommand $command Command instance.
  49. * @param string $prefix Prefix string.
  50. */
  51. public static function interleaved(ICommand $command, $prefix)
  52. {
  53. $arguments = $command->getArguments();
  54. $length = count($arguments);
  55. for ($i = 0; $i < $length; $i += 2) {
  56. $arguments[$i] = "$prefix{$arguments[$i]}";
  57. }
  58. $command->setRawArguments($arguments);
  59. }
  60. /**
  61. * Applies the specified prefix to all the arguments but the first one.
  62. *
  63. * @param ICommand $command Command instance.
  64. * @param string $prefix Prefix string.
  65. */
  66. public static function skipFirst(ICommand $command, $prefix)
  67. {
  68. $arguments = $command->getArguments();
  69. $length = count($arguments);
  70. for ($i = 1; $i < $length; $i++) {
  71. $arguments[$i] = "$prefix{$arguments[$i]}";
  72. }
  73. $command->setRawArguments($arguments);
  74. }
  75. /**
  76. * Applies the specified prefix to all the arguments but the last one.
  77. *
  78. * @param ICommand $command Command instance.
  79. * @param string $prefix Prefix string.
  80. */
  81. public static function skipLast(ICommand $command, $prefix)
  82. {
  83. $arguments = $command->getArguments();
  84. $length = count($arguments);
  85. for ($i = 0; $i < $length - 1; $i++) {
  86. $arguments[$i] = "$prefix{$arguments[$i]}";
  87. }
  88. $command->setRawArguments($arguments);
  89. }
  90. }