mirror of
https://github.com/Lastorder-DC/rhymix.git
synced 2026-05-22 05:15:29 +09:00
키워드 처리를 별도 함수로 분리
This commit is contained in:
parent
0c08e92a54
commit
a0449a2e0f
1 changed files with 70 additions and 51 deletions
|
|
@ -237,57 +237,9 @@ class VariableBase
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'search':
|
case 'search':
|
||||||
$keywords = preg_split('/("[^"]*")|[\s,]+/', $value, 10, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE);
|
$parsed_keywords = $this->_parseSearchKeywords($value)
|
||||||
$conditions = array();
|
$where = $parsed_keywords->where;
|
||||||
$operators = array('AND', 'OR', '|');
|
$params = $parsed_keywords->params;
|
||||||
$placeholders = implode(', ', array_fill(0, count($keywords), '?'));
|
|
||||||
foreach ($keywords as $item)
|
|
||||||
{
|
|
||||||
// trim quotation mark
|
|
||||||
if (strlen($item) > 2 && (substr($item, 0, 1) === substr($item, -1)) && substr($item, -1) === '"')
|
|
||||||
{
|
|
||||||
$item = substr($item, 1, -1);
|
|
||||||
}
|
|
||||||
elseif (strlen($item) > 3 && (substr($item, 0, 1) === '-') && (substr($item, 1, 1) === substr($item, -1)) && substr($item, -1) === '"')
|
|
||||||
{
|
|
||||||
$item = '-' . substr($item, 2, -1);
|
|
||||||
}
|
|
||||||
|
|
||||||
// pass blank text
|
|
||||||
if (trim($item) === "")
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
// process 'AND' or 'OR' operator
|
|
||||||
if (in_array($item, $operators))
|
|
||||||
{
|
|
||||||
if ($item === '|')
|
|
||||||
{
|
|
||||||
$item = 'OR';
|
|
||||||
}
|
|
||||||
$conditions[] = sprintf('%s', $item);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
if (substr($item, 0, 1) === '-')
|
|
||||||
{
|
|
||||||
$conditions[] = sprintf('%s NOT LIKE ?', $column);
|
|
||||||
$item = substr($item, 1);
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
$conditions[] = sprintf('%s LIKE ?', $column);
|
|
||||||
}
|
|
||||||
$params[] = '%' . str_replace(['\\', '_', '%'], ['\\\\', '\_', '\%'], $item) . '%';
|
|
||||||
// if there is no operator, assume 'AND'
|
|
||||||
$conditions[] = 'AND';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// remove the last point (would be an operator)
|
|
||||||
array_pop($conditions)
|
|
||||||
$conditions = implode(' ', $conditions);
|
|
||||||
$where = count($keywords) === 1 ? $conditions : "($conditions)";
|
|
||||||
break;
|
break;
|
||||||
case 'plus':
|
case 'plus':
|
||||||
$where = sprintf('%s = %s + %s', $column, $column, $is_expression ? $value : '?');
|
$where = sprintf('%s = %s + %s', $column, $column, $is_expression ? $value : '?');
|
||||||
|
|
@ -477,4 +429,71 @@ class VariableBase
|
||||||
throw new \Rhymix\Framework\Exceptions\QueryError('Variable ' . $this->var . ' for column ' . $column . ' must contain no more than ' . $this->minlength . ' characters');
|
throw new \Rhymix\Framework\Exceptions\QueryError('Variable ' . $this->var . ' for column ' . $column . ' must contain no more than ' . $this->minlength . ' characters');
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Parse the search text.
|
||||||
|
*
|
||||||
|
* @param string $value
|
||||||
|
* @return object
|
||||||
|
*/
|
||||||
|
private function _parseSearchKeywords() ($value)
|
||||||
|
{
|
||||||
|
// parse the value (text)
|
||||||
|
$keywords = preg_split('/("[^"]*")|[\s,]+/', trim($value), 10, \PREG_SPLIT_NO_EMPTY | \PREG_SPLIT_DELIM_CAPTURE);
|
||||||
|
$conditions = array();
|
||||||
|
|
||||||
|
// loop the parsed keywords or operators
|
||||||
|
foreach ($keywords as $item)
|
||||||
|
{
|
||||||
|
// trim quotation mark
|
||||||
|
if (strlen($item) > 2 && (substr($item, 0, 1) === substr($item, -1)) && substr($item, -1) === '"')
|
||||||
|
{
|
||||||
|
$item = substr($item, 1, -1);
|
||||||
|
}
|
||||||
|
elseif (strlen($item) > 3 && (substr($item, 0, 1) === '-') && (substr($item, 1, 1) === substr($item, -1)) && substr($item, -1) === '"')
|
||||||
|
{
|
||||||
|
$item = '-' . substr($item, 2, -1);
|
||||||
|
}
|
||||||
|
|
||||||
|
// pass blank text
|
||||||
|
if (trim($item) === "")
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
// process 'AND' or 'OR' operator
|
||||||
|
if (in_array($item, $operators))
|
||||||
|
{
|
||||||
|
if ($item === '|')
|
||||||
|
{
|
||||||
|
$item = 'OR';
|
||||||
|
}
|
||||||
|
$conditions[] = sprintf('%s', $item);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
if (substr($item, 0, 1) === '-')
|
||||||
|
{
|
||||||
|
$conditions[] = sprintf('%s NOT LIKE ?', $column);
|
||||||
|
$item = substr($item, 1);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
$conditions[] = sprintf('%s LIKE ?', $column);
|
||||||
|
}
|
||||||
|
$params[] = '%' . str_replace(['\\', '_', '%'], ['\\\\', '\_', '\%'], $item) . '%';
|
||||||
|
// if there is no operator, assume 'AND'
|
||||||
|
$conditions[] = 'AND';
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// remove the last point (would be an operator)
|
||||||
|
array_pop($conditions);
|
||||||
|
$conditions = implode(' ', $conditions);
|
||||||
|
$where = count($keywords) === 1 ? $conditions : "($conditions)";
|
||||||
|
|
||||||
|
return (object) array (
|
||||||
|
'where' => $where,
|
||||||
|
'params' => $params
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue