PrefixHelpers.php 3.0 KB

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