Saves allocating an empty array.
foreach ($aWordsetSearches as $oCurrentSearch) {
foreach ($oValidTokens->get($sToken) as $oSearchTerm) {
- $aNewSearches = $oSearchTerm->extendSearch(
- $oCurrentSearch,
- $oPosition
- );
-
- foreach ($aNewSearches as $oSearch) {
- if ($oSearch->getRank() < $this->iMaxRank) {
- $aNewWordsetSearches[] = $oSearch;
+ if ($oSearchTerm->isExtendable($oCurrentSearch, $oPosition)) {
+ $aNewSearches = $oSearchTerm->extendSearch(
+ $oCurrentSearch,
+ $oPosition
+ );
+
+ foreach ($aNewSearches as $oSearch) {
+ if ($oSearch->getRank() < $this->iMaxRank) {
+ $aNewWordsetSearches[] = $oSearch;
+ }
}
}
}
return $this->iId;
}
+ /**
+ * Check if the token can be added to the given search.
+ * Derive new searches by adding this token to an existing search.
+ *
+ * @param object $oSearch Partial search description derived so far.
+ * @param object $oPosition Description of the token position within
+ the query.
+ *
+ * @return True if the token is compatible with the search configuration
+ * given the position.
+ */
+ public function isExtendable($oSearch, $oPosition)
+ {
+ return !$oSearch->hasCountry() && $oPosition->maybePhrase('country');
+ }
+
/**
* Derive new searches by adding this token to an existing search.
*
*/
public function extendSearch($oSearch, $oPosition)
{
- if ($oSearch->hasCountry() || !$oPosition->maybePhrase('country')) {
- return array();
- }
-
$oNewSearch = $oSearch->clone($oPosition->isLastToken() ? 1 : 6);
$oNewSearch->setCountry($this->sCountryCode);
return $this->iId;
}
+ /**
+ * Check if the token can be added to the given search.
+ * Derive new searches by adding this token to an existing search.
+ *
+ * @param object $oSearch Partial search description derived so far.
+ * @param object $oPosition Description of the token position within
+ the query.
+ *
+ * @return True if the token is compatible with the search configuration
+ * given the position.
+ */
+ public function isExtendable($oSearch, $oPosition)
+ {
+ return !$oSearch->hasHousenumber()
+ && !$oSearch->hasOperator(\Nominatim\Operator::POSTCODE)
+ && $oPosition->maybePhrase('street');
+ }
+
/**
* Derive new searches by adding this token to an existing search.
*
{
$aNewSearches = array();
- if ($oSearch->hasHousenumber()
- || $oSearch->hasOperator(\Nominatim\Operator::POSTCODE)
- || !$oPosition->maybePhrase('street')
- ) {
- return $aNewSearches;
- }
-
// sanity check: if the housenumber is not mainly made
// up of numbers, add a penalty
$iSearchCost = 1;
return $this->iId;
}
+ /**
+ * Check if the token can be added to the given search.
+ * Derive new searches by adding this token to an existing search.
+ *
+ * @param object $oSearch Partial search description derived so far.
+ * @param object $oPosition Description of the token position within
+ the query.
+ *
+ * @return True if the token is compatible with the search configuration
+ * given the position.
+ */
+ public function isExtendable($oSearch, $oPosition)
+ {
+ return !$oPosition->isPhrase('country');
+ }
+
/**
* Derive new searches by adding this token to an existing search.
*
*/
public function extendSearch($oSearch, $oPosition)
{
- if ($oPosition->isPhrase('country')) {
- return array();
- }
-
$aNewSearches = array();
// Partial token in Address.
return $this->iId;
}
+ /**
+ * Check if the token can be added to the given search.
+ * Derive new searches by adding this token to an existing search.
+ *
+ * @param object $oSearch Partial search description derived so far.
+ * @param object $oPosition Description of the token position within
+ the query.
+ *
+ * @return True if the token is compatible with the search configuration
+ * given the position.
+ */
+ public function isExtendable($oSearch, $oPosition)
+ {
+ return !$oSearch->hasPostcode() && $oPosition->maybePhrase('postalcode');
+ }
+
/**
* Derive new searches by adding this token to an existing search.
*
{
$aNewSearches = array();
- if ($oSearch->hasPostcode() || !$oPosition->maybePhrase('postalcode')) {
- return $aNewSearches;
- }
-
// If we have structured search or this is the first term,
// make the postcode the primary search element.
if ($oSearch->hasOperator(\Nominatim\Operator::NONE) && $oPosition->isFirstToken()) {
return $this->iId;
}
+ /**
+ * Check if the token can be added to the given search.
+ * Derive new searches by adding this token to an existing search.
+ *
+ * @param object $oSearch Partial search description derived so far.
+ * @param object $oPosition Description of the token position within
+ the query.
+ *
+ * @return True if the token is compatible with the search configuration
+ * given the position.
+ */
+ public function isExtendable($oSearch, $oPosition)
+ {
+ return !$oSearch->hasOperator() && $oPosition->isPhrase('');
+ }
+
/**
* Derive new searches by adding this token to an existing search.
*
*/
public function extendSearch($oSearch, $oPosition)
{
- if ($oSearch->hasOperator() || !$oPosition->isPhrase('')) {
- return array();
- }
-
$iSearchCost = 2;
$iOp = $this->iOperator;
return $this->iId;
}
+ /**
+ * Check if the token can be added to the given search.
+ * Derive new searches by adding this token to an existing search.
+ *
+ * @param object $oSearch Partial search description derived so far.
+ * @param object $oPosition Description of the token position within
+ the query.
+ *
+ * @return True if the token is compatible with the search configuration
+ * given the position.
+ */
+ public function isExtendable($oSearch, $oPosition)
+ {
+ return !$oPosition->isPhrase('country');
+ }
+
/**
* Derive new searches by adding this token to an existing search.
*
*/
public function extendSearch($oSearch, $oPosition)
{
- if ($oPosition->isPhrase('country')) {
- return array();
- }
-
// Full words can only be a name if they appear at the beginning
// of the phrase. In structured search the name must forcably in
// the first phrase. In unstructured search it may be in a later