21 declare(strict_types=1);
97 $className = $classMap[$dialect];
113 uksort($this->variables,
function($s1, $s2){
121 for($i = 1, $iMax =
mb_strlen($this->query); $i < $iMax; ++$i){
122 $thisChar =
mb_substr($this->query, $i, 1);
124 if($quotesState !== null){
125 if($thisChar ===
"\\"){
129 if($thisChar === $quotesState){
135 if(
in_array($thisChar, [
"'",
"\"",
"`"],
true)){
136 $quotesState = $thisChar;
140 if($thisChar ===
":"){
143 foreach($this->variables as $variable){
144 if(
mb_strpos($this->query, $variable->getName(), $i + 1) === $i + 1){
145 $positions[$i] =
$name = $variable->getName();
152 $usedNames[
$name] =
true;
160 foreach($positions as $pos =>
$name){
161 $newQuery .=
mb_substr($this->query, $lastPos, $pos - $lastPos);
165 $newQuery .=
mb_substr($this->query, $lastPos);
167 $this->query = $newQuery;
169 foreach($this->variables as $variable){
170 if(!isset($usedNames[$variable->getName()])){
171 throw new InvalidArgumentException(
"The variable {$variable->getName()} is not used anywhere in the query! Check for typos.");
176 public function format(array $vars, ?
string $placeHolder, ?array &$outArgs) : string{
178 foreach($this->variables as $variable){
179 if(!$variable->isOptional() && !
array_key_exists($variable->getName(), $vars)){
187 foreach($this->varPositions as $pos =>
$name){
189 $value = $vars[
$name] ?? $this->variables[
$name]->getDefault();
193 throw new InvalidArgumentException(
"Invalid value for :$name - " . $e->getMessage() .
", " . self::getType($value) .
" given", 0, $e);
202 private static function getType($value){
format(array $vars, ?string $placeHolder, ?array &$outArgs)
__construct(string $name, string $query, string $doc, array $variables, ?string $file, int $lineNo)
static forDialect(string $dialect, string $name, string $query, string $doc, array $variables, ?string $file, int $lineNo)
formatVariable(GenericVariable $variable, $value, ?string $placeHolder, array &$outArgs)