/** * Return delegate that maps individual characters in string with $iterator * * @param callable $iterator * @return callable */ function str_map_dg($iterator) { return function ($string) use($iterator) { return str_map($string, $iterator); }; }
/** * @param string $term * @param array $eqCol * @param array $likeCol * @param array $additionalSelectCol * @return $this */ public function buildSearchCondition($term, array $eqCol = array(), array $likeCol = array(), array $additionalSelectCol = array()) { $collate = 'utf8_general_ci'; $mappedTerm = str_map($term, function ($char) { return ctype_alnum($char) ? $char : ' '; }); $db = $this->getDb(); $firstOne = true; $where = array_chain(explode(' ', $mappedTerm), array_filter_key_dg(not_dg(empty_dg())), array_map_val_dg(function ($termPart) use($db, $likeCol, $eqCol, $collate, &$firstOne) { if ($firstOne) { $t1 = $db->quote("{$termPart}%", 'string'); $firstOne = false; } else { $t1 = $db->quote("%{$termPart}%", 'string'); } $t2 = $db->quote($termPart, 'string'); $whereLike = array_map_val($likeCol, function ($column) use($collate, $termPart, $t1) { return "{$column} COLLATE {$collate} LIKE {$t1}"; }); $whereEq = array_map_val($eqCol, function ($column) use($termPart, $t2) { return "{$column} = {$t2}"; }); $where = array_merge($whereLike, $whereEq); return '( ' . implode(' OR ', $where) . ' )'; })); $this->setColumns(array_merge($eqCol, $likeCol, $additionalSelectCol))->setWhere(implode(" AND ", $where)); return $this; }