3  * SPDX-License-Identifier: GPL-2.0-only
 
   5  * This file is part of Nominatim. (https://nominatim.org)
 
   7  * Copyright (C) 2022 by the Nominatim developer community.
 
   8  * For a full list of authors see the git log.
 
  11 namespace Nominatim\Token;
 
  14  * A house number token.
 
  18     /// Database word id, if available.
 
  20     /// Normalized house number.
 
  23     public function __construct($iId, $sToken)
 
  26         $this->sToken = $sToken;
 
  29     public function getId()
 
  35      * Check if the token can be added to the given search.
 
  36      * Derive new searches by adding this token to an existing search.
 
  38      * @param object  $oSearch      Partial search description derived so far.
 
  39      * @param object  $oPosition    Description of the token position within
 
  42      * @return True if the token is compatible with the search configuration
 
  45     public function isExtendable($oSearch, $oPosition)
 
  47         return !$oSearch->hasHousenumber()
 
  48                && !$oSearch->hasOperator(\Nominatim\Operator::POSTCODE)
 
  49                && $oPosition->maybePhrase('street');
 
  53      * Derive new searches by adding this token to an existing search.
 
  55      * @param object  $oSearch      Partial search description derived so far.
 
  56      * @param object  $oPosition    Description of the token position within
 
  59      * @return SearchDescription[] List of derived search descriptions.
 
  61     public function extendSearch($oSearch, $oPosition)
 
  63         $aNewSearches = array();
 
  65         // sanity check: if the housenumber is not mainly made
 
  66         // up of numbers, add a penalty
 
  68         if (preg_match('/\\d/', $this->sToken) === 0
 
  69             || preg_match_all('/[^0-9 ]/', $this->sToken, $aMatches) > 3) {
 
  70             $iSearchCost += strlen($this->sToken) - 1;
 
  72         if (!$oSearch->hasOperator(\Nominatim\Operator::NONE)) {
 
  75         if (empty($this->iId)) {
 
  78         // also must not appear in the middle of the address
 
  79         if ($oSearch->hasAddress() || $oSearch->hasPostcode()) {
 
  83         $oNewSearch = $oSearch->clone($iSearchCost);
 
  84         $oNewSearch->setHousenumber($this->sToken);
 
  85         $aNewSearches[] = $oNewSearch;
 
  87         // Housenumbers may appear in the name when the place has its own
 
  89         if ($this->iId !== null
 
  90             && ($oSearch->getNamePhrase() >= 0 || !$oSearch->hasName())
 
  91             && !$oSearch->hasAddress()
 
  93             $oNewSearch = $oSearch->clone($iSearchCost);
 
  94             $oNewSearch->setHousenumberAsName($this->iId);
 
  96             $aNewSearches[] = $oNewSearch;
 
 103     public function debugInfo()
 
 107                 'Type' => 'house number',
 
 108                 'Info' => array('nr' => $this->sToken)
 
 112     public function debugCode()