Created
February 28, 2024 23:23
-
-
Save kmuenkel/f063cdc837a7c50e7fedd85e5e70c21a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
function compileQuery(Chess\DoctrineBundle\Doctrine\ORM\HintedQueryDecorator|\Doctrine\ORM\AbstractQuery $query) { | |
$sql = $query->getSQL(); | |
$params = array_column(array_map(function (\Doctrine\ORM\Query\Parameter $parameter) { | |
/** @var mixed|\MyCLabs\Enum\Enum $value */ | |
$value = $parameter->getValue(); | |
$type = $parameter->getType(); | |
$name = $parameter->getName(); | |
$value = match ($type) { | |
'integer' => $value, | |
2 => "'" . $value->getValue() . "'", | |
default => throw new \UnexpectedValueException("Unhandled type: $type") | |
}; | |
return compact('name', 'value'); | |
}, $query->getParameters()->toArray()), 'value', 'name'); | |
if ($query instanceof \Doctrine\ORM\Query) { | |
preg_match_all('/:(\S+)/', $query->getDQL(), $matches); | |
$sequence = $matches[1] ?? []; | |
$params = $sequence ? array_merge(array_flip($sequence), $params) : $params; | |
} | |
return preg_replace_callback('/\?/', function () use (&$params) { | |
return array_shift($params); | |
}, $sql); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment