TextCommandSerializer.php 690 B

12345678910111213141516171819202122232425262728
  1. <?php
  2. namespace Predis\Protocol\Text;
  3. use Predis\Commands\ICommand;
  4. use Predis\Protocol\ICommandSerializer;
  5. class TextCommandSerializer implements ICommandSerializer
  6. {
  7. public function serialize(ICommand $command)
  8. {
  9. $commandId = $command->getId();
  10. $arguments = $command->getArguments();
  11. $cmdlen = strlen($commandId);
  12. $reqlen = count($arguments) + 1;
  13. $buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandId}\r\n";
  14. for ($i = 0; $i < $reqlen - 1; $i++) {
  15. $argument = $arguments[$i];
  16. $arglen = strlen($argument);
  17. $buffer .= "\${$arglen}\r\n{$argument}\r\n";
  18. }
  19. return $buffer;
  20. }
  21. }