* 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\Command;
use InvalidArgumentException;
* Defines an abstraction representing a Redis command.
* @author Daniele Alessandri <suppakilla@gmail.com>
interface CommandInterface
* Returns the ID of the Redis command. By convention, command identifiers
* must always be uppercase.
* Assign the specified slot to the command for clustering distribution.
* @param int $slot Slot ID.
public function setSlot($slot);
* Returns the assigned slot of the command for clustering distribution.
public function getSlot();
* Sets the arguments for the command.
* @param array $arguments List of arguments.
public function setArguments(array $arguments);
* Sets the raw arguments for the command without processing them.
* @param array $arguments List of arguments.
public function setRawArguments(array $arguments);
* Gets the arguments of the command.
public function getArguments();
* Gets the argument of the command at the specified index.
* @param int $index Index of the desired argument.
public function getArgument($index);
* Parses a raw response and returns a PHP object.
* @param string $data Binary string containing the whole response.
public function parseResponse($data);
* Base class for Redis commands.
* @author Daniele Alessandri <suppakilla@gmail.com>
abstract class Command implements CommandInterface
private $arguments = array();
* Returns a filtered array of the arguments.
* @param array $arguments List of arguments.
protected function filterArguments(array $arguments)
public function setArguments(array $arguments)
$this->arguments = $this->filterArguments($arguments);
public function setRawArguments(array $arguments)
$this->arguments = $arguments;
public function getArguments()
public function getArgument($index)
if (isset($this->arguments[$index])) {
return $this->arguments[$index];
public function setSlot($slot)
public function getSlot()
if (isset($this->slot)) {
public function parseResponse($data)
* Normalizes the arguments array passed to a Redis command.
* @param array $arguments Arguments for a command.
public static function normalizeArguments(array $arguments)
if (count($arguments) === 1 && is_array($arguments[0])) {
* Normalizes the arguments array passed to a variadic Redis command.
* @param array $arguments Arguments for a command.
public static function normalizeVariadic(array $arguments)
if (count($arguments) === 2 && is_array($arguments[1])) {
return array_merge(array($arguments[0]), $arguments[1]);
* @link http://redis.io/commands/zrange
* @author Daniele Alessandri <suppakilla@gmail.com>
class ZSetRange extends Command
protected function filterArguments(array $arguments)
if (count($arguments) === 4) {
$lastType = gettype($arguments[3]);
if ($lastType === 'string' && strtoupper($arguments[3]) === 'WITHSCORES') {
// Used for compatibility with older versions
$arguments[3] = array('WITHSCORES' => true);
if ($lastType === 'array') {
$options = $this->prepareOptions(array_pop($arguments));
return array_merge($arguments, $options);
* Returns a list of options and modifiers compatible with Redis.
* @param array $options List of options.
protected function prepareOptions($options)
$opts = array_change_key_case($options, CASE_UPPER);
$finalizedOpts = array();
if (!empty($opts['WITHSCORES'])) {
$finalizedOpts[] = 'WITHSCORES';
* Checks for the presence of the WITHSCORES modifier.
protected function withScores()
$arguments = $this->getArguments();
if (count($arguments) < 4) {
return strtoupper($arguments[3]) === 'WITHSCORES';
public function parseResponse($data)
if ($this->withScores()) {
for ($i = 0; $i < count($data); $i++) {
$result[$data[$i]] = $data[++$i];
* @link http://redis.io/commands/sinterstore
* @author Daniele Alessandri <suppakilla@gmail.com>
class SetIntersectionStore extends Command
protected function filterArguments(array $arguments)
if (count($arguments) === 2 && is_array($arguments[1])) {
return array_merge(array($arguments[0]), $arguments[1]);
* @link http://redis.io/commands/sinter
* @author Daniele Alessandri <suppakilla@gmail.com>
class SetIntersection extends Command
protected function filterArguments(array $arguments)
return self::normalizeArguments($arguments);
* @link http://redis.io/commands/eval
* @author Daniele Alessandri <suppakilla@gmail.com>
class ServerEval extends Command
* Calculates the SHA1 hash of the body of the script.
* @return string SHA1 hash.
public function getScriptHash()
return sha1($this->getArgument(0));
* @link http://redis.io/commands/rename
* @author Daniele Alessandri <suppakilla@gmail.com>
class KeyRename extends Command
* @link http://redis.io/commands/setex
* @author Daniele Alessandri <suppakilla@gmail.com>
class StringSetExpire extends Command
* @link http://redis.io/commands/mset
* @author Daniele Alessandri <suppakilla@gmail.com>
class StringSetMultiple extends Command
protected function filterArguments(array $arguments)
if (count($arguments) === 1 && is_array($arguments[0])) {
foreach ($args as $k => $v) {
* @link http://redis.io/commands/expireat
* @author Daniele Alessandri <suppakilla@gmail.com>
class KeyExpireAt extends Command
public function parseResponse($data)
* @link http://redis.io/commands/blpop
* @author Daniele Alessandri <suppakilla@gmail.com>
class ListPopFirstBlocking extends Command
protected function filterArguments(array $arguments)
if (count($arguments) === 2 && is_array($arguments[0])) {
list($arguments, $timeout) = $arguments;
array_push($arguments, $timeout);
* @link http://redis.io/commands/unsubscribe
* @author Daniele Alessandri <suppakilla@gmail.com>
class PubSubUnsubscribe extends Command