TextCommandSerializer.php 640 B

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