RawCommand.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. use InvalidArgumentException;
  12. /**
  13. * Class for generic "anonymous" Redis commands.
  14. *
  15. * This command class does not filter input arguments or parse responses, but
  16. * can be used to leverage the standard Predis API to execute any command simply
  17. * by providing the needed arguments following the command signature as defined
  18. * by Redis in its documentation.
  19. *
  20. * @author Daniele Alessandri <suppakilla@gmail.com>
  21. */
  22. class RawCommand implements CommandInterface
  23. {
  24. private $hash;
  25. private $commandID;
  26. private $arguments;
  27. public function __construct(array $arguments)
  28. {
  29. if (!$arguments) {
  30. throw new InvalidArgumentException(
  31. 'The arguments array must contain at least the command ID.'
  32. );
  33. }
  34. $this->commandID = strtoupper(array_shift($arguments));
  35. $this->arguments = $arguments;
  36. }
  37. /**
  38. * Creates a new raw command using a variadic method.
  39. *
  40. * @param string $commandID Redis command ID.
  41. * @param string ... Arguments list for the command.
  42. * @return CommandInterface
  43. */
  44. public static function create($commandID /* [ $arg, ... */)
  45. {
  46. $arguments = func_get_args();
  47. $command = new self($arguments);
  48. return $command;
  49. }
  50. /**
  51. * {@inheritdoc}
  52. */
  53. public function getId()
  54. {
  55. return $this->commandID;
  56. }
  57. /**
  58. * {@inheritdoc}
  59. */
  60. public function setArguments(array $arguments)
  61. {
  62. $this->arguments = $arguments;
  63. unset($this->hash);
  64. }
  65. /**
  66. * {@inheritdoc}
  67. */
  68. public function setRawArguments(array $arguments)
  69. {
  70. $this->setArguments($arguments);
  71. }
  72. /**
  73. * {@inheritdoc}
  74. */
  75. public function getArguments()
  76. {
  77. return $this->arguments;
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function getArgument($index)
  83. {
  84. if (isset($this->arguments[$index])) {
  85. return $this->arguments[$index];
  86. }
  87. }
  88. /**
  89. * {@inheritdoc}
  90. */
  91. public function setHash($hash)
  92. {
  93. $this->hash = $hash;
  94. }
  95. /**
  96. * {@inheritdoc}
  97. */
  98. public function getHash()
  99. {
  100. if (isset($this->hash)) {
  101. return $this->hash;
  102. }
  103. }
  104. /**
  105. * {@inheritdoc}
  106. */
  107. public function parseResponse($data)
  108. {
  109. return $data;
  110. }
  111. /**
  112. * Helper function used to reduce a list of arguments to a string.
  113. *
  114. * @param string $accumulator Temporary string.
  115. * @param string $argument Current argument.
  116. * @return string
  117. */
  118. protected function toStringArgumentReducer($accumulator, $argument)
  119. {
  120. if (strlen($argument) > 32) {
  121. $argument = substr($argument, 0, 32) . '[...]';
  122. }
  123. $accumulator .= " $argument";
  124. return $accumulator;
  125. }
  126. /**
  127. * Returns a partial string representation of the command with its arguments.
  128. *
  129. * @return string
  130. */
  131. public function __toString()
  132. {
  133. return array_reduce(
  134. $this->getArguments(),
  135. array($this, 'toStringArgumentReducer'),
  136. $this->getId()
  137. );
  138. }
  139. }