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 normalized postcode (upper cased).
 
  22     // Optional country code the postcode belongs to (currently unused).
 
  23     private $sCountryCode;
 
  25     public function __construct($iId, $sPostcode, $sCountryCode = '')
 
  28         $iSplitPos = strpos($sPostcode, '@');
 
  29         if ($iSplitPos === false) {
 
  30             $this->sPostcode = $sPostcode;
 
  32             $this->sPostcode = substr($sPostcode, 0, $iSplitPos);
 
  34         $this->sCountryCode = empty($sCountryCode) ? '' : $sCountryCode;
 
  37     public function getId()
 
  43      * Check if the token can be added to the given search.
 
  44      * Derive new searches by adding this token to an existing search.
 
  46      * @param object  $oSearch      Partial search description derived so far.
 
  47      * @param object  $oPosition    Description of the token position within
 
  50      * @return True if the token is compatible with the search configuration
 
  53     public function isExtendable($oSearch, $oPosition)
 
  55         return !$oSearch->hasPostcode() && $oPosition->maybePhrase('postalcode');
 
  59      * Derive new searches by adding this token to an existing search.
 
  61      * @param object  $oSearch      Partial search description derived so far.
 
  62      * @param object  $oPosition    Description of the token position within
 
  65      * @return SearchDescription[] List of derived search descriptions.
 
  67     public function extendSearch($oSearch, $oPosition)
 
  69         $aNewSearches = array();
 
  71         // If we have structured search or this is the first term,
 
  72         // make the postcode the primary search element.
 
  73         if ($oSearch->hasOperator(\Nominatim\Operator::NONE) && $oPosition->isFirstToken()) {
 
  74             $oNewSearch = $oSearch->clone(1);
 
  75             $oNewSearch->setPostcodeAsName($this->iId, $this->sPostcode);
 
  77             $aNewSearches[] = $oNewSearch;
 
  80         // If we have a structured search or this is not the first term,
 
  81         // add the postcode as an addendum.
 
  82         if (!$oSearch->hasOperator(\Nominatim\Operator::POSTCODE)
 
  83             && ($oPosition->isPhrase('postalcode') || $oSearch->hasName())
 
  86             if (strlen($this->sPostcode) < 4) {
 
  87                 $iPenalty += 4 - strlen($this->sPostcode);
 
  89             $oNewSearch = $oSearch->clone($iPenalty);
 
  90             $oNewSearch->setPostcode($this->sPostcode);
 
  92             $aNewSearches[] = $oNewSearch;
 
  98     public function debugInfo()
 
 102                 'Type' => 'postcode',
 
 103                 'Info' => $this->sPostcode.'('.$this->sCountryCode.')'
 
 107     public function debugCode()