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;
 
  18     /// Database word id, if available.
 
  20     /// Full nomralized postcode (upper cased).
 
  22     // Optional country code the postcode belongs to (currently unused).
 
  23     private $sCountryCode;
 
  25     public function __construct($iId, $sPostcode, $sCountryCode = '')
 
  28         $this->sPostcode = $sPostcode;
 
  29         $this->sCountryCode = empty($sCountryCode) ? '' : $sCountryCode;
 
  32     public function getId()
 
  38      * Check if the token can be added to the given search.
 
  39      * Derive new searches by adding this token to an existing search.
 
  41      * @param object  $oSearch      Partial search description derived so far.
 
  42      * @param object  $oPosition    Description of the token position within
 
  45      * @return True if the token is compatible with the search configuration
 
  48     public function isExtendable($oSearch, $oPosition)
 
  50         return !$oSearch->hasPostcode() && $oPosition->maybePhrase('postalcode');
 
  54      * Derive new searches by adding this token to an existing search.
 
  56      * @param object  $oSearch      Partial search description derived so far.
 
  57      * @param object  $oPosition    Description of the token position within
 
  60      * @return SearchDescription[] List of derived search descriptions.
 
  62     public function extendSearch($oSearch, $oPosition)
 
  64         $aNewSearches = array();
 
  66         // If we have structured search or this is the first term,
 
  67         // make the postcode the primary search element.
 
  68         if ($oSearch->hasOperator(\Nominatim\Operator::NONE) && $oPosition->isFirstToken()) {
 
  69             $oNewSearch = $oSearch->clone(1);
 
  70             $oNewSearch->setPostcodeAsName($this->iId, $this->sPostcode);
 
  72             $aNewSearches[] = $oNewSearch;
 
  75         // If we have a structured search or this is not the first term,
 
  76         // add the postcode as an addendum.
 
  77         if (!$oSearch->hasOperator(\Nominatim\Operator::POSTCODE)
 
  78             && ($oPosition->isPhrase('postalcode') || $oSearch->hasName())
 
  81             if (strlen($this->sPostcode) < 4) {
 
  82                 $iPenalty += 4 - strlen($this->sPostcode);
 
  84             $oNewSearch = $oSearch->clone($iPenalty);
 
  85             $oNewSearch->setPostcode($this->sPostcode);
 
  87             $aNewSearches[] = $oNewSearch;
 
  93     public function debugInfo()
 
  98                 'Info' => $this->sPostcode.'('.$this->sCountryCode.')'
 
 102     public function debugCode()