TextCommandSerializer.php 688 B

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