Command.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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 $slot;
  19. private $arguments = array();
  20. /**
  21. * Returns a filtered array of the arguments.
  22. *
  23. * @param array $arguments List of arguments.
  24. *
  25. * @return array
  26. */
  27. protected function filterArguments(array $arguments)
  28. {
  29. return $arguments;
  30. }
  31. /**
  32. * {@inheritdoc}
  33. */
  34. public function setArguments(array $arguments)
  35. {
  36. $this->arguments = $this->filterArguments($arguments);
  37. unset($this->slot);
  38. }
  39. /**
  40. * {@inheritdoc}
  41. */
  42. public function setRawArguments(array $arguments)
  43. {
  44. $this->arguments = $arguments;
  45. unset($this->slot);
  46. }
  47. /**
  48. * {@inheritdoc}
  49. */
  50. public function getArguments()
  51. {
  52. return $this->arguments;
  53. }
  54. /**
  55. * {@inheritdoc}
  56. */
  57. public function getArgument($index)
  58. {
  59. if (isset($this->arguments[$index])) {
  60. return $this->arguments[$index];
  61. }
  62. }
  63. /**
  64. * {@inheritdoc}
  65. */
  66. public function setSlot($slot)
  67. {
  68. $this->slot = $slot;
  69. }
  70. /**
  71. * {@inheritdoc}
  72. */
  73. public function getSlot()
  74. {
  75. if (isset($this->slot)) {
  76. return $this->slot;
  77. }
  78. }
  79. /**
  80. * {@inheritdoc}
  81. */
  82. public function parseResponse($data)
  83. {
  84. return $data;
  85. }
  86. /**
  87. * Normalizes the arguments array passed to a Redis command.
  88. *
  89. * @param array $arguments Arguments for a command.
  90. *
  91. * @return array
  92. */
  93. public static function normalizeArguments(array $arguments)
  94. {
  95. if (count($arguments) === 1 && is_array($arguments[0])) {
  96. return $arguments[0];
  97. }
  98. return $arguments;
  99. }
  100. /**
  101. * Normalizes the arguments array passed to a variadic Redis command.
  102. *
  103. * @param array $arguments Arguments for a command.
  104. *
  105. * @return array
  106. */
  107. public static function normalizeVariadic(array $arguments)
  108. {
  109. if (count($arguments) === 2 && is_array($arguments[1])) {
  110. return array_merge(array($arguments[0]), $arguments[1]);
  111. }
  112. return $arguments;
  113. }
  114. }