/** * Turns Criterion into IN Sql Clause * * @param Criterion $criterion Criterion to process * @param string $placeHolder Placeholder for parameter name * @param array $fieldMap Array representing field map */ public static function criterionToInClause(Criterion $criterion, $placeHolder = 'filter_0', $fieldMap = array()) { // Create local operand and collate $op = false; $collate = false; $clause = new Clause(); switch ($criterion->op()) { case 'in': $op = 'IN'; $collate = 'utf8_bin'; break; case 'nin': $op = 'NOT IN'; $collate = 'utf8_bin'; break; case 'ini': $op = 'IN'; $collate = 'utf8_general_ci'; break; case 'nini': $op = 'NOT IN'; $collate = 'utf8_general_ci'; break; } if ($op && $collate) { if ($criterion->type() == Criterion::CRITERION_TYPE_VALUE) { // Values should be in a comma separated list. $values = explode(',', $criterion->value()); // We must add a parameter and a placeholder for each value. $placeHolders = array(); foreach ($values as $key => $value) { $clause->setParameter($placeHolder . '_' . $key, $value); $placeHolders[] = ':' . $placeHolder . '_' . $key . ' COLLATE ' . $collate; } // The comparand for this operation is the list of values. $comparand = '(' . implode(', ', $placeHolders) . ')'; } elseif ($criterion->type() == Criterion::CRITERION_TYPE_FIELD) { // Field names should be in a comma separated list. $fList = explode(',', $criterion->value()); foreach (array_keys($fList) as $key) { $fList[$key] = self::getSafeFieldName($fList[$key], $fieldMap) . ' COLLATE ' . $collate; } // The comparand for this operation is the list of field names. $comparand = '(' . implode(', ', $fList) . ')'; } // Escape, quote and qualify the field name for security. $field = self::getSafeFieldName($criterion->key(), $fieldMap); // Build the final clause. // Use end wild-card character for "begins with". $clause->setStatement(self::addLogic($criterion) . ' ' . $field . ' ' . $op . ' ' . $comparand); } return $clause; }
/** * Decides which transformation method use for given operand * * @param Criterion $criterion * @return string | false Return method name or false if method can not be determined */ public static function criterionOperandToMethod(Criterion $criterion) { switch ($criterion->op()) { // Boolean check to see if the value is set or not. case 'bool': $method = 'criterionToBool'; break; // Direct comparison checks. // Direct comparison checks. case 'eq': // equals // equals case 'ne': // does not equal // does not equal case 'eqi': // equals (case insensitive) // equals (case insensitive) case 'nei': // does not equal (case insensitive) $method = 'criterionToDirect'; break; // Relative comparison checks. // Relative comparison checks. case 'gt': // greater than // greater than case 'ge': // greater than or equal to // greater than or equal to case 'lt': // less than // less than case 'le': // less than or equal to $method = 'criterionToRelative'; break; // Wildcard comparison checks (contains/includes) // Wildcard comparison checks (contains/includes) case 'inc': // includes // includes case 'ninc': // does not include // does not include case 'inci': // includes (case insensitive) // includes (case insensitive) case 'ninci': // does not include (case insensitive) $method = 'criterionToContains'; break; // Wildcard comparison checks (begins with) // Wildcard comparison checks (begins with) case 'begins': // begins with // begins with case 'nbegins': // does not begin with // does not begin with case 'beginsi': // begins with (case insensitive) // begins with (case insensitive) case 'nbeginsi': // does not begin with (case insensitive) $method = 'criterionToBegins'; break; // Regex match // Regex match case 're': // matches regex string $method = 'criterionToRegex'; break; // Check for a list of values (match or no match). // Check for a list of values (match or no match). case 'in': // is in the list // is in the list case 'nin': // is not in the list // is not in the list case 'ini': // is in the list (case insensitive) // is in the list (case insensitive) case 'nini': // is not in the list (case insensitive) $method = 'criterionToIn'; break; default: $method = false; } return $method; }