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.
 
  13 require_once(CONST_LibDir.'/TokenCountry.php');
 
  14 require_once(CONST_LibDir.'/TokenHousenumber.php');
 
  15 require_once(CONST_LibDir.'/TokenPostcode.php');
 
  16 require_once(CONST_LibDir.'/TokenSpecialTerm.php');
 
  17 require_once(CONST_LibDir.'/TokenWord.php');
 
  18 require_once(CONST_LibDir.'/TokenPartial.php');
 
  19 require_once(CONST_LibDir.'/SpecialSearchOperator.php');
 
  22  * Saves information about the tokens that appear in a search query.
 
  24  * Tokens are sorted by their normalized form, the token word. There are different
 
  25  * kinds of tokens, represented by different Token* classes. Note that
 
  26  * tokens do not have a common base class. All tokens need to have a field
 
  27  * with the word id that points to an entry in the `word` database table
 
  28  * but otherwise the information saved about a token can be very different.
 
  32     // List of list of tokens indexed by their word_token.
 
  33     private $aTokens = array();
 
  37      * Return total number of tokens.
 
  41     public function count()
 
  43         return count($this->aTokens);
 
  47      * Check if there are tokens for the given token word.
 
  49      * @param string $sWord Token word to look for.
 
  51      * @return bool True if there is one or more token for the token word.
 
  53     public function contains($sWord)
 
  55         return isset($this->aTokens[$sWord]);
 
  59      * Check if there are partial or full tokens for the given word.
 
  61      * @param string $sWord Token word to look for.
 
  63      * @return bool True if there is one or more token for the token word.
 
  65     public function containsAny($sWord)
 
  67         return isset($this->aTokens[$sWord]);
 
  71      * Get the list of tokens for the given token word.
 
  73      * @param string $sWord Token word to look for.
 
  75      * @return object[] Array of tokens for the given token word or an
 
  76      *                  empty array if no tokens could be found.
 
  78     public function get($sWord)
 
  80         return isset($this->aTokens[$sWord]) ? $this->aTokens[$sWord] : array();
 
  83     public function getFullWordIDs()
 
  87         foreach ($this->aTokens as $aTokenList) {
 
  88             foreach ($aTokenList as $oToken) {
 
  89                 if (is_a($oToken, '\Nominatim\Token\Word')) {
 
  90                     $ids[$oToken->getId()] = $oToken->getId();
 
  99      * Add a new token for the given word.
 
 101      * @param string $sWord  Word the token describes.
 
 102      * @param object $oToken Token object to add.
 
 106     public function addToken($sWord, $oToken)
 
 108         if (isset($this->aTokens[$sWord])) {
 
 109             $this->aTokens[$sWord][] = $oToken;
 
 111             $this->aTokens[$sWord] = array($oToken);
 
 115     public function debugTokenByWordIdList()
 
 117         $aWordsIDs = array();
 
 118         foreach ($this->aTokens as $sToken => $aWords) {
 
 119             foreach ($aWords as $aToken) {
 
 120                 $iId = $aToken->getId();
 
 122                     $aWordsIDs[$iId] = '#'.$sToken.'('.$aToken->debugCode().' '.$iId.')#';
 
 130     public function debugInfo()
 
 132         return $this->aTokens;