public function hasSimilarName($str, $strict = false) { $str = OrgTable::removeSuffixes($str); $str = trim($str); if (!strlen($str)) { return false; } $terms = LsQuery::splitSearchPhrase($str); $matched = false; $names = $this->Entity->getAllNames(); foreach ($terms as &$term) { if (is_array($term)) { foreach ($term as &$t) { $t = LsString::escapeStringForRegex($t); } $term = implode('|', $term); } else { $term = LsString::escapeStringForRegex($term); } } unset($term); if ($terms[0] == 'The') { array_shift($terms); } foreach ($names as $name) { $matched = true; if (!preg_match('/^(The\\s+)?(' . $terms[0] . ')/isu', $name)) { $matched = false; continue; } foreach ($terms as $term) { $new = preg_replace('/((^|\\s)|\\b)(' . $term . ')(\\b|(\\s|$))/isu', ' ', $name, 1); if ($new == $name) { $matched = false; continue; } $name = $new; } $name = trim(OrgTable::removeSuffixes($name)); if ($strict && $matched && strlen($name) > 0 && count(LsString::split($name)) >= $strict) { $matched = false; } if ($matched == true) { break; } } return $matched; }
static function getOrgsWithSimilarNames($name, $strict = false) { $name = trim(OrgTable::removeSuffixes($name, $exclude = array('Bancorp'))); if (strlen($name) < 3) { return array(); } $terms = LsQuery::splitSearchPhrase($name); $q = EntityTable::getByExtensionQuery('Org')->leftJoin('e.Alias a'); $search_terms = array(); $e = array(); $a = array(); for ($i = 0; $i < count($terms); $i++) { $term = $terms[$i]; if (is_array($term)) { $e_temp = array(); $a_temp = array(); foreach ($term as $t) { $search_terms[] = $i == 0 ? $t . '%' : '%' . $t . '%'; $e_temp[] = 'e.name like ?'; $a_temp[] = 'a.name like ?'; } $e[] = '(' . implode(' or ', $e_temp) . ')'; $a[] = '(' . implode(' or ', $a_temp) . ')'; } else { $search_terms[] = $i == 0 ? $term . '%' : '%' . $term . '%'; $e[] = 'e.name like ?'; $a[] = 'a.name like ?'; } } $e = implode(' and ', $e); $a = implode(' and ', $a); $search_terms = array_merge($search_terms, $search_terms); $q->addWhere('(' . $e . ') or (' . $a . ')', $search_terms); $orgs = $q->execute(); $org_names = array(); $found_orgs = array(); foreach ($orgs as $org) { if ($org->hasSimilarName($name, $strict)) { $found_orgs[] = $org; $org_names[] = $org->name; } } $org_names = array_unique($org_names); return $found_orgs; }