Predis\Client::getClientBy PHP Method

getClientBy() public method

The new client instances inherites the same options of the original one. When no callable object is supplied, this method returns the new client. When a callable object is supplied, the new client is passed as its sole argument and its return value is returned by this method to the caller. NOTE: This method works against any kind of underlying connection object as it uses a duck-typing approach and looks for a suitable method that matches the selector type to extract the correct connection.
public getClientBy ( string $selector, string $value, callable | null $callable = null ) : predis\ClientInterface | mixed
$selector string Type of selector (`id`, `key`, `slot`, `command`)
$value string Values of selector.
$callable callable | null Optional callable object.
return predis\ClientInterface | mixed
    public function getClientBy($selector, $value, $callable = null)
    {
        $selector = strtolower($selector);
        if (!in_array($selector, array('id', 'key', 'slot', 'role', 'alias', 'command'))) {
            throw new \InvalidArgumentException("Invalid selector type: `{$selector}`");
        }
        if (!method_exists($this->connection, $method = "getConnectionBy{$selector}")) {
            $class = get_class($this->connection);
            throw new \InvalidArgumentException("Selecting connection by {$selector} is not supported by {$class}");
        }
        if (!($connection = $this->connection->{$method}($value))) {
            throw new \InvalidArgumentException("Cannot find a connection by {$selector} matching `{$value}`");
        }
        $client = new static($connection, $this->getOptions());
        if ($callable) {
            return call_user_func($callable, $client);
        } else {
            return $client;
        }
    }