RawCommand.php 3.4 KB

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