+ $aNewSearches = array();
+
+ if (($sPhraseType == '' || $sPhraseType == 'country')
+ && !empty($aSearchTerm['country_code'])
+ && $aSearchTerm['country_code'] != '0'
+ ) {
+ if (!$this->sCountryCode) {
+ $oSearch = clone $this;
+ $oSearch->iSearchRank++;
+ $oSearch->sCountryCode = $aSearchTerm['country_code'];
+ // Country is almost always at the end of the string
+ // - increase score for finding it anywhere else (optimisation)
+ if (!$bLastToken) {
+ $oSearch->iSearchRank += 5;
+ }
+ $aNewSearches[] = $oSearch;
+ }
+ } elseif (($sPhraseType == '' || $sPhraseType == 'postalcode')
+ && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'postcode'
+ ) {
+ // We need to try the case where the postal code is the primary element
+ // (i.e. no way to tell if it is (postalcode, city) OR (city, postalcode)
+ // so try both.
+ if (!$this->sPostcode
+ && $aSearchTerm['word']
+ && pg_escape_string($aSearchTerm['word']) == $aSearchTerm['word']
+ ) {
+ // If we have structured search or this is the first term,
+ // make the postcode the primary search element.
+ if ($this->iOperator == Operator::NONE
+ && ($sPhraseType == 'postalcode' || $bFirstToken)
+ ) {
+ $oSearch = clone $this;
+ $oSearch->iSearchRank++;
+ $oSearch->iOperator = Operator::POSTCODE;
+ $oSearch->aAddress = array_merge($this->aAddress, $this->aName);
+ $oSearch->aName =
+ array($aSearchTerm['word_id'] => $aSearchTerm['word']);
+ $aNewSearches[] = $oSearch;
+ }
+
+ // If we have a structured search or this is not the first term,
+ // add the postcode as an addendum.
+ if ($this->iOperator != Operator::POSTCODE
+ && ($sPhraseType == 'postalcode' || sizeof($this->aName))
+ ) {
+ $oSearch = clone $this;
+ $oSearch->iSearchRank++;
+ $oSearch->sPostcode = $aSearchTerm['word'];
+ $aNewSearches[] = $oSearch;
+ }
+ }
+ } elseif (($sPhraseType == '' || $sPhraseType == 'street')
+ && $aSearchTerm['class'] == 'place' && $aSearchTerm['type'] == 'house'
+ ) {
+ if (!$this->sHouseNumber && $this->iOperator != Operator::POSTCODE) {
+ $oSearch = clone $this;
+ $oSearch->iSearchRank++;
+ $oSearch->sHouseNumber = trim($aSearchTerm['word_token']);
+ // sanity check: if the housenumber is not mainly made
+ // up of numbers, add a penalty
+ if (preg_match_all('/[^0-9]/', $oSearch->sHouseNumber, $aMatches) > 2) {
+ $oSearch->iSearchRank++;
+ }
+ if (!isset($aSearchTerm['word_id'])) {
+ $oSearch->iSearchRank++;
+ }
+ // also must not appear in the middle of the address
+ if (sizeof($this->aAddress)
+ || sizeof($this->aAddressNonSearch)
+ || $this->sPostcode
+ ) {
+ $oSearch->iSearchRank++;
+ }
+ $aNewSearches[] = $oSearch;
+ }
+ } elseif ($sPhraseType == '' && $aSearchTerm['class']) {
+ if ($this->iOperator == Operator::NONE) {
+ $oSearch = clone $this;
+ $oSearch->iSearchRank++;
+
+ $iOp = Operator::NEAR; // near == in for the moment
+ if ($aSearchTerm['operator'] == '') {
+ if (sizeof($this->aName)) {
+ $iOp = Operator::NAME;
+ }
+ $oSearch->iSearchRank += 2;
+ }
+
+ $oSearch->setPoiSearch($iOp, $aSearchTerm['class'], $aSearchTerm['type']);
+ $aNewSearches[] = $oSearch;
+ }
+ } elseif (isset($aSearchTerm['word_id'])
+ && $aSearchTerm['word_id']
+ && $sPhraseType != 'country'
+ ) {
+ $iWordID = $aSearchTerm['word_id'];
+ if (sizeof($this->aName)) {
+ if (($sPhraseType == '' || !$bFirstPhrase)
+ && $sPhraseType != 'country'
+ && !$bHasPartial
+ ) {
+ $oSearch = clone $this;
+ $oSearch->iSearchRank++;
+ $oSearch->aAddress[$iWordID] = $iWordID;
+ $aNewSearches[] = $oSearch;
+ } else {
+ $this->aFullNameAddress[$iWordID] = $iWordID;
+ }
+ } else {
+ $oSearch = clone $this;
+ $oSearch->iSearchRank++;
+ $oSearch->aName = array($iWordID => $iWordID);
+ $aNewSearches[] = $oSearch;
+ }
+ }
+
+ return $aNewSearches;