]> git.openstreetmap.org Git - nominatim.git/blob - lib-php/TokenList.php
Merge remote-tracking branch 'upstream/master'
[nominatim.git] / lib-php / TokenList.php
1 <?php
2
3 namespace Nominatim;
4
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');
12
13 /**
14  * Saves information about the tokens that appear in a search query.
15  *
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.
21  */
22 class TokenList
23 {
24     // List of list of tokens indexed by their word_token.
25     private $aTokens = array();
26
27
28     /**
29      * Return total number of tokens.
30      *
31      * @return Integer
32      */
33     public function count()
34     {
35         return count($this->aTokens);
36     }
37
38     /**
39      * Check if there are tokens for the given token word.
40      *
41      * @param string $sWord Token word to look for.
42      *
43      * @return bool True if there is one or more token for the token word.
44      */
45     public function contains($sWord)
46     {
47         return isset($this->aTokens[$sWord]);
48     }
49
50     /**
51      * Check if there are partial or full tokens for the given word.
52      *
53      * @param string $sWord Token word to look for.
54      *
55      * @return bool True if there is one or more token for the token word.
56      */
57     public function containsAny($sWord)
58     {
59         return isset($this->aTokens[$sWord]);
60     }
61
62     /**
63      * Get the list of tokens for the given token word.
64      *
65      * @param string $sWord Token word to look for.
66      *
67      * @return object[] Array of tokens for the given token word or an
68      *                  empty array if no tokens could be found.
69      */
70     public function get($sWord)
71     {
72         return isset($this->aTokens[$sWord]) ? $this->aTokens[$sWord] : array();
73     }
74
75     public function getFullWordIDs()
76     {
77         $ids = array();
78
79         foreach ($this->aTokens as $aTokenList) {
80             foreach ($aTokenList as $oToken) {
81                 if (is_a($oToken, '\Nominatim\Token\Word')) {
82                     $ids[$oToken->getId()] = $oToken->getId();
83                 }
84             }
85         }
86
87         return $ids;
88     }
89
90     /**
91      * Add a new token for the given word.
92      *
93      * @param string $sWord  Word the token describes.
94      * @param object $oToken Token object to add.
95      *
96      * @return void
97      */
98     public function addToken($sWord, $oToken)
99     {
100         if (isset($this->aTokens[$sWord])) {
101             $this->aTokens[$sWord][] = $oToken;
102         } else {
103             $this->aTokens[$sWord] = array($oToken);
104         }
105     }
106
107     public function debugTokenByWordIdList()
108     {
109         $aWordsIDs = array();
110         foreach ($this->aTokens as $sToken => $aWords) {
111             foreach ($aWords as $aToken) {
112                 $iId = $aToken->getId();
113                 if ($iId !== null) {
114                     $aWordsIDs[$iId] = '#'.$sToken.'('.$aToken->debugCode().' '.$iId.')#';
115                 }
116             }
117         }
118
119         return $aWordsIDs;
120     }
121
122     public function debugInfo()
123     {
124         return $this->aTokens;
125     }
126 }