5 require_once(CONST_LibDir.'/TokenCountry.php');
6 require_once(CONST_LibDir.'/TokenHousenumber.php');
7 require_once(CONST_LibDir.'/TokenPostcode.php');
8 require_once(CONST_LibDir.'/TokenSpecialTerm.php');
9 require_once(CONST_LibDir.'/TokenWord.php');
10 require_once(CONST_LibDir.'/TokenPartial.php');
11 require_once(CONST_LibDir.'/SpecialSearchOperator.php');
14 * Saves information about the tokens that appear in a search query.
16 * Tokens are sorted by their normalized form, the token word. There are different
17 * kinds of tokens, represented by different Token* classes. Note that
18 * tokens do not have a common base class. All tokens need to have a field
19 * with the word id that points to an entry in the `word` database table
20 * but otherwise the information saved about a token can be very different.
22 * There are two different kinds of token words: full words and partial terms.
24 * Full words start with a space. They represent a complete name of a place.
25 * All special tokens are normally full words.
27 * Partial terms have no space at the beginning. They may represent a part of
28 * a name of a place (e.g. in the name 'World Trade Center' a partial term
29 * would be 'Trade' or 'Trade Center'). They are only used in TokenWord.
33 // List of list of tokens indexed by their word_token.
34 private $aTokens = array();
38 * Return total number of tokens.
42 public function count()
44 return count($this->aTokens);
48 * Check if there are tokens for the given token word.
50 * @param string $sWord Token word to look for.
52 * @return bool True if there is one or more token for the token word.
54 public function contains($sWord)
56 return isset($this->aTokens[$sWord]);
60 * Check if there are partial or full tokens for the given word.
62 * @param string $sWord Token word to look for.
64 * @return bool True if there is one or more token for the token word.
66 public function containsAny($sWord)
68 return isset($this->aTokens[$sWord]) || isset($this->aTokens[' '.$sWord]);
72 * Get the list of tokens for the given token word.
74 * @param string $sWord Token word to look for.
76 * @return object[] Array of tokens for the given token word or an
77 * empty array if no tokens could be found.
79 public function get($sWord)
81 return isset($this->aTokens[$sWord]) ? $this->aTokens[$sWord] : array();
84 public function getFullWordIDs()
88 foreach ($this->aTokens as $aTokenList) {
89 foreach ($aTokenList as $oToken) {
90 if (is_a($oToken, '\Nominatim\Token\Word') && !$oToken->bPartial) {
91 $ids[$oToken->iId] = $oToken->iId;
100 * Add a new token for the given word.
102 * @param string $sWord Word the token describes.
103 * @param object $oToken Token object to add.
107 public function addToken($sWord, $oToken)
109 if (isset($this->aTokens[$sWord])) {
110 $this->aTokens[$sWord][] = $oToken;
112 $this->aTokens[$sWord] = array($oToken);
116 public function debugTokenByWordIdList()
118 $aWordsIDs = array();
119 foreach ($this->aTokens as $sToken => $aWords) {
120 foreach ($aWords as $aToken) {
121 if ($aToken->iId !== null) {
122 $aWordsIDs[$aToken->iId] =
123 '#'.$sToken.'('.$aToken->iId.')#';
131 public function debugInfo()
133 return $this->aTokens;