RequestSerializer.php 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  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. * @author Daniele Alessandri <suppakilla@gmail.com>
  18. */
  19. class RequestSerializer implements RequestSerializerInterface
  20. {
  21. /**
  22. * {@inheritdoc}
  23. */
  24. public function serialize(CommandInterface $command)
  25. {
  26. $commandID = $command->getId();
  27. $arguments = $command->getArguments();
  28. $cmdlen = strlen($commandID);
  29. $reqlen = count($arguments) + 1;
  30. $buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandID}\r\n";
  31. for ($i = 0; $i < $reqlen - 1; $i++) {
  32. $argument = $arguments[$i];
  33. $arglen = strlen($argument);
  34. $buffer .= "\${$arglen}\r\n{$argument}\r\n";
  35. }
  36. return $buffer;
  37. }
  38. }