ScriptedCommand.php 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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 used to implement an higher level abstraction for "virtual"
  13. * commands based on EVAL.
  14. *
  15. * @link http://redis.io/commands/eval
  16. * @author Daniele Alessandri <suppakilla@gmail.com>
  17. */
  18. abstract class ScriptedCommand extends ServerEvalSHA
  19. {
  20. /**
  21. * Gets the body of a Lua script.
  22. *
  23. * @return string
  24. */
  25. abstract public function getScript();
  26. /**
  27. * Specifies the number of arguments that should be considered as keys.
  28. *
  29. * The default behaviour for the base class is to return 0 to indicate that
  30. * all the elements of the arguments array should be considered as keys, but
  31. * subclasses can enforce a static number of keys.
  32. *
  33. * @return int
  34. */
  35. protected function getKeysCount()
  36. {
  37. return 0;
  38. }
  39. /**
  40. * Returns the elements from the arguments that are identified as keys.
  41. *
  42. * @return array
  43. */
  44. public function getKeys()
  45. {
  46. return array_slice($this->getArguments(), 2, $this->getKeysCount());
  47. }
  48. /**
  49. * {@inheritdoc}
  50. */
  51. protected function filterArguments(Array $arguments)
  52. {
  53. if (($numkeys = $this->getKeysCount()) && $numkeys < 0) {
  54. $numkeys = count($arguments) + $numkeys;
  55. }
  56. return array_merge(array(sha1($this->getScript()), (int) $numkeys), $arguments);
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function getEvalArguments()
  62. {
  63. $arguments = $this->getArguments();
  64. $arguments[0] = $this->getScript();
  65. return $arguments;
  66. }
  67. }