add line break to matches in "." . '/s'; /** @var string lookup for only numbered placeholders */ public const REGEX_LOOKUP_NUMBERED = '/' . self::PATTERN_COMMENT . '|' . self::PATTERN_TEXT_BLOCK_SINGLE_QUOTE . '|' . self::PATTERN_TEXT_BLOCK_DOLLAR . '|' // match for replace part . '(?:' // $n numbered part (\PG php) [1] . self::PATTERN_NUMBERED // end match . ')' . '/s'; /** @var int position for regex in full placeholder lookup: named */ public const LOOOKUP_NAMED_POS = 2; /** @var int position for regex in full placeholder lookup: question mark */ public const LOOOKUP_QUESTION_MARK_POS = 3; /** @var int position for regex in full placeholder lookup: numbered */ public const LOOOKUP_NUMBERED_POS = 4; /** @var int matches position for replacement and single lookup */ public const MATCHING_POS = 2; /** * Convert PDO type query with placeholders to \PG style and vica versa * For PDO to: ? and :named * For \PG to: $number * * If the query has a mix of ?, :named or $numbrer the \OutOfRangeException exception * will be thrown * * If the convert_to is either pg or pdo, nothing will be changed * * found has -1 if an error occoured in the preg_match_all call * * @param string $query Query with placeholders to convert * @param ?array $params The parameters that are used for the query, and will be updated * @param string $convert_to Either pdo or pg, will be converted to lower case for check * @return array{original:array{query:string,params:array,empty_params:bool},type:''|'named'|'numbered'|'question_mark',found:int,matches:array,params_lookup:array,query:string,params:array} * @throws \OutOfRangeException 200 If mixed placeholder types * @throws \InvalidArgumentException 300 or 301 if wrong convert to with found placeholders */ public static function convertPlaceholderInQuery( string $query, ?array $params, string $convert_to = 'pg' ): array { $convert_to = strtolower($convert_to); $matches = []; // matches: // 1: :named // 2: ? question mark // 3: $n numbered $found = preg_match_all(self::REGEX_LOOKUP_PLACEHOLDERS, $query, $matches, PREG_UNMATCHED_AS_NULL); // if false or null set to -1 // || $found === null if ($found === false) { $found = -1; } /** @var array 1: named */ $named_matches = array_filter($matches[self::LOOOKUP_NAMED_POS]); /** @var array 2: open ? */ $qmark_matches = array_filter($matches[self::LOOOKUP_QUESTION_MARK_POS]); /** @var array 3: $n matches */ $numbered_matches = array_filter($matches[self::LOOOKUP_NUMBERED_POS]); // print "**MATCHES**:
" . print_r($matches, true) . "
"; // count matches $count_named = count(array_unique($named_matches)); $count_qmark = count($qmark_matches); $count_numbered = count(array_unique($numbered_matches)); // throw exception if mixed found if ( ($count_named && $count_qmark) || ($count_named && $count_numbered) || ($count_qmark && $count_numbered) ) { throw new \OutOfRangeException('Cannot have named, question mark and numbered in the same query', 200); } // // throw if invalid conversion // if (($count_named || $count_qmark) && $convert_to != 'pg') { // throw new \InvalidArgumentException('Cannot convert from named or question mark placeholders to PDO', 300); // } // if ($count_numbered && $convert_to != 'pdo') { // throw new \InvalidArgumentException('Cannot convert from numbered placeholders to Pg', 301); // } // return array $return_placeholders = [ // original 'original' => [ 'query' => $query, 'params' => $params ?? [], 'empty_params' => $params === null ? true : false, ], // type found, empty if nothing was done 'type' => '', // int: found, not found; -1: problem (set from false) 'found' => (int)$found, 'matches' => [], // old to new lookup check 'params_lookup' => [], // this must match the count in params in new 'needed' => 0, // new 'query' => '', 'params' => [], ]; // replace basic regex and name settings if ($count_named) { $return_placeholders['type'] = 'named'; $return_placeholders['matches'] = $named_matches; $return_placeholders['needed'] = $count_named; } elseif ($count_qmark) { $return_placeholders['type'] = 'question_mark'; $return_placeholders['matches'] = $qmark_matches; $return_placeholders['needed'] = $count_qmark; // for each ?:DTN: -> replace with $1 ... $n, any remaining :DTN: remove } elseif ($count_numbered) { $return_placeholders['type'] = 'numbered'; $return_placeholders['matches'] = $numbered_matches; $return_placeholders['needed'] = $count_numbered; } // run convert only if matching type and direction if ( (($count_named || $count_qmark) && $convert_to == 'pg') || ($count_numbered && $convert_to == 'pdo') ) { $param_list = self::updateParamList($return_placeholders); $return_placeholders['params_lookup'] = $param_list['params_lookup']; $return_placeholders['query'] = $param_list['query']; $return_placeholders['params'] = $param_list['params']; } // return data return $return_placeholders; } /** * Updates the params list from one style to the other to match the query output * if original.empty_params is set to true, no params replacement is done * if param replacement has been done in a dbPrepare then this has to be run * with the return palceholders array with params in original filled and empty_params turned off * * phpcs:disable Generic.Files.LineLength * @param array{original:array{query:string,params:array,empty_params:bool},type:''|'named'|'numbered'|'question_mark',found:int,matches?:array,params_lookup?:array,query?:string,params?:array} $converted_placeholders * phpcs:enable Generic.Files.LineLength * @return array{params_lookup:array,query:string,params:array} */ public static function updateParamList(array $converted_placeholders): array { // skip if nothing set if (!$converted_placeholders['found']) { return [ 'params_lookup' => [], 'query' => '', 'params' => [] ]; } $query_new = ''; $params_new = []; $params_lookup = []; // set to null if params is empty $params = $converted_placeholders['original']['params']; $empty_params = $converted_placeholders['original']['empty_params']; switch ($converted_placeholders['type']) { case 'named': // 1: replace part :named $pos = 0; $query_new = preg_replace_callback( self::REGEX_REPLACE_NAMED, function ($matches) use (&$pos, &$params_new, &$params_lookup, $params, $empty_params) { if (!isset($matches[self::MATCHING_POS])) { throw new \RuntimeException( 'Cannot lookup ' . self::MATCHING_POS . ' in matches list', 209 ); } $match = $matches[self::MATCHING_POS]; // only count up if $match[1] is not yet in lookup table if (empty($params_lookup[$match])) { $pos++; $params_lookup[$match] = '$' . $pos; // skip params setup if param list is empty if (!$empty_params) { $params_new[] = $params[$match] ?? throw new \RuntimeException( 'Cannot lookup ' . $match . ' in params list', 210 ); } } // add the connectors back (1), and the data sets only if no replacement will be done return $params_lookup[$match] ?? throw new \RuntimeException( 'Cannot lookup ' . $match . ' in params lookup list', 211 ); }, $converted_placeholders['original']['query'] ); break; case 'question_mark': if (!$empty_params) { // order and data stays the same $params_new = $params ?? []; } // 1: replace part ? $pos = 0; $query_new = preg_replace_callback( self::REGEX_REPLACE_QUESTION_MARK, function ($matches) use (&$pos, &$params_lookup) { if (!isset($matches[self::MATCHING_POS])) { throw new \RuntimeException( 'Cannot lookup ' . self::MATCHING_POS . ' in matches list', 229 ); } $match = $matches[self::MATCHING_POS]; // only count pos up for actual replacements we will do if (!empty($match)) { $pos++; $params_lookup[] = '$' . $pos; } // add the connectors back (1), and the data sets only if no replacement will be done return '$' . $pos; }, $converted_placeholders['original']['query'] ); break; case 'numbered': // 1: replace part $numbered $pos = 0; $query_new = preg_replace_callback( self::REGEX_REPLACE_NUMBERED, function ($matches) use (&$pos, &$params_new, &$params_lookup, $params, $empty_params) { if (!isset($matches[self::MATCHING_POS])) { throw new \RuntimeException( 'Cannot lookup ' . self::MATCHING_POS . ' in matches list', 239 ); } $match = $matches[self::MATCHING_POS]; // only count up if $match[1] is not yet in lookup table if (empty($params_lookup[$match])) { $pos++; $params_lookup[$match] = ':' . $pos . '_named'; // skip params setup if param list is empty if (!$empty_params) { $params_new[] = $params[($pos - 1)] ?? throw new \RuntimeException( 'Cannot lookup ' . ($pos - 1) . ' in params list', 230 ); } } // add the connectors back (1), and the data sets only if no replacement will be done return $params_lookup[$match] ?? throw new \RuntimeException( 'Cannot lookup ' . $match . ' in params lookup list', 231 ); }, $converted_placeholders['original']['query'] ); break; } return [ 'params_lookup' => $params_lookup, 'query' => $query_new ?? '', 'params' => $params_new, ]; } } // __END__