TextCommandSerializer.php 924 B

12345678910111213141516171819202122232425262728293031323334353637
  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. class TextCommandSerializer implements ICommandSerializer
  14. {
  15. public function serialize(ICommand $command)
  16. {
  17. $commandId = $command->getId();
  18. $arguments = $command->getArguments();
  19. $cmdlen = strlen($commandId);
  20. $reqlen = count($arguments) + 1;
  21. $buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandId}\r\n";
  22. for ($i = 0; $i < $reqlen - 1; $i++) {
  23. $argument = $arguments[$i];
  24. $arglen = strlen($argument);
  25. $buffer .= "\${$arglen}\r\n{$argument}\r\n";
  26. }
  27. return $buffer;
  28. }
  29. }