Command.php 3.4 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. /**
  12. * Base class for Redis commands.
  13. *
  14. * @author Daniele Alessandri <suppakilla@gmail.com>
  15. */
  16. abstract class Command implements CommandInterface
  17. {
  18. private $hash;
  19. private $arguments = array();
  20. /**
  21. * Returns a filtered array of the arguments.
  22. *
  23. * @param array $arguments List of arguments.
  24. * @return array
  25. */
  26. protected function filterArguments(array $arguments)
  27. {
  28. return $arguments;
  29. }
  30. /**
  31. * {@inheritdoc}
  32. */
  33. public function setArguments(array $arguments)
  34. {
  35. $this->arguments = $this->filterArguments($arguments);
  36. unset($this->hash);
  37. }
  38. /**
  39. * {@inheritdoc}
  40. */
  41. public function setRawArguments(array $arguments)
  42. {
  43. $this->arguments = $arguments;
  44. unset($this->hash);
  45. }
  46. /**
  47. * {@inheritdoc}
  48. */
  49. public function getArguments()
  50. {
  51. return $this->arguments;
  52. }
  53. /**
  54. * Gets the argument from the arguments list at the specified index.
  55. *
  56. * @param array $arguments Position of the argument.
  57. */
  58. public function getArgument($index)
  59. {
  60. if (isset($this->arguments[$index])) {
  61. return $this->arguments[$index];
  62. }
  63. }
  64. /**
  65. * {@inheritdoc}
  66. */
  67. public function setHash($hash)
  68. {
  69. $this->hash = $hash;
  70. }
  71. /**
  72. * {@inheritdoc}
  73. */
  74. public function getHash()
  75. {
  76. if (isset($this->hash)) {
  77. return $this->hash;
  78. }
  79. }
  80. /**
  81. * {@inheritdoc}
  82. */
  83. public function parseResponse($data)
  84. {
  85. return $data;
  86. }
  87. /**
  88. * Helper function used to reduce a list of arguments to a string.
  89. *
  90. * @param string $accumulator Temporary string.
  91. * @param string $argument Current argument.
  92. * @return string
  93. */
  94. protected function toStringArgumentReducer($accumulator, $argument)
  95. {
  96. if (strlen($argument) > 32) {
  97. $argument = substr($argument, 0, 32) . '[...]';
  98. }
  99. $accumulator .= " $argument";
  100. return $accumulator;
  101. }
  102. /**
  103. * Returns a partial string representation of the command with its arguments.
  104. *
  105. * @return string
  106. */
  107. public function __toString()
  108. {
  109. return array_reduce(
  110. $this->getArguments(),
  111. array($this, 'toStringArgumentReducer'),
  112. $this->getId()
  113. );
  114. }
  115. /**
  116. * Normalizes the arguments array passed to a Redis command.
  117. *
  118. * @param array $arguments Arguments for a command.
  119. * @return array
  120. */
  121. public static function normalizeArguments(array $arguments)
  122. {
  123. if (count($arguments) === 1 && is_array($arguments[0])) {
  124. return $arguments[0];
  125. }
  126. return $arguments;
  127. }
  128. /**
  129. * Normalizes the arguments array passed to a variadic Redis command.
  130. *
  131. * @param array $arguments Arguments for a command.
  132. * @return array
  133. */
  134. public static function normalizeVariadic(array $arguments)
  135. {
  136. if (count($arguments) === 2 && is_array($arguments[1])) {
  137. return array_merge(array($arguments[0]), $arguments[1]);
  138. }
  139. return $arguments;
  140. }
  141. }