public static function extractExpressions(array $args) { $numArgs = sizeof($args); if (!$numArgs) { throw new Exception('Not enough arguments'); } if ($numArgs > 2) { throw new Exception('Too many arguments'); } $fieldNameOrPrepared = $args[0]; $valueOrBinds = null; if ($numArgs > 1) { $valueOrBinds = $args[1]; } $expressions = []; if (is_string($fieldNameOrPrepared)) { /* * Агрументы либо (prepared, binds), либо (field, value) */ if (PlainSql::hasPlaceholders($fieldNameOrPrepared) || $numArgs === 1) { if ($numArgs == 1) { $valueOrBinds = []; } else { if (!is_array($valueOrBinds)) { throw new Exception('Binds must be an array'); } } $expressions[] = self::prepared($fieldNameOrPrepared, $valueOrBinds); } else { if ($numArgs !== 2) { throw new Exception('Exactly 2 arguments expected'); } $expressions[] = self::fieldEqual($fieldNameOrPrepared, $valueOrBinds); } } else { if ($fieldNameOrPrepared instanceof Literal) { /* * Аргумент - литерал. Дополнительных аргументов не нужно */ if ($numArgs !== 1) { throw new Exception('Two-arguments form is not allowed when Literal given'); } $expressions[] = $fieldNameOrPrepared; } else { if (is_array($fieldNameOrPrepared)) { /* * Аргумент - массив соответствий типа [field1 => value1, field2 => value2] */ if ($numArgs !== 1) { throw new Exception('Exactly one argument expected'); } foreach ($fieldNameOrPrepared as $field => $value) { $expressions[] = self::fieldEqual($field, $value); } } else { throw new Exception('Incorrect expression definition'); } } } return $expressions; }
/** * @expectedException b2\Exception * @expectedExceptionMessage Literal expected, but AnyList found */ public function testListUnnamedBindsButNotList() { $b = new PlainSql('?', [new \b2\literal\AnyList([new Constant(1), new Constant(2)])]); $b->toString($this->quoter()); }