Elastica\Transport\AbstractTransport::create PHP Метод

create() публичный статический Метод

The $transport parameter can be one of the following values: * string: The short name of a transport. For instance "Http" * object: An already instantiated instance of a transport * array: An array with a "type" key which must be set to one of the two options. All other keys in the array will be set as parameters in the transport instance
public static create ( mixed $transport, Connection $connection, array $params = [] ) : AbstractTransport
$transport mixed A transport definition
$connection Elastica\Connection A connection instance
$params array Parameters for the transport class
Результат AbstractTransport
    public static function create($transport, Connection $connection, array $params = [])
    {
        if (is_array($transport) && isset($transport['type'])) {
            $transportParams = $transport;
            unset($transportParams['type']);
            $params = array_replace($params, $transportParams);
            $transport = $transport['type'];
        }
        if (is_string($transport)) {
            $specialTransports = ['httpadapter' => 'HttpAdapter', 'nulltransport' => 'NullTransport'];
            if (isset($specialTransports[strtolower($transport)])) {
                $transport = $specialTransports[strtolower($transport)];
            } else {
                $transport = ucfirst($transport);
            }
            $classNames = ["Elastica\\Transport\\{$transport}", $transport];
            foreach ($classNames as $className) {
                if (class_exists($className)) {
                    $transport = new $className();
                    break;
                }
            }
        }
        if ($transport instanceof self) {
            $transport->setConnection($connection);
            foreach ($params as $key => $value) {
                $transport->setParam($key, $value);
            }
        } else {
            throw new InvalidException('Invalid transport');
        }
        return $transport;
    }

Usage Example

 public function testCanInjectParamsWhenUsingArray()
 {
     $connection = new Connection();
     $params = array('param1' => 'some value', 'param3' => 'value3');
     $transport = AbstractTransport::create(array('type' => 'Http', 'param1' => 'value1', 'param2' => 'value2'), $connection, $params);
     $this->assertSame('value1', $transport->getParam('param1'));
     $this->assertSame('value2', $transport->getParam('param2'));
     $this->assertSame('value3', $transport->getParam('param3'));
 }
All Usage Examples Of Elastica\Transport\AbstractTransport::create