Command.php 2.9 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\Commands;
  11. use Predis\Helpers;
  12. use Predis\Distribution\INodeKeyGenerator;
  13. abstract class Command implements ICommand
  14. {
  15. private $_hash;
  16. private $_arguments = array();
  17. protected function filterArguments(Array $arguments)
  18. {
  19. return $arguments;
  20. }
  21. public function setArguments(Array $arguments)
  22. {
  23. $this->_arguments = $this->filterArguments($arguments);
  24. unset($this->_hash);
  25. }
  26. public function setRawArguments(Array $arguments)
  27. {
  28. $this->_arguments = $arguments;
  29. unset($this->_hash);
  30. }
  31. public function getArguments()
  32. {
  33. return $this->_arguments;
  34. }
  35. public function getArgument($index = 0)
  36. {
  37. if (isset($this->_arguments[$index]) === true) {
  38. return $this->_arguments[$index];
  39. }
  40. }
  41. protected function onPrefixKeys(Array $arguments, $prefix)
  42. {
  43. $arguments[0] = "$prefix{$arguments[0]}";
  44. return $arguments;
  45. }
  46. public function prefixKeys($prefix)
  47. {
  48. $arguments = $this->onPrefixKeys($this->_arguments, $prefix);
  49. if (isset($arguments)) {
  50. $this->_arguments = $arguments;
  51. unset($this->_hash);
  52. }
  53. }
  54. protected function canBeHashed()
  55. {
  56. return isset($this->_arguments[0]);
  57. }
  58. protected function checkSameHashForKeys(Array $keys)
  59. {
  60. if (($count = count($keys)) === 0) {
  61. return false;
  62. }
  63. $currentKey = Helpers::getKeyHashablePart($keys[0]);
  64. for ($i = 1; $i < $count; $i++) {
  65. $nextKey = Helpers::getKeyHashablePart($keys[$i]);
  66. if ($currentKey !== $nextKey) {
  67. return false;
  68. }
  69. $currentKey = $nextKey;
  70. }
  71. return true;
  72. }
  73. public function getHash(INodeKeyGenerator $distributor)
  74. {
  75. if (isset($this->_hash)) {
  76. return $this->_hash;
  77. }
  78. if ($this->canBeHashed()) {
  79. $key = Helpers::getKeyHashablePart($this->_arguments[0]);
  80. $this->_hash = $distributor->generateKey($key);
  81. return $this->_hash;
  82. }
  83. return null;
  84. }
  85. public function parseResponse($data)
  86. {
  87. return $data;
  88. }
  89. protected function toStringArgumentReducer($accumulator, $argument)
  90. {
  91. if (strlen($argument) > 32) {
  92. $argument = substr($argument, 0, 32) . '[...]';
  93. }
  94. $accumulator .= " $argument";
  95. return $accumulator;
  96. }
  97. public function __toString()
  98. {
  99. return array_reduce(
  100. $this->getArguments(),
  101. array($this, 'toStringArgumentReducer'),
  102. $this->getId()
  103. );
  104. }
  105. }