Predis to v2.2.2

This commit is contained in:
the-djmaze 2024-04-08 16:29:55 +02:00
parent 9510347d3a
commit 969dca5f7e
449 changed files with 22013 additions and 2 deletions

View file

@ -0,0 +1,66 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait Aggregate
{
/**
* @var string[]
*/
private static $aggregateValuesEnum = [
'min' => 'MIN',
'max' => 'MAX',
'sum' => 'SUM',
];
/**
* @var string
*/
private static $aggregateModifier = 'AGGREGATE';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$aggregateArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$aggregateArgumentPositionOffset];
if (is_string($argument) && in_array(strtoupper($argument), self::$aggregateValuesEnum)) {
$argument = self::$aggregateValuesEnum[$argument];
} else {
$enumValues = implode(', ', array_keys(self::$aggregateValuesEnum));
throw new UnexpectedValueException("Aggregate argument accepts only: {$enumValues} values");
}
$argumentsBefore = array_slice($arguments, 0, static::$aggregateArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$aggregateArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$aggregateModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
trait BitByte
{
private static $argumentEnum = [
'bit' => 'BIT',
'byte' => 'BYTE',
];
public function setArguments(array $arguments)
{
$value = array_pop($arguments);
if (null === $value) {
parent::setArguments($arguments);
return;
}
if (in_array(strtoupper($value), self::$argumentEnum, true)) {
$arguments[] = self::$argumentEnum[$value];
} else {
$arguments[] = $value;
}
parent::setArguments($arguments);
}
}

View file

@ -0,0 +1,57 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\BloomFilters;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait BucketSize
{
private static $bucketSizeModifier = 'BUCKETSIZE';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$bucketSizeArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if ($arguments[static::$bucketSizeArgumentPositionOffset] === -1) {
array_splice($arguments, static::$bucketSizeArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
if ($arguments[static::$bucketSizeArgumentPositionOffset] < 1) {
throw new UnexpectedValueException('Wrong bucket size argument value or position offset');
}
$argument = $arguments[static::$bucketSizeArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$bucketSizeArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$bucketSizeArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$bucketSizeModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,57 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\BloomFilters;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait Capacity
{
private static $capacityModifier = 'CAPACITY';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$capacityArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if ($arguments[static::$capacityArgumentPositionOffset] === -1) {
array_splice($arguments, static::$capacityArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
if ($arguments[static::$capacityArgumentPositionOffset] < 1) {
throw new UnexpectedValueException('Wrong capacity argument value or position offset');
}
$argument = $arguments[static::$capacityArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$capacityArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$capacityArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$capacityModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,57 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\BloomFilters;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait Error
{
private static $errorModifier = 'ERROR';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$errorArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if ($arguments[static::$errorArgumentPositionOffset] === -1) {
array_splice($arguments, static::$errorArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
if ($arguments[static::$errorArgumentPositionOffset] < 0) {
throw new UnexpectedValueException('Wrong error argument value or position offset');
}
$argument = $arguments[static::$errorArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$errorArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$errorArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$errorModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\BloomFilters;
use UnexpectedValueException;
trait Expansion
{
private static $expansionModifier = 'EXPANSION';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$expansionArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if ($arguments[static::$expansionArgumentPositionOffset] === -1) {
array_splice($arguments, static::$expansionArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
if ($arguments[static::$expansionArgumentPositionOffset] < 1) {
throw new UnexpectedValueException('Wrong expansion argument value or position offset');
}
$argument = $arguments[static::$expansionArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$expansionArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$expansionArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$expansionModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,45 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\BloomFilters;
use Predis\Command\Command;
/**
* @mixin Command
*/
trait Items
{
private static $itemsModifier = 'ITEMS';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$itemsArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$itemsArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$itemsArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$itemsArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$itemsModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,57 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\BloomFilters;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait MaxIterations
{
private static $maxIterationsModifier = 'MAXITERATIONS';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$maxIterationsArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if ($arguments[static::$maxIterationsArgumentPositionOffset] === -1) {
array_splice($arguments, static::$maxIterationsArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
if ($arguments[static::$maxIterationsArgumentPositionOffset] < 1) {
throw new UnexpectedValueException('Wrong max iterations argument value or position offset');
}
$argument = $arguments[static::$maxIterationsArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$maxIterationsArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$maxIterationsArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$maxIterationsModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\BloomFilters;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait NoCreate
{
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (
static::$noCreateArgumentPositionOffset >= $argumentsLength
|| false === $arguments[static::$noCreateArgumentPositionOffset]
) {
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$noCreateArgumentPositionOffset];
if (true === $argument) {
$argument = 'NOCREATE';
} else {
throw new UnexpectedValueException('Wrong NOCREATE argument type');
}
$argumentsBefore = array_slice($arguments, 0, static::$noCreateArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$noCreateArgumentPositionOffset + 1);
parent::setArguments(array_merge($argumentsBefore, [$argument], $argumentsAfter));
}
}

View file

@ -0,0 +1,40 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\By;
use Predis\Command\Command;
/**
* @mixin Command
*/
trait ByArgument
{
private $byModifier = 'BY';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$byArgumentPositionOffset >= $argumentsLength || null === $arguments[static::$byArgumentPositionOffset]) {
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$byArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$byArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$byArgumentPositionOffset + 1);
parent::setArguments(array_merge($argumentsBefore, [$this->byModifier, $argument], $argumentsAfter));
}
}

View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\By;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait ByLexByScore
{
private static $argumentsEnum = [
'bylex' => 'BYLEX',
'byscore' => 'BYSCORE',
];
public function setArguments(array $arguments)
{
$argument = $arguments[static::$byLexByScoreArgumentPositionOffset];
if (false === $argument) {
parent::setArguments($arguments);
return;
}
if (is_string($argument) && in_array(strtoupper($argument), self::$argumentsEnum)) {
$argument = self::$argumentsEnum[$argument];
} else {
throw new UnexpectedValueException('By argument accepts only "bylex" and "byscore" values');
}
$argumentsBefore = array_slice($arguments, 0, static::$byLexByScoreArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$byLexByScoreArgumentPositionOffset + 1);
parent::setArguments(array_merge($argumentsBefore, [$argument], $argumentsAfter));
}
}

View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\By;
use InvalidArgumentException;
use Predis\Command\Argument\Geospatial\ByInterface;
trait GeoBy
{
public function setArguments(array $arguments)
{
$argumentPositionOffset = $this->getByArgumentPositionOffset($arguments);
if (null === $argumentPositionOffset) {
throw new InvalidArgumentException('Invalid BY argument value given');
}
$byArgumentObject = $arguments[$argumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, $argumentPositionOffset);
$argumentsAfter = array_slice($arguments, $argumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
$byArgumentObject->toArray(),
$argumentsAfter
));
}
private function getByArgumentPositionOffset(array $arguments): ?int
{
foreach ($arguments as $i => $value) {
if ($value instanceof ByInterface) {
return $i;
}
}
return null;
}
}

View file

@ -0,0 +1,71 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait Count
{
private $countModifier = 'COUNT';
private $anyModifier = 'ANY';
public function setArguments(array $arguments, bool $any = false)
{
$argumentsLength = count($arguments);
if (static::$countArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if ($arguments[static::$countArgumentPositionOffset] === -1) {
array_splice($arguments, static::$countArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
if ($arguments[static::$countArgumentPositionOffset] < 1) {
throw new UnexpectedValueException('Wrong count argument value or position offset');
}
$countArgument = $arguments[static::$countArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$countArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$countArgumentPositionOffset + 2);
if (!$any) {
$argumentsAfter = array_slice($arguments, static::$countArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[$this->countModifier],
[$countArgument],
$argumentsAfter
));
return;
}
parent::setArguments(array_merge(
$argumentsBefore,
[$this->countModifier],
[$countArgument],
[$this->anyModifier],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use UnexpectedValueException;
trait DB
{
private $dbModifier = 'DB';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$dbArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if (!is_numeric($arguments[static::$dbArgumentPositionOffset])) {
throw new UnexpectedValueException('DB argument should be a valid numeric value');
}
if ($arguments[static::$dbArgumentPositionOffset] < 0) {
array_splice($arguments, static::$dbArgumentPositionOffset, 1);
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$dbArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$dbArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$dbArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[$this->dbModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,42 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\Expire;
trait ExpireOptions
{
private static $argumentEnum = [
'nx' => 'NX',
'xx' => 'XX',
'gt' => 'GT',
'lt' => 'LT',
];
public function setArguments(array $arguments)
{
$value = array_pop($arguments);
if (null === $value) {
parent::setArguments($arguments);
return;
}
if (in_array(strtoupper($value), self::$argumentEnum, true)) {
$arguments[] = self::$argumentEnum[strtolower($value)];
} else {
$arguments[] = $value;
}
parent::setArguments($arguments);
}
}

View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\From;
use InvalidArgumentException;
use Predis\Command\Argument\Geospatial\FromInterface;
trait GeoFrom
{
public function setArguments(array $arguments)
{
$argumentPositionOffset = $this->getFromArgumentPositionOffset($arguments);
if (null === $argumentPositionOffset) {
throw new InvalidArgumentException('Invalid FROM argument value given');
}
$fromArgumentObject = $arguments[$argumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, $argumentPositionOffset);
$argumentsAfter = array_slice($arguments, $argumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
$fromArgumentObject->toArray(),
$argumentsAfter
));
}
private function getFromArgumentPositionOffset(array $arguments): ?int
{
foreach ($arguments as $i => $value) {
if ($value instanceof FromInterface) {
return $i;
}
}
return null;
}
}

View file

@ -0,0 +1,47 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\Get;
use UnexpectedValueException;
trait Get
{
private static $getModifier = 'GET';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$getArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if (!is_array($arguments[static::$getArgumentPositionOffset])) {
throw new UnexpectedValueException('Wrong get argument type');
}
$patterns = [];
foreach ($arguments[static::$getArgumentPositionOffset] as $pattern) {
$patterns[] = self::$getModifier;
$patterns[] = $pattern;
}
$argumentsBeforeKeys = array_slice($arguments, 0, static::$getArgumentPositionOffset);
$argumentsAfterKeys = array_slice($arguments, static::$getArgumentPositionOffset + 1);
parent::setArguments(array_merge($argumentsBeforeKeys, $patterns, $argumentsAfterKeys));
}
}

View file

@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\Json;
use UnexpectedValueException;
trait Indent
{
private static $indentModifier = 'INDENT';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$indentArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if ($arguments[static::$indentArgumentPositionOffset] === '') {
array_splice($arguments, static::$indentArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$indentArgumentPositionOffset];
if (!is_string($argument)) {
throw new UnexpectedValueException('Indent argument value should be a string');
}
$argumentsBefore = array_slice($arguments, 0, static::$indentArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$indentArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$indentModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\Json;
use UnexpectedValueException;
trait Newline
{
private static $newlineModifier = 'NEWLINE';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$newlineArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if ($arguments[static::$newlineArgumentPositionOffset] === '') {
array_splice($arguments, static::$newlineArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$newlineArgumentPositionOffset];
if (!is_string($argument)) {
throw new UnexpectedValueException('Newline argument value should be a string');
}
$argumentsBefore = array_slice($arguments, 0, static::$newlineArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$newlineArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$newlineModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,64 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\Json;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait NxXxArgument
{
/**
* @var string[]
*/
private static $argumentEnum = [
'nx' => 'NX',
'xx' => 'XX',
];
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$nxXxArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if (null === $arguments[static::$nxXxArgumentPositionOffset]) {
array_splice($arguments, static::$nxXxArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$nxXxArgumentPositionOffset];
if (!in_array(strtoupper($argument), self::$argumentEnum, true)) {
$enumValues = implode(', ', array_keys(self::$argumentEnum));
throw new UnexpectedValueException("Argument accepts only: {$enumValues} values");
}
$argumentsBefore = array_slice($arguments, 0, static::$nxXxArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$nxXxArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$argumentEnum[strtolower($argument)]],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\Json;
use UnexpectedValueException;
trait Space
{
private static $spaceModifier = 'SPACE';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$spaceArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if ($arguments[static::$spaceArgumentPositionOffset] === '') {
array_splice($arguments, static::$spaceArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$spaceArgumentPositionOffset];
if (!is_string($argument)) {
throw new UnexpectedValueException('Space argument value should be a string');
}
$argumentsBefore = array_slice($arguments, 0, static::$spaceArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$spaceArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$spaceModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,47 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait Keys
{
public function setArguments(array $arguments, bool $withNumkeys = true)
{
$argumentsLength = count($arguments);
if (
static::$keysArgumentPositionOffset > $argumentsLength
|| !is_array($arguments[static::$keysArgumentPositionOffset])
) {
throw new UnexpectedValueException('Wrong keys argument type or position offset');
}
$keysArgument = $arguments[static::$keysArgumentPositionOffset];
$argumentsBeforeKeys = array_slice($arguments, 0, static::$keysArgumentPositionOffset);
$argumentsAfterKeys = array_slice($arguments, static::$keysArgumentPositionOffset + 1);
if ($withNumkeys) {
$numkeys = count($keysArgument);
parent::setArguments(array_merge($argumentsBeforeKeys, [$numkeys], $keysArgument, $argumentsAfterKeys));
return;
}
parent::setArguments(array_merge($argumentsBeforeKeys, $keysArgument, $argumentsAfterKeys));
}
}

View file

@ -0,0 +1,60 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait LeftRight
{
/**
* @var array{string: string}
*/
private static $leftRightEnum = [
'left' => 'LEFT',
'right' => 'RIGHT',
];
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$leftRightArgumentPositionOffset >= $argumentsLength) {
$arguments[] = 'LEFT';
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$leftRightArgumentPositionOffset];
if (is_string($argument) && in_array(strtoupper($argument), self::$leftRightEnum, true)) {
$argument = self::$leftRightEnum[$argument];
} else {
$enumValues = implode(', ', array_keys(self::$leftRightEnum));
throw new UnexpectedValueException("Left/Right argument accepts only: {$enumValues} values");
}
$argumentsBefore = array_slice($arguments, 0, static::$leftRightArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$leftRightArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,54 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\Limit;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait Limit
{
private static $limitModifier = 'LIMIT';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
$argumentsBefore = array_slice($arguments, 0, static::$limitArgumentPositionOffset);
if (
static::$limitArgumentPositionOffset >= $argumentsLength
|| false === $arguments[static::$limitArgumentPositionOffset]
) {
parent::setArguments($argumentsBefore);
return;
}
$argument = $arguments[static::$limitArgumentPositionOffset];
$argumentsAfter = array_slice($arguments, static::$limitArgumentPositionOffset + 1);
if (true === $argument) {
parent::setArguments(array_merge($argumentsBefore, [self::$limitModifier], $argumentsAfter));
return;
}
if (!is_int($argument)) {
throw new UnexpectedValueException('Wrong limit argument type');
}
parent::setArguments(array_merge($argumentsBefore, [self::$limitModifier], [$argument], $argumentsAfter));
}
}

View file

@ -0,0 +1,50 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\Limit;
use Predis\Command\Argument\Server\LimitInterface;
trait LimitObject
{
public function setArguments(array $arguments)
{
$argumentPositionOffset = $this->getLimitArgumentPositionOffset($arguments);
if (null === $argumentPositionOffset) {
parent::setArguments($arguments);
return;
}
$limitObject = $arguments[$argumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, $argumentPositionOffset);
$argumentsAfter = array_slice($arguments, $argumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
$limitObject->toArray(),
$argumentsAfter
));
}
private function getLimitArgumentPositionOffset(array $arguments): ?int
{
foreach ($arguments as $i => $value) {
if ($value instanceof LimitInterface) {
return $i;
}
}
return null;
}
}

View file

@ -0,0 +1,45 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait MinMaxModifier
{
/**
* @var array{string: string}
*/
private $modifierEnum = [
'min' => 'MIN',
'max' => 'MAX',
];
public function resolveModifier(int $offset, array &$arguments): void
{
if ($offset >= count($arguments)) {
$arguments[$offset] = $this->modifierEnum['min'];
return;
}
if (!is_string($arguments[$offset]) || !array_key_exists($arguments[$offset], $this->modifierEnum)) {
throw new UnexpectedValueException('Wrong type of modifier given');
}
$arguments[$offset] = $this->modifierEnum[$arguments[$offset]];
}
}

View file

@ -0,0 +1,34 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use Predis\Command\Command;
/**
* @mixin Command
*/
trait Replace
{
public function setArguments(array $arguments)
{
$replace = array_pop($arguments);
if (is_bool($replace) && $replace) {
$arguments[] = 'REPLACE';
} elseif (!is_bool($replace)) {
$arguments[] = $replace;
}
parent::setArguments($arguments);
}
}

View file

@ -0,0 +1,44 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait Rev
{
public function setArguments(array $arguments)
{
$argument = $arguments[static::$revArgumentPositionOffset];
if (false === $argument) {
parent::setArguments($arguments);
return;
}
if (true === $argument) {
$argument = 'REV';
} else {
throw new UnexpectedValueException('Wrong rev argument type');
}
$argumentsBefore = array_slice($arguments, 0, static::$revArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$revArgumentPositionOffset + 1);
parent::setArguments(array_merge($argumentsBefore, [$argument], $argumentsAfter));
}
}

View file

@ -0,0 +1,57 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use UnexpectedValueException;
trait Sorting
{
private static $sortingEnum = [
'asc' => 'ASC',
'desc' => 'DESC',
];
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$sortArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$sortArgumentPositionOffset];
if (null === $argument) {
array_splice($arguments, static::$sortArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
if (!in_array(strtoupper($argument), self::$sortingEnum, true)) {
$enumValues = implode(', ', array_keys(self::$sortingEnum));
throw new UnexpectedValueException("Sorting argument accepts only: {$enumValues} values");
}
$argumentsBefore = array_slice($arguments, 0, static::$sortArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$sortArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$sortingEnum[$argument]],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait Storedist
{
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (
static::$storeDistArgumentPositionOffset >= $argumentsLength
|| false === $arguments[static::$storeDistArgumentPositionOffset]
) {
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$storeDistArgumentPositionOffset];
if (true === $argument) {
$argument = 'STOREDIST';
} else {
throw new UnexpectedValueException('Wrong STOREDIST argument type');
}
$argumentsBefore = array_slice($arguments, 0, static::$storeDistArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$storeDistArgumentPositionOffset + 1);
parent::setArguments(array_merge($argumentsBefore, [$argument], $argumentsAfter));
}
}

View file

@ -0,0 +1,53 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use UnexpectedValueException;
trait Timeout
{
private static $timeoutModifier = 'TIMEOUT';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$timeoutArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if ($arguments[static::$timeoutArgumentPositionOffset] === -1) {
array_splice($arguments, static::$timeoutArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
if ($arguments[static::$timeoutArgumentPositionOffset] < 1) {
throw new UnexpectedValueException('Wrong timeout argument value or position offset');
}
$argument = $arguments[static::$timeoutArgumentPositionOffset];
$argumentsBefore = array_slice($arguments, 0, static::$timeoutArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$timeoutArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$timeoutModifier],
[$argument],
$argumentsAfter
));
}
}

View file

@ -0,0 +1,48 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\To;
use Predis\Command\Argument\Server\To;
trait ServerTo
{
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$toArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
/** @var To|null $toArgument */
$toArgument = $arguments[static::$toArgumentPositionOffset];
if (null === $toArgument) {
array_splice($arguments, static::$toArgumentPositionOffset, 1, [false]);
parent::setArguments($arguments);
return;
}
$argumentsBefore = array_slice($arguments, 0, static::$toArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$toArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
$toArgument->toArray(),
$argumentsAfter
));
}
}

View file

@ -0,0 +1,61 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait Weights
{
/**
* @var string
*/
private static $weightsModifier = 'WEIGHTS';
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (static::$weightsArgumentPositionOffset >= $argumentsLength) {
parent::setArguments($arguments);
return;
}
if (!is_array($arguments[static::$weightsArgumentPositionOffset])) {
throw new UnexpectedValueException('Wrong weights argument type');
}
$weightsArray = $arguments[static::$weightsArgumentPositionOffset];
if (empty($weightsArray)) {
unset($arguments[static::$weightsArgumentPositionOffset]);
parent::setArguments($arguments);
return;
}
$argumentsBefore = array_slice($arguments, 0, static::$weightsArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$weightsArgumentPositionOffset + 1);
parent::setArguments(array_merge(
$argumentsBefore,
[self::$weightsModifier],
$weightsArray,
$argumentsAfter
));
}
}

View file

@ -0,0 +1,49 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\With;
use Predis\Command\Command;
use UnexpectedValueException;
/**
* @mixin Command
*/
trait WithCoord
{
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (
static::$withCoordArgumentPositionOffset >= $argumentsLength
|| false === $arguments[static::$withCoordArgumentPositionOffset]
) {
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$withCoordArgumentPositionOffset];
if (true === $argument) {
$argument = 'WITHCOORD';
} else {
throw new UnexpectedValueException('Wrong WITHCOORD argument type');
}
$argumentsBefore = array_slice($arguments, 0, static::$withCoordArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$withCoordArgumentPositionOffset + 1);
parent::setArguments(array_merge($argumentsBefore, [$argument], $argumentsAfter));
}
}

View file

@ -0,0 +1,45 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\With;
use UnexpectedValueException;
trait WithDist
{
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (
static::$withDistArgumentPositionOffset >= $argumentsLength
|| false === $arguments[static::$withDistArgumentPositionOffset]
) {
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$withDistArgumentPositionOffset];
if (true === $argument) {
$argument = 'WITHDIST';
} else {
throw new UnexpectedValueException('Wrong WITHDIST argument type');
}
$argumentsBefore = array_slice($arguments, 0, static::$withDistArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$withDistArgumentPositionOffset + 1);
parent::setArguments(array_merge($argumentsBefore, [$argument], $argumentsAfter));
}
}

View file

@ -0,0 +1,45 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\With;
use UnexpectedValueException;
trait WithHash
{
public function setArguments(array $arguments)
{
$argumentsLength = count($arguments);
if (
static::$withHashArgumentPositionOffset >= $argumentsLength
|| false === $arguments[static::$withHashArgumentPositionOffset]
) {
parent::setArguments($arguments);
return;
}
$argument = $arguments[static::$withHashArgumentPositionOffset];
if (true === $argument) {
$argument = 'WITHHASH';
} else {
throw new UnexpectedValueException('Wrong WITHHASH argument type');
}
$argumentsBefore = array_slice($arguments, 0, static::$withHashArgumentPositionOffset);
$argumentsAfter = array_slice($arguments, static::$withHashArgumentPositionOffset + 1);
parent::setArguments(array_merge($argumentsBefore, [$argument], $argumentsAfter));
}
}

View file

@ -0,0 +1,68 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\With;
use Predis\Command\Command;
/**
* Handles last argument passed into command as WITHSCORES.
*
* @mixin Command
*/
trait WithScores
{
public function setArguments(array $arguments)
{
$withScores = array_pop($arguments);
if (is_bool($withScores) && $withScores) {
$arguments[] = 'WITHSCORES';
} elseif (!is_bool($withScores)) {
$arguments[] = $withScores;
}
parent::setArguments($arguments);
}
/**
* Checks for the presence of the WITHSCORES modifier.
*
* @return bool
*/
private function isWithScoreModifier(): bool
{
$arguments = parent::getArguments();
$lastArgument = (!empty($arguments)) ? $arguments[count($arguments) - 1] : null;
return is_string($lastArgument) && strtoupper($lastArgument) === 'WITHSCORES';
}
public function parseResponse($data)
{
if ($this->isWithScoreModifier()) {
$result = [];
for ($i = 0, $iMax = count($data); $i < $iMax; ++$i) {
if (is_array($data[$i])) {
$result[$data[$i][0]] = $data[$i][1]; // Relay
} elseif (array_key_exists($i + 1, $data)) {
$result[$data[$i]] = $data[++$i];
}
}
return $result;
}
return $data;
}
}

View file

@ -0,0 +1,34 @@
<?php
/*
* This file is part of the Predis package.
*
* (c) 2009-2020 Daniele Alessandri
* (c) 2021-2023 Till Krüss
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Predis\Command\Traits\With;
use Predis\Command\Command;
/**
* @mixin Command
*/
trait WithValues
{
public function setArguments(array $arguments)
{
$withValues = array_pop($arguments);
if (is_bool($withValues) && $withValues) {
$arguments[] = 'WITHVALUES';
} elseif (!is_bool($withValues)) {
$arguments[] = $withValues;
}
parent::setArguments($arguments);
}
}