Skip to content

Commit

Permalink
Merge pull request #2 from mag310/add-bind-tuple
Browse files Browse the repository at this point in the history
Добавил тип bind-а для поддержки кортежей
  • Loading branch information
miramir authored May 28, 2018
2 parents 9d716e9 + b7ea1fb commit 1da1cd6
Showing 1 changed file with 53 additions and 6 deletions.
59 changes: 53 additions & 6 deletions Parser.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,16 +76,38 @@ private function simplifyParams($params)
foreach ($params as $key => $value) {
$key = ':' . ltrim($key, ':');
if (is_array($value)) {
if (isset($value[0]) && is_array($value[0])) {
foreach ($value[0] as $valKey => $valVal) {
$newParams[$key . '_' . $valKey] = $valVal;
}
} elseif (!isset($value['bind']) || $value['bind'] === true) {
if (!isset($value['bind'])) {
$value['bind'] = true;
}

if ($value['bind'] === true) {
if (isset($value[0]) && isset($value[1])) {
$newParams[$key] = [$value[0], $value[1]];
} elseif (isset($value[0])) {
$newParams[$key] = $value[0];
}
} elseif ($value['bind'] === 'tuple') {

if (isset($value[0]) && is_array($value[0])) {
//скинем индексы
$value[0] = array_values($value[0]);

foreach ($value[0] as $valKey => $valVal) {
if (is_array($valVal)) {
foreach ($valVal as $k => $v) {
$newParams[$key . '_' . $valKey . '_' . $k] = $v;
}
} else {
$newParams[$key . '_' . $valKey] = $valVal;
}
}
} elseif (isset($value[0])) {
$newParams[$key] = $value[0];
}
} elseif (isset($value[0]) && is_array($value[0])) {
foreach ($value[0] as $valKey => $valVal) {
$newParams[$key . '_' . $valKey] = $valVal;
}
}
} else {
$newParams[$key] = $value;
Expand Down Expand Up @@ -152,6 +174,30 @@ private function replaceComment($comment, $queryInComment, $paramName)
$queryInComment = preg_replace('/:@' . preg_quote($paramName) . '/', $replacement, $queryInComment);
} elseif ($bind === 'text') {
$queryInComment = preg_replace('/' . preg_quote($paramName) . '/', $value, $queryInComment);
} elseif ($bind === 'tuple') {
if (is_array($paramValue[0])) {
$replacements = [];
//скинем индексы
$paramValue[0] = array_values($paramValue[0]);

foreach ($paramValue[0] as $keyParam => $val) {
$name = ':' . $paramName . '_' . $keyParam;
if (is_array($val)) {
$valArr = [];
foreach (array_keys($val) as $keyVal) {
$valArr[] = $name . '_' . $keyVal;
}
$valName = implode(',', $valArr);
} else {
$valName = $name;
}
$replacements[] = '(' . $valName . ')';
}
$replacement = implode(',', $replacements);
} else {
$replacement = $paramValue;
}
$queryInComment = preg_replace('/:@' . preg_quote($paramName) . '/', $replacement, $queryInComment);
}
} else {
$queryInComment = '';
Expand All @@ -163,7 +209,8 @@ private function replaceComment($comment, $queryInComment, $paramName)
/**
* Ищет параметр в массиве $this->params
* @param string $name имя параметра
* @return array|bool массив ['имя_параметра_без_ведущего_двоеточия', 'значение_параметра'] или ложь если параметра нет
* @return array|bool массив ['имя_параметра_без_ведущего_двоеточия', 'значение_параметра'] или ложь если параметра
* нет
*/
private function getParam($name)
{
Expand Down

0 comments on commit 1da1cd6

Please sign in to comment.