RequestSerializer.php 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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\Protocol\Text;
  11. use Predis\Command\CommandInterface;
  12. use Predis\Protocol\RequestSerializerInterface;
  13. /**
  14. * Request serializer for the standard Redis wire protocol.
  15. *
  16. * @link http://redis.io/topics/protocol
  17. *
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class RequestSerializer implements RequestSerializerInterface
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function serialize(CommandInterface $command)
  26. {
  27. $commandID = $command->getId();
  28. $arguments = $command->getArguments();
  29. $cmdlen = strlen($commandID);
  30. $reqlen = count($arguments) + 1;
  31. $buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandID}\r\n";
  32. for ($i = 0, $reqlen--; $i < $reqlen; ++$i) {
  33. $argument = $arguments[$i];
  34. $arglen = strlen($argument);
  35. $buffer .= "\${$arglen}\r\n{$argument}\r\n";
  36. }
  37. return $buffer;
  38. }
  39. }