Command.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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. * {@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. * Normalizes the arguments array passed to a Redis command.
  87. *
  88. * @param array $arguments Arguments for a command.
  89. * @return array
  90. */
  91. public static function normalizeArguments(array $arguments)
  92. {
  93. if (count($arguments) === 1 && is_array($arguments[0])) {
  94. return $arguments[0];
  95. }
  96. return $arguments;
  97. }
  98. /**
  99. * Normalizes the arguments array passed to a variadic Redis command.
  100. *
  101. * @param array $arguments Arguments for a command.
  102. * @return array
  103. */
  104. public static function normalizeVariadic(array $arguments)
  105. {
  106. if (count($arguments) === 2 && is_array($arguments[1])) {
  107. return array_merge(array($arguments[0]), $arguments[1]);
  108. }
  109. return $arguments;
  110. }
  111. }