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.'/SpecialSearchOperator.php');
13 * Saves information about the tokens that appear in a search query.
15 * Tokens are sorted by their normalized form, the token word. There are different
16 * kinds of tokens, represented by different Token* classes. Note that
17 * tokens do not have a common base class. All tokens need to have a field
18 * with the word id that points to an entry in the `word` database table
19 * but otherwise the information saved about a token can be very different.
21 * There are two different kinds of token words: full words and partial terms.
23 * Full words start with a space. They represent a complete name of a place.
24 * All special tokens are normally full words.
26 * Partial terms have no space at the beginning. They may represent a part of
27 * a name of a place (e.g. in the name 'World Trade Center' a partial term
28 * would be 'Trade' or 'Trade Center'). They are only used in TokenWord.
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]) || 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') && !$oToken->bPartial) {
90 $ids[$oToken->iId] = $oToken->iId;
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 if ($aToken->iId !== null) {
121 $aWordsIDs[$aToken->iId] =
122 '#'.$sToken.'('.$aToken->iId.')#';
130 public function debugInfo()
132 return $this->aTokens;