RawCommand.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. */
  43. public static function create($commandID /* [ $arg, ... */)
  44. {
  45. $arguments = func_get_args();
  46. $command = new self($arguments);
  47. return $command;
  48. }
  49. /**
  50. * {@inheritdoc}
  51. */
  52. public function getId()
  53. {
  54. return $this->commandID;
  55. }
  56. /**
  57. * {@inheritdoc}
  58. */
  59. public function setArguments(array $arguments)
  60. {
  61. $this->arguments = $arguments;
  62. unset($this->hash);
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setRawArguments(array $arguments)
  68. {
  69. $this->setArguments($arguments);
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getArguments()
  75. {
  76. return $this->arguments;
  77. }
  78. /**
  79. * Gets the argument from the arguments list at the specified index.
  80. *
  81. * @param array $arguments Position of the argument.
  82. */
  83. public function getArgument($index)
  84. {
  85. if (isset($this->arguments[$index])) {
  86. return $this->arguments[$index];
  87. }
  88. }
  89. /**
  90. * {@inheritdoc}
  91. */
  92. public function setHash($hash)
  93. {
  94. $this->hash = $hash;
  95. }
  96. /**
  97. * {@inheritdoc}
  98. */
  99. public function getHash()
  100. {
  101. if (isset($this->hash)) {
  102. return $this->hash;
  103. }
  104. }
  105. /**
  106. * {@inheritdoc}
  107. */
  108. public function parseResponse($data)
  109. {
  110. return $data;
  111. }
  112. /**
  113. * Helper function used to reduce a list of arguments to a string.
  114. *
  115. * @param string $accumulator Temporary string.
  116. * @param string $argument Current argument.
  117. * @return string
  118. */
  119. protected function toStringArgumentReducer($accumulator, $argument)
  120. {
  121. if (strlen($argument) > 32) {
  122. $argument = substr($argument, 0, 32) . '[...]';
  123. }
  124. $accumulator .= " $argument";
  125. return $accumulator;
  126. }
  127. /**
  128. * Returns a partial string representation of the command with its arguments.
  129. *
  130. * @return string
  131. */
  132. public function __toString()
  133. {
  134. return array_reduce(
  135. $this->getArguments(),
  136. array($this, 'toStringArgumentReducer'),
  137. $this->getId()
  138. );
  139. }
  140. }