TextCommandSerializer.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\Commands\ICommand;
  12. use Predis\Protocol\ICommandSerializer;
  13. /**
  14. * Implements a pluggable command serializer using the standard wire protocol
  15. * defined by Redis.
  16. *
  17. * @link http://redis.io/topics/protocol
  18. * @author Daniele Alessandri <suppakilla@gmail.com>
  19. */
  20. class TextCommandSerializer implements ICommandSerializer
  21. {
  22. /**
  23. * {@inheritdoc}
  24. */
  25. public function serialize(ICommand $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; $i < $reqlen - 1; $i++) {
  33. $argument = $arguments[$i];
  34. $arglen = strlen($argument);
  35. $buffer .= "\${$arglen}\r\n{$argument}\r\n";
  36. }
  37. return $buffer;
  38. }
  39. }