AbstractCommand.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. * Sets the arguments array without filtering.
  40. *
  41. * @param array $arguments List of arguments.
  42. */
  43. public function setRawArguments(Array $arguments)
  44. {
  45. $this->arguments = $arguments;
  46. unset($this->hash);
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. public function getArguments()
  52. {
  53. return $this->arguments;
  54. }
  55. /**
  56. * Gets the argument from the arguments list at the specified index.
  57. *
  58. * @param array $arguments Position of the argument.
  59. */
  60. public function getArgument($index = 0)
  61. {
  62. if (isset($this->arguments[$index]) === true) {
  63. return $this->arguments[$index];
  64. }
  65. }
  66. /**
  67. * {@inheritdoc}
  68. */
  69. public function setHash($hash)
  70. {
  71. $this->hash = $hash;
  72. }
  73. /**
  74. * {@inheritdoc}
  75. */
  76. public function getHash()
  77. {
  78. if (isset($this->hash)) {
  79. return $this->hash;
  80. }
  81. }
  82. /**
  83. * {@inheritdoc}
  84. */
  85. public function parseResponse($data)
  86. {
  87. return $data;
  88. }
  89. /**
  90. * Helper function used to reduce a list of arguments to a string.
  91. *
  92. * @param string $accumulator Temporary string.
  93. * @param string $argument Current argument.
  94. * @return string
  95. */
  96. protected function toStringArgumentReducer($accumulator, $argument)
  97. {
  98. if (strlen($argument) > 32) {
  99. $argument = substr($argument, 0, 32) . '[...]';
  100. }
  101. $accumulator .= " $argument";
  102. return $accumulator;
  103. }
  104. /**
  105. * Returns a partial string representation of the command with its arguments.
  106. *
  107. * @return string
  108. */
  109. public function __toString()
  110. {
  111. return array_reduce(
  112. $this->getArguments(),
  113. array($this, 'toStringArgumentReducer'),
  114. $this->getId()
  115. );
  116. }
  117. }