RawCommand.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  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 for generic "anonymous" Redis commands.
  13. *
  14. * This command class does not filter input arguments or parse responses, but
  15. * can be used to leverage the standard Predis API to execute any command simply
  16. * by providing the needed arguments following the command signature as defined
  17. * by Redis in its documentation.
  18. *
  19. * @author Daniele Alessandri <suppakilla@gmail.com>
  20. */
  21. class RawCommand implements CommandInterface
  22. {
  23. private $slot;
  24. private $commandID;
  25. private $arguments;
  26. /**
  27. * @param array $arguments Command ID and its arguments.
  28. *
  29. * @throws \InvalidArgumentException
  30. */
  31. public function __construct(array $arguments)
  32. {
  33. if (!$arguments) {
  34. throw new \InvalidArgumentException(
  35. 'The arguments array must contain at least the command ID.'
  36. );
  37. }
  38. $this->commandID = strtoupper(array_shift($arguments));
  39. $this->arguments = $arguments;
  40. }
  41. /**
  42. * Creates a new raw command using a variadic method.
  43. *
  44. * @param string $commandID Redis command ID.
  45. * @param string ... Arguments list for the command.
  46. *
  47. * @return CommandInterface
  48. */
  49. public static function create($commandID /* [ $arg, ... */)
  50. {
  51. $arguments = func_get_args();
  52. $command = new self($arguments);
  53. return $command;
  54. }
  55. /**
  56. * {@inheritdoc}
  57. */
  58. public function getId()
  59. {
  60. return $this->commandID;
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function setArguments(array $arguments)
  66. {
  67. $this->arguments = $arguments;
  68. unset($this->slot);
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function setRawArguments(array $arguments)
  74. {
  75. $this->setArguments($arguments);
  76. }
  77. /**
  78. * {@inheritdoc}
  79. */
  80. public function getArguments()
  81. {
  82. return $this->arguments;
  83. }
  84. /**
  85. * {@inheritdoc}
  86. */
  87. public function getArgument($index)
  88. {
  89. if (isset($this->arguments[$index])) {
  90. return $this->arguments[$index];
  91. }
  92. }
  93. /**
  94. * {@inheritdoc}
  95. */
  96. public function setSlot($slot)
  97. {
  98. $this->slot = $slot;
  99. }
  100. /**
  101. * {@inheritdoc}
  102. */
  103. public function getSlot()
  104. {
  105. if (isset($this->slot)) {
  106. return $this->slot;
  107. }
  108. }
  109. /**
  110. * {@inheritdoc}
  111. */
  112. public function parseResponse($data)
  113. {
  114. return $data;
  115. }
  116. }