AbstractCommand.php 3.3 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. /**
  12. * Base class for Redis commands.
  13. *
  14. * @author Daniele Alessandri <suppakilla@gmail.com>
  15. */
  16. abstract class AbstractCommand 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. * {@inheritdoc}
  55. */
  56. public function getArgument($index)
  57. {
  58. if (isset($this->arguments[$index])) {
  59. return $this->arguments[$index];
  60. }
  61. }
  62. /**
  63. * {@inheritdoc}
  64. */
  65. public function setHash($hash)
  66. {
  67. $this->hash = $hash;
  68. }
  69. /**
  70. * {@inheritdoc}
  71. */
  72. public function getHash()
  73. {
  74. if (isset($this->hash)) {
  75. return $this->hash;
  76. }
  77. }
  78. /**
  79. * {@inheritdoc}
  80. */
  81. public function parseResponse($data)
  82. {
  83. return $data;
  84. }
  85. /**
  86. * Helper function used to reduce a list of arguments to a string.
  87. *
  88. * @param string $accumulator Temporary string.
  89. * @param string $argument Current argument.
  90. * @return string
  91. */
  92. protected function toStringArgumentReducer($accumulator, $argument)
  93. {
  94. if (strlen($argument) > 32) {
  95. $argument = substr($argument, 0, 32) . '[...]';
  96. }
  97. $accumulator .= " $argument";
  98. return $accumulator;
  99. }
  100. /**
  101. * Returns a partial string representation of the command with its arguments.
  102. *
  103. * @return string
  104. */
  105. public function __toString()
  106. {
  107. return array_reduce(
  108. $this->getArguments(),
  109. array($this, 'toStringArgumentReducer'),
  110. $this->getId()
  111. );
  112. }
  113. /**
  114. * Normalizes the arguments array passed to a Redis command.
  115. *
  116. * @param array $arguments Arguments for a command.
  117. * @return array
  118. */
  119. public static function normalizeArguments(Array $arguments)
  120. {
  121. if (count($arguments) === 1 && is_array($arguments[0])) {
  122. return $arguments[0];
  123. }
  124. return $arguments;
  125. }
  126. /**
  127. * Normalizes the arguments array passed to a variadic Redis command.
  128. *
  129. * @param array $arguments Arguments for a command.
  130. * @return array
  131. */
  132. public static function normalizeVariadic(Array $arguments)
  133. {
  134. if (count($arguments) === 2 && is_array($arguments[1])) {
  135. return array_merge(array($arguments[0]), $arguments[1]);
  136. }
  137. return $arguments;
  138. }
  139. }