Files
predis/lib/Predis/Protocol/Text/TextCommandSerializer.php
T
2013-12-16 18:52:11 +01:00

48 lines
1.2 KiB
PHP

<?php
/*
* This file is part of the Predis package.
*
* (c) Daniele Alessandri <suppakilla@gmail.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Protocol\Text;
use Predis\Command\CommandInterface;
use Predis\Protocol\CommandSerializerInterface;
/**
* Implements a pluggable command serializer using the standard wire protocol
* defined by Redis.
*
* @link http://redis.io/topics/protocol
* @author Daniele Alessandri <suppakilla@gmail.com>
*/
class TextCommandSerializer implements CommandSerializerInterface
{
/**
* {@inheritdoc}
*/
public function serialize(CommandInterface $command)
{
$commandId = $command->getId();
$arguments = $command->getArguments();
$cmdlen = strlen($commandId);
$reqlen = count($arguments) + 1;
$buffer = "*{$reqlen}\r\n\${$cmdlen}\r\n{$commandId}\r\n";
for ($i = 0, $reqlen--; $i < $reqlen; $i++) {
$argument = $arguments[$i];
$arglen = strlen($argument);
$buffer .= "\${$arglen}\r\n{$argument}\r\n";
}
return $buffer;
}
}