public function _bindInto($pattern, $sql, $params, $func)
{
$result = NULL;
// http://stackoverflow.com/questions/5695240/php-regex-to-ignore-escaped-quotes-within-quotes
// this could be done with back refs, but this is much faster
$regex = "/('[^'\\\\]*(?:\\\\.[^'\\\\]*)*'|\"[^\"\\\\]*(?:\\\\.[^\"\\\\]*)*\"|{$pattern})/";
foreach (preg_split($regex, $sql, -1, PREG_SPLIT_DELIM_CAPTURE | PREG_SPLIT_NO_EMPTY) as $token) {
if ($token == '?' || $token[0] != '"' && $token[0] != "'" && preg_match("/{$pattern}/", $token)) {
if (!count($params)) {
throw new \InvalidArgumentException("Not enough parameters to bind({$sql})");
}
$result .= $func($this, array_shift($params), $token);
} else {
$result .= $token;
}
}
if (count($params)) {
$exception = new \InvalidArgumentException("Parameters left over in bind({$sql})");
$exception->leftOverParams = $params;
throw $exception;
}
return $result;
}