RawCommand.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  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 $slot;
  25. private $commandID;
  26. private $arguments;
  27. /**
  28. * @param array $arguments Command ID and its arguments.
  29. */
  30. public function __construct(array $arguments)
  31. {
  32. if (!$arguments) {
  33. throw new InvalidArgumentException(
  34. 'The arguments array must contain at least the command ID.'
  35. );
  36. }
  37. $this->commandID = strtoupper(array_shift($arguments));
  38. $this->arguments = $arguments;
  39. }
  40. /**
  41. * Creates a new raw command using a variadic method.
  42. *
  43. * @param string $commandID Redis command ID.
  44. * @param string ... Arguments list for the command.
  45. *
  46. * @return CommandInterface
  47. */
  48. public static function create($commandID /* [ $arg, ... */)
  49. {
  50. $arguments = func_get_args();
  51. $command = new self($arguments);
  52. return $command;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getId()
  58. {
  59. return $this->commandID;
  60. }
  61. /**
  62. * {@inheritdoc}
  63. */
  64. public function setArguments(array $arguments)
  65. {
  66. $this->arguments = $arguments;
  67. unset($this->slot);
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function setRawArguments(array $arguments)
  73. {
  74. $this->setArguments($arguments);
  75. }
  76. /**
  77. * {@inheritdoc}
  78. */
  79. public function getArguments()
  80. {
  81. return $this->arguments;
  82. }
  83. /**
  84. * {@inheritdoc}
  85. */
  86. public function getArgument($index)
  87. {
  88. if (isset($this->arguments[$index])) {
  89. return $this->arguments[$index];
  90. }
  91. return null;
  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. return null;
  109. }
  110. /**
  111. * {@inheritdoc}
  112. */
  113. public function parseResponse($data)
  114. {
  115. return $data;
  116. }
  117. }