RawCommand.php 3.4 KB

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